Datasets:

ArXiv:
License:
File size: 3,253 Bytes
674e95d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import yaml

import open3d as o3d

import numpy as np

import zarr
from tempfile import NamedTemporaryFile
import fsspec


from qdgset.utils.import_utils import load_mesh_from_vertices_and_triangles

def export_to_obj_and_urdf_and_npz(object_group, directory_path, fs = fsspec.filesystem('local')):
    vertices = object_group.obj.vertices
    triangles = object_group.obj.faces



    path = directory_path + object_group.name
    name = path.split("/")[-1]

    obj_path = path + "/" + name +  ".obj"
    urdf_path = path + "/"+ name + ".urdf"
    grasps_path = path + "/"+ "individuals_0.npz"

    fs.makedirs(path)

    mesh = load_mesh_from_vertices_and_triangles(vertices, triangles)

    base_name = os.path.basename(obj_path)

    with NamedTemporaryFile(suffix=".obj") as tmp_file:
        o3d.io.write_triangle_mesh(tmp_file.name, mesh)
        fs.cp(tmp_file.name, obj_path)

        inertia_xyz_str = " ".join(map(str,np.asarray(object_group.obj.inertia_origin[:3])))
    mass = object_group.obj.mass[0]
    ixx, ixy, ixz, iyy, iyz, izz = object_group.obj.inertia

    with fs.open(urdf_path, "w") as urdf_file:
        urdf_file.write(f"""
<?xml version="1.0"?>
<robot name="object">
    <link name="object_link">
    <visual>
        <origin rpy="0 0 0" xyz="0 0 0" />
        <geometry>
            <mesh filename="{base_name}" scale="1 1 1" />
        </geometry>
    </visual>
    <collision>
        <origin rpy="0 0 0" xyz="0 0 0" />
        <geometry>
            <mesh filename="{base_name}" scale="1 1 1" />
        </geometry>
    </collision>
    <inertial>
        <origin rpy="0 0 0" xyz="{inertia_xyz_str}" />
        <mass value="{mass}" />
        <inertia ixx="{ixx}" ixy="{ixy}" ixz="{ixz}"
                 iyy="{iyy}" iyz="{iyz}" izz="{izz}" />
    </inertial>
  </link>
</robot>""".strip()) #!!IMPORTANT WE ASSUME NO ROTATION BETWEEN INERTIAL FRAME AND GEOMETRY FRAME!!


    if "grasps" in object_group.keys():
        info_keys = np.asarray(['xyz_pose_x', 'xyz_pose_y', 'xyz_pose_z', 'quat_1', 'quat_2', 'quat_3', 'quat_4'])

        with fs.open(grasps_path,"wb") as grasp_file:
            np.savez(grasp_file, infos=np.asarray(object_group.grasps), infos_keys=info_keys)
    else:
        print(f"WARN: Object {object_group.name} has no grasps")


if __name__=="__main__":
    import multiprocessing
    from multiprocessing import Pool, cpu_count
    import zarr
    zarr.blosc.use_threads = False

    import argparse

    def arg_parser():
        parser = argparse.ArgumentParser()
        parser.add_argument("-o", "--out", help="Export path", type=str, required=True)
        parser.add_argument("-i", "--in_zarr", help="Path of the zarr database", type=str, required=True)
        return parser.parse_args()

    parser = arg_parser()
    export_directory = parser.out
    def export(obj):
        return export_to_obj_and_urdf_and_npz(obj, export_directory)


    nb_cpu = cpu_count() -2
    p = Pool(nb_cpu)

    zarr_path = parser.in_zarr
    with zarr.ZipStore(zarr_path, mode='r') as store:
        root = zarr.group(store=store)

        for dataset in root.datasets:
            print(f"INFO: Export {dataset} objects and grasps")
            p.map(export, root.datasets[dataset].values())