|
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()) |
|
|
|
|
|
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()) |
|
|