merging with main mujoco repo
This commit is contained in:
+14
-126
@@ -15,6 +15,7 @@
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
import mujoco
|
||||
import mujoco.usd.utils
|
||||
import numpy as np
|
||||
|
||||
# TODO: b/288149332 - Remove once USD Python Binding works well with pytype.
|
||||
@@ -38,7 +39,7 @@ class USDMesh:
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
dataid: int,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
rgba: np.ndarray = np.array([1, 1, 1, 1]),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
self.stage = stage
|
||||
@@ -236,7 +237,7 @@ class USDPrimitiveMesh:
|
||||
stage: Usd.Stage,
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
rgba: np.ndarray = np.array([1, 1, 1, 1]),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
self.stage = stage
|
||||
@@ -245,7 +246,10 @@ class USDPrimitiveMesh:
|
||||
self.rgba = rgba
|
||||
self.texture_file = texture_file
|
||||
|
||||
self.usd_prim = Usd.Prim()
|
||||
self.usd_mesh = UsdGeom.Mesh()
|
||||
self.prim_mesh = None
|
||||
self.transform_op = Gf.Matrix4d(1.)
|
||||
|
||||
def _set_refinement_properties(self):
|
||||
self.usd_prim.GetAttribute("subdivisionScheme").Set("none")
|
||||
@@ -374,119 +378,6 @@ class USDPrimitiveMesh:
|
||||
self.usd_prim.GetAttribute("visibility").Set("invisible", frame)
|
||||
|
||||
|
||||
class USDPrimitive:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
stage: Usd.Stage,
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
self.stage = stage
|
||||
self.geom = geom
|
||||
self.obj_name = obj_name
|
||||
self.rgba = rgba
|
||||
self.texture_file = texture_file
|
||||
|
||||
def _set_refinement_properties(self):
|
||||
self.usd_prim.GetAttribute("subdivisionScheme").Set("none")
|
||||
|
||||
def _attach_material(self):
|
||||
mtl_path = Sdf.Path(f"/World/_materials/Material_{self.obj_name}")
|
||||
mtl = UsdShade.Material.Define(self.stage, mtl_path)
|
||||
if self.texture_file:
|
||||
bsdf_shader = UsdShade.Shader.Define(
|
||||
self.stage, mtl_path.AppendPath("Principled_BSDF")
|
||||
)
|
||||
image_shader = UsdShade.Shader.Define(
|
||||
self.stage, mtl_path.AppendPath("Image_Texture")
|
||||
)
|
||||
uvmap_shader = UsdShade.Shader.Define(
|
||||
self.stage, mtl_path.AppendPath("uvmap")
|
||||
)
|
||||
|
||||
# settings the bsdf shader attributes
|
||||
bsdf_shader.CreateIdAttr("UsdPreviewSurface")
|
||||
bsdf_shader.CreateInput(
|
||||
"diffuseColor", Sdf.ValueTypeNames.Color3f
|
||||
).ConnectToSource(image_shader.ConnectableAPI(), "rgb")
|
||||
bsdf_shader.CreateInput("opacity", Sdf.ValueTypeNames.Float).Set(
|
||||
float(self.rgba[-1])
|
||||
)
|
||||
bsdf_shader.CreateInput("metallic", Sdf.ValueTypeNames.Float).Set(
|
||||
self.geom.shininess
|
||||
)
|
||||
bsdf_shader.CreateInput("roughness", Sdf.ValueTypeNames.Float).Set(
|
||||
1.0 - self.geom.shininess
|
||||
)
|
||||
|
||||
mtl.CreateSurfaceOutput().ConnectToSource(
|
||||
bsdf_shader.ConnectableAPI(), "surface"
|
||||
)
|
||||
|
||||
self.usd_primitive_shape.GetPrim().ApplyAPI(UsdShade.MaterialBindingAPI)
|
||||
UsdShade.MaterialBindingAPI(self.usd_primitive_shape).Bind(mtl)
|
||||
|
||||
# setting the image texture attributes
|
||||
image_shader.CreateIdAttr("UsdUVTexture")
|
||||
image_shader.CreateInput("file", Sdf.ValueTypeNames.Asset).Set(
|
||||
self.texture_file
|
||||
)
|
||||
image_shader.CreateInput(
|
||||
"sourceColorSpace", Sdf.ValueTypeNames.Token
|
||||
).Set("sRGB")
|
||||
image_shader.CreateInput("st", Sdf.ValueTypeNames.Float2).ConnectToSource(
|
||||
uvmap_shader.ConnectableAPI(), "result"
|
||||
)
|
||||
image_shader.CreateOutput("rgb", Sdf.ValueTypeNames.Float3)
|
||||
|
||||
# setting uvmap shader attributes
|
||||
uvmap_shader.CreateIdAttr("UsdPrimvarReader_float2")
|
||||
uvmap_shader.CreateInput("varname", Sdf.ValueTypeNames.Token).Set("UVMap")
|
||||
uvmap_shader.CreateOutput("results", Sdf.ValueTypeNames.Float2)
|
||||
else:
|
||||
bsdf_shader = UsdShade.Shader.Define(
|
||||
self.stage, mtl_path.AppendPath("Principled_BSDF")
|
||||
)
|
||||
|
||||
# settings the bsdf shader attributes
|
||||
bsdf_shader.CreateIdAttr("UsdPreviewSurface")
|
||||
bsdf_shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).Set(
|
||||
tuple(self.rgba[:3])
|
||||
)
|
||||
bsdf_shader.CreateInput("opacity", Sdf.ValueTypeNames.Float).Set(
|
||||
float(self.rgba[-1])
|
||||
)
|
||||
bsdf_shader.CreateInput("metallic", Sdf.ValueTypeNames.Float).Set(
|
||||
self.geom.shininess
|
||||
)
|
||||
bsdf_shader.CreateInput("roughness", Sdf.ValueTypeNames.Float).Set(
|
||||
1.0 - self.geom.shininess
|
||||
)
|
||||
|
||||
mtl.CreateSurfaceOutput().ConnectToSource(
|
||||
bsdf_shader.ConnectableAPI(), "surface"
|
||||
)
|
||||
|
||||
self.usd_primitive_shape.GetPrim().ApplyAPI(UsdShade.MaterialBindingAPI)
|
||||
UsdShade.MaterialBindingAPI(self.usd_primitive_shape).Bind(mtl)
|
||||
|
||||
def update(self, pos: np.ndarray, mat: np.ndarray, visible: bool, frame: int):
|
||||
transformation_mat = mujoco.usd.utils.create_transform_matrix(
|
||||
rotation_matrix=mat, translation_vector=pos
|
||||
).T
|
||||
self.transform_op.Set(Gf.Matrix4d(transformation_mat.tolist()), frame)
|
||||
self.update_visibility(visible, frame)
|
||||
|
||||
def update_visibility(self, visible: bool, frame: int):
|
||||
if visible:
|
||||
self.usd_prim.GetAttribute("visibility").Set("inherited", frame)
|
||||
else:
|
||||
self.usd_prim.GetAttribute("visibility").Set("invisible", frame)
|
||||
|
||||
|
||||
class USDCapsule(USDPrimitive):
|
||||
|
||||
def __init__(
|
||||
@@ -494,7 +385,7 @@ class USDCapsule(USDPrimitive):
|
||||
stage: Usd.Stage,
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
rgba: np.ndarray = np.array([1, 1, 1, 1]),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
|
||||
@@ -514,7 +405,7 @@ class USDCapsule(USDPrimitive):
|
||||
self._set_size_attributes()
|
||||
self._attach_material()
|
||||
|
||||
self._set_refinement_properties()
|
||||
# self._set_refinement_properties()
|
||||
|
||||
def _set_size_attributes(self):
|
||||
self.usd_primitive_shape.GetRadiusAttr().Set(float(self.geom.size[0]))
|
||||
@@ -530,7 +421,7 @@ class USDEllipsoid(USDPrimitive):
|
||||
stage: Usd.Stage,
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
rgba: np.ndarray = np.array([1, 1, 1, 1]),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
|
||||
@@ -550,7 +441,7 @@ class USDEllipsoid(USDPrimitive):
|
||||
self._set_size_attributes()
|
||||
self._attach_material()
|
||||
|
||||
self._set_refinement_properties()
|
||||
# self._set_refinement_properties()
|
||||
|
||||
def _set_size_attributes(self):
|
||||
self.scale_op.Set(Gf.Vec3d(self.geom.size.tolist()))
|
||||
@@ -563,7 +454,7 @@ class USDCubeMesh(USDPrimitiveMesh):
|
||||
stage: Usd.Stage,
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
rgba: np.ndarray = np.array([1, 1, 1, 1]),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
|
||||
@@ -579,8 +470,6 @@ class USDCubeMesh(USDPrimitiveMesh):
|
||||
width=self.geom.size[0] * 2,
|
||||
height=self.geom.size[1] * 2,
|
||||
depth=self.geom.size[2] * 2,
|
||||
create_uv_map=True,
|
||||
map_texture_to_each_face=True,
|
||||
)
|
||||
|
||||
self.prim_mesh.translate(-self.prim_mesh.get_center())
|
||||
@@ -617,7 +506,7 @@ class USDSphereMesh(USDPrimitiveMesh):
|
||||
stage: Usd.Stage,
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
rgba: np.ndarray = np.array([1, 1, 1, 1]),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
|
||||
@@ -667,7 +556,7 @@ class USDCylinderMesh(USDPrimitiveMesh):
|
||||
stage: Usd.Stage,
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
rgba: np.ndarray = np.array([1, 1, 1, 1]),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
|
||||
@@ -719,7 +608,7 @@ class USDPlaneMesh(USDPrimitiveMesh):
|
||||
stage: Usd.Stage,
|
||||
geom: mujoco.MjvGeom,
|
||||
obj_name: str,
|
||||
rgba: Tuple[int, ...] = (1, 1, 1, 1),
|
||||
rgba: np.ndarray = np.array([1, 1, 1, 1]),
|
||||
texture_file: Optional[str] = None,
|
||||
):
|
||||
|
||||
@@ -814,7 +703,6 @@ class USDDomeLight:
|
||||
self.usd_light.GetNormalizeAttr().Set(True)
|
||||
|
||||
def update(self, intensity: int, color: np.ndarray, frame: int):
|
||||
|
||||
self.usd_light.GetIntensityAttr().Set(intensity)
|
||||
self.usd_light.GetExposureAttr().Set(0.0)
|
||||
self.usd_light.GetColorAttr().Set(Gf.Vec3d(color.tolist()))
|
||||
|
||||
@@ -14,6 +14,6 @@ if __name__ == "__main__":
|
||||
|
||||
exp.update_scene(d)
|
||||
|
||||
exp.save_scene(filetype="usd")
|
||||
exp.save_scene(filetype="usda")
|
||||
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
import os
|
||||
|
||||
import mujoco
|
||||
import mujoco.usd.component as component_module
|
||||
import numpy as np
|
||||
import scipy
|
||||
import termcolor
|
||||
@@ -235,7 +236,7 @@ class USDExporter:
|
||||
|
||||
# handles meshes in scene
|
||||
if geom.type == mujoco.mjtGeom.mjGEOM_MESH:
|
||||
usd_geom = mujoco.usd.component.USDMesh(
|
||||
usd_geom = component_module.USDMesh(
|
||||
stage=self.stage,
|
||||
model=self.model,
|
||||
geom=geom,
|
||||
@@ -245,7 +246,7 @@ class USDExporter:
|
||||
texture_file=texture_file,
|
||||
)
|
||||
elif geom.type == mujoco.mjtGeom.mjGEOM_PLANE:
|
||||
usd_geom = mujoco.usd.component.USDPlaneMesh(
|
||||
usd_geom = component_module.USDPlaneMesh(
|
||||
stage=self.stage,
|
||||
geom=geom,
|
||||
obj_name=geom_name,
|
||||
@@ -253,7 +254,7 @@ class USDExporter:
|
||||
texture_file=texture_file,
|
||||
)
|
||||
elif geom.type == mujoco.mjtGeom.mjGEOM_SPHERE:
|
||||
usd_geom = mujoco.usd.component.USDSphereMesh(
|
||||
usd_geom = component_module.USDSphereMesh(
|
||||
stage=self.stage,
|
||||
geom=geom,
|
||||
obj_name=geom_name,
|
||||
@@ -261,7 +262,7 @@ class USDExporter:
|
||||
texture_file=texture_file,
|
||||
)
|
||||
elif geom.type == mujoco.mjtGeom.mjGEOM_CAPSULE:
|
||||
usd_geom = mujoco.usd.component.USDCapsule(
|
||||
usd_geom = component_module.USDCapsule(
|
||||
stage=self.stage,
|
||||
geom=geom,
|
||||
obj_name=geom_name,
|
||||
@@ -269,7 +270,7 @@ class USDExporter:
|
||||
texture_file=texture_file,
|
||||
)
|
||||
elif geom.type == mujoco.mjtGeom.mjGEOM_ELLIPSOID:
|
||||
usd_geom = mujoco.usd.component.USDEllipsoid(
|
||||
usd_geom = component_module.USDEllipsoid(
|
||||
stage=self.stage,
|
||||
geom=geom,
|
||||
obj_name=geom_name,
|
||||
@@ -277,7 +278,7 @@ class USDExporter:
|
||||
texture_file=texture_file,
|
||||
)
|
||||
elif geom.type == mujoco.mjtGeom.mjGEOM_CYLINDER:
|
||||
usd_geom = mujoco.usd.component.USDCylinderMesh(
|
||||
usd_geom = component_module.USDCylinderMesh(
|
||||
stage=self.stage,
|
||||
geom=geom,
|
||||
obj_name=geom_name,
|
||||
@@ -285,7 +286,7 @@ class USDExporter:
|
||||
texture_file=texture_file,
|
||||
)
|
||||
elif geom.type == mujoco.mjtGeom.mjGEOM_BOX:
|
||||
usd_geom = mujoco.usd.component.USDCubeMesh(
|
||||
usd_geom = component_module.USDCubeMesh(
|
||||
stage=self.stage,
|
||||
geom=geom,
|
||||
obj_name=geom_name,
|
||||
@@ -331,7 +332,8 @@ class USDExporter:
|
||||
for i in range(self.scene.nlight):
|
||||
light = self.scene.lights[i]
|
||||
if not np.allclose(light.pos, [0, 0, 0]):
|
||||
self.usd_lights.append(mujoco.usd.component.USDSphereLight(stage=self.stage, obj_name=i))
|
||||
self.usd_lights.append
|
||||
(component_module.USDSphereLight(stage=self.stage, obj_name=str(i)))
|
||||
else:
|
||||
self.usd_lights.append(None)
|
||||
|
||||
@@ -342,7 +344,7 @@ class USDExporter:
|
||||
if np.allclose(light.pos, [0, 0, 0]):
|
||||
continue
|
||||
|
||||
if self.usd_lights[i] is None:
|
||||
if i >= len(self.usd_lights) or self.usd_lights[i] is None:
|
||||
continue
|
||||
|
||||
self.usd_lights[i].update(
|
||||
@@ -356,7 +358,8 @@ class USDExporter:
|
||||
self.usd_cameras = []
|
||||
if self.camera_names is not None:
|
||||
for name in self.camera_names:
|
||||
self.usd_cameras.append(mujoco.usd.USDCamera(stage=self.stage, obj_name=name))
|
||||
self.usd_cameras.append(
|
||||
component_module.USDCamera(stage=self.stage, obj_name=name))
|
||||
|
||||
def _update_cameras(
|
||||
self,
|
||||
@@ -372,7 +375,8 @@ class USDExporter:
|
||||
data, scene_option=scene_option, camera=camera_name
|
||||
)
|
||||
|
||||
avg_camera = mujoco.mjv_averageCamera(self.scene.camera[0], self.scene.camera[1])
|
||||
avg_camera = mujoco.mjv_averageCamera(
|
||||
self.scene.camera[0], self.scene.camera[1])
|
||||
|
||||
forward = avg_camera.forward
|
||||
up = avg_camera.up
|
||||
@@ -396,11 +400,12 @@ class USDExporter:
|
||||
):
|
||||
|
||||
if light_type == "sphere":
|
||||
new_light = mujoco.usd.component.USDSphereLight(stage=self.stage, obj_name=obj_name, radius=radius)
|
||||
new_light = component_module.USDSphereLight(stage=self.stage, obj_name=str(objid), radius=radius)
|
||||
|
||||
new_light.update(pos=pos, intensity=intensity, color=color, frame=0)
|
||||
new_light.update(pos=np.array(pos), intensity=intensity, color=color, frame=0)
|
||||
elif light_type == "dome":
|
||||
new_light = mujoco.usd.component.USDDomeLight(stage=self.stage, obj_name=obj_name)
|
||||
new_light = component_module.USDDomeLight(
|
||||
stage=self.stage, obj_name=str(objid))
|
||||
|
||||
new_light.update(intensity=intensity, color=color, frame=0)
|
||||
|
||||
@@ -410,11 +415,12 @@ class USDExporter:
|
||||
rotation_xyz: List[float],
|
||||
obj_name: Optional[str] = "camera_1",
|
||||
):
|
||||
new_camera = mujoco.usd.component.USDCamera(stage=self.stage, obj_name=obj_name)
|
||||
new_camera = component_module.USDCamera(
|
||||
stage=self.stage, obj_name=str(objid))
|
||||
|
||||
r = scipy.spatial.transform.Rotation.from_euler(
|
||||
"xyz", rotation_xyz, degrees=True)
|
||||
new_camera.update(cam_pos=pos, cam_mat=r.as_matrix(), frame=0)
|
||||
new_camera.update(cam_pos=np.array(pos), cam_mat=r.as_matrix(), frame=0)
|
||||
|
||||
def save_scene(self, filetype: str = "usd"):
|
||||
self.stage.SetEndTimeCode(self.frame_count)
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
# Copyright 2024 DeepMind Technologies Limited
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""Tests for the MuJoCo USD Exporter."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
import mujoco
|
||||
|
||||
from absl.testing import absltest
|
||||
from etils import epath
|
||||
|
||||
# Open3D and USD are not fully supported on all MuJoCo architectures.
|
||||
# pylint: disable=python.style(g-import-not-at-top)
|
||||
execute_test = True
|
||||
try:
|
||||
from mujoco.usd import exporter as exporter_module
|
||||
from pxr import Usd
|
||||
except ImportError:
|
||||
logging.warning('Skipping test due to missing import')
|
||||
execute_test = False
|
||||
# pylint: enable=python.style(g-import-not-at-top)
|
||||
|
||||
class ExporterTest(absltest.TestCase):
|
||||
|
||||
def test_usd_export(self):
|
||||
if not execute_test:
|
||||
return
|
||||
|
||||
output_dir = os.getenv('TEST_UNDECLARED_OUTPUTS_DIR')
|
||||
xml = """
|
||||
<mujoco>
|
||||
<worldbody>
|
||||
<camera name="closeup" pos="0 -6 0" xyaxes="1 0 0 0 1 100"/>
|
||||
<geom name="white_box" type="box" size="1 1 1" rgba="1 1 1 1"/>
|
||||
</worldbody>
|
||||
</mujoco>
|
||||
"""
|
||||
model = mujoco.MjModel.from_xml_string(xml)
|
||||
data = mujoco.MjData(model)
|
||||
exporter = exporter_module.USDExporter(
|
||||
model,
|
||||
output_directory_name="mujoco_usdpkg",
|
||||
output_directory_root=output_dir,
|
||||
)
|
||||
exporter.update_scene(data)
|
||||
exporter.save_scene("export.usda")
|
||||
|
||||
with open(os.path.join(
|
||||
output_dir, "mujoco_usdpkg/frames", "frame_1_.export.usda"), "r") as f:
|
||||
golden_path = os.path.join(
|
||||
epath.resource_path("mujoco"), "testdata", "usd_golden.usda")
|
||||
with open(golden_path, "r") as golden_file:
|
||||
self.assertEqual(f.read(), golden_file.read())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
absltest.main()
|
||||
Reference in New Issue
Block a user