Skip to content

Commit 09bc73e

Browse files
authored
Merge pull request #2 from awesome-aj0123/demo_script
Demo script
2 parents d7f131e + a4b5355 commit 09bc73e

File tree

6 files changed

+29
-123
lines changed

6 files changed

+29
-123
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*.egg-info/
1919
build/
2020
build_cmake/
21+
python/dist/
2122

2223
# Exclude macOS folder attributes
2324
.DS_Store
@@ -28,4 +29,4 @@ Info.framework.plist
2829
# Python byte-compiled / optimized / DLL files
2930
__pycache__/
3031
*.py[cod]
31-
*$py.class
32+
*$py.class

python/mujoco/usd/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .exporter import *
2+
from .component import *
3+
from .utils import *

python/mujoco/usd/component.py

+3-120
Original file line numberDiff line numberDiff line change
@@ -378,123 +378,6 @@ def update_visibility(self, visible: bool, frame: int):
378378
self.usd_prim.GetAttribute("visibility").Set("invisible", frame)
379379

380380

381-
class USDPrimitive:
382-
383-
def __init__(
384-
self,
385-
stage: Usd.Stage,
386-
geom: mujoco.MjvGeom,
387-
obj_name: str,
388-
rgba: np.ndarray = np.array([1, 1, 1, 1]),
389-
texture_file: Optional[str] = None,
390-
):
391-
self.stage = stage
392-
self.geom = geom
393-
self.obj_name = obj_name
394-
self.rgba = rgba
395-
self.texture_file = texture_file
396-
397-
self.usd_prim = Usd.Prim()
398-
self.usd_primitive_shape = Usd.PrimitiveShape()
399-
self.transform_op = Usd.TransformOp()
400-
401-
def _set_refinement_properties(self):
402-
self.usd_prim.GetAttribute("subdivisionScheme").Set("none")
403-
404-
def _attach_material(self):
405-
mtl_path = Sdf.Path(f"/World/_materials/Material_{self.obj_name}")
406-
mtl = UsdShade.Material.Define(self.stage, mtl_path)
407-
if self.texture_file:
408-
bsdf_shader = UsdShade.Shader.Define(
409-
self.stage, mtl_path.AppendPath("Principled_BSDF")
410-
)
411-
image_shader = UsdShade.Shader.Define(
412-
self.stage, mtl_path.AppendPath("Image_Texture")
413-
)
414-
uvmap_shader = UsdShade.Shader.Define(
415-
self.stage, mtl_path.AppendPath("uvmap")
416-
)
417-
418-
# settings the bsdf shader attributes
419-
bsdf_shader.CreateIdAttr("UsdPreviewSurface")
420-
bsdf_shader.CreateInput(
421-
"diffuseColor", Sdf.ValueTypeNames.Color3f
422-
).ConnectToSource(image_shader.ConnectableAPI(), "rgb")
423-
bsdf_shader.CreateInput("opacity", Sdf.ValueTypeNames.Float).Set(
424-
float(self.rgba[-1])
425-
)
426-
bsdf_shader.CreateInput("metallic", Sdf.ValueTypeNames.Float).Set(
427-
self.geom.shininess
428-
)
429-
bsdf_shader.CreateInput("roughness", Sdf.ValueTypeNames.Float).Set(
430-
1.0 - self.geom.shininess
431-
)
432-
433-
mtl.CreateSurfaceOutput().ConnectToSource(
434-
bsdf_shader.ConnectableAPI(), "surface"
435-
)
436-
437-
self.usd_primitive_shape.GetPrim().ApplyAPI(UsdShade.MaterialBindingAPI)
438-
UsdShade.MaterialBindingAPI(self.usd_primitive_shape).Bind(mtl)
439-
440-
# setting the image texture attributes
441-
image_shader.CreateIdAttr("UsdUVTexture")
442-
image_shader.CreateInput("file", Sdf.ValueTypeNames.Asset).Set(
443-
self.texture_file
444-
)
445-
image_shader.CreateInput(
446-
"sourceColorSpace", Sdf.ValueTypeNames.Token
447-
).Set("sRGB")
448-
image_shader.CreateInput("st", Sdf.ValueTypeNames.Float2).ConnectToSource(
449-
uvmap_shader.ConnectableAPI(), "result"
450-
)
451-
image_shader.CreateOutput("rgb", Sdf.ValueTypeNames.Float3)
452-
453-
# setting uvmap shader attributes
454-
uvmap_shader.CreateIdAttr("UsdPrimvarReader_float2")
455-
uvmap_shader.CreateInput("varname", Sdf.ValueTypeNames.Token).Set("UVMap")
456-
uvmap_shader.CreateOutput("results", Sdf.ValueTypeNames.Float2)
457-
else:
458-
bsdf_shader = UsdShade.Shader.Define(
459-
self.stage, mtl_path.AppendPath("Principled_BSDF")
460-
)
461-
462-
# settings the bsdf shader attributes
463-
bsdf_shader.CreateIdAttr("UsdPreviewSurface")
464-
bsdf_shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).Set(
465-
tuple(self.rgba[:3])
466-
)
467-
bsdf_shader.CreateInput("opacity", Sdf.ValueTypeNames.Float).Set(
468-
float(self.rgba[-1])
469-
)
470-
bsdf_shader.CreateInput("metallic", Sdf.ValueTypeNames.Float).Set(
471-
self.geom.shininess
472-
)
473-
bsdf_shader.CreateInput("roughness", Sdf.ValueTypeNames.Float).Set(
474-
1.0 - self.geom.shininess
475-
)
476-
477-
mtl.CreateSurfaceOutput().ConnectToSource(
478-
bsdf_shader.ConnectableAPI(), "surface"
479-
)
480-
481-
self.usd_primitive_shape.GetPrim().ApplyAPI(UsdShade.MaterialBindingAPI)
482-
UsdShade.MaterialBindingAPI(self.usd_primitive_shape).Bind(mtl)
483-
484-
def update(self, pos: np.ndarray, mat: np.ndarray, visible: bool, frame: int):
485-
transformation_mat = mujoco.usd_util.create_transform_matrix(
486-
rotation_matrix=mat, translation_vector=pos
487-
).T
488-
self.transform_op.Set(Gf.Matrix4d(transformation_mat.tolist()), frame)
489-
self.update_visibility(visible, frame)
490-
491-
def update_visibility(self, visible: bool, frame: int):
492-
if visible:
493-
self.usd_prim.GetAttribute("visibility").Set("inherited", frame)
494-
else:
495-
self.usd_prim.GetAttribute("visibility").Set("invisible", frame)
496-
497-
498381
class USDCapsule(USDPrimitive):
499382

500383
def __init__(
@@ -522,7 +405,7 @@ def __init__(
522405
self._set_size_attributes()
523406
self._attach_material()
524407

525-
self._set_refinement_properties()
408+
# self._set_refinement_properties()
526409

527410
def _set_size_attributes(self):
528411
self.usd_primitive_shape.GetRadiusAttr().Set(float(self.geom.size[0]))
@@ -558,7 +441,7 @@ def __init__(
558441
self._set_size_attributes()
559442
self._attach_material()
560443

561-
self._set_refinement_properties()
444+
# self._set_refinement_properties()
562445

563446
def _set_size_attributes(self):
564447
self.scale_op.Set(Gf.Vec3d(self.geom.size.tolist()))
@@ -849,7 +732,7 @@ def __init__(self, stage: Usd.Stage, obj_name: str):
849732

850733
def update(self, cam_pos: np.ndarray, cam_mat: np.ndarray, frame: int):
851734

852-
transformation_mat = mujoco.usd_util.create_transform_matrix(
735+
transformation_mat = mujoco.usd.utils.create_transform_matrix(
853736
rotation_matrix=cam_mat, translation_vector=cam_pos
854737
).T
855738
self.transform_op.Set(Gf.Matrix4d(transformation_mat.tolist()), frame)

python/mujoco/usd/demo.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import mujoco
2+
from mujoco.usd import exporter
3+
4+
if __name__ == "__main__":
5+
6+
# load a model to mujoco
7+
m = mujoco.MjModel.from_xml_path("/Users/abhishek/Documents/research/mujoco/model/humanoid/humanoid.xml")
8+
d = mujoco.MjData(m)
9+
10+
# create an instance of the USDExporter
11+
exp = exporter.USDExporter(model=m)
12+
13+
mujoco.mj_step(m, d)
14+
15+
exp.update_scene(d)
16+
17+
exp.save_scene(filetype="usda")
18+
19+

python/mujoco/usd/exporter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def add_light(
395395
intensity: int,
396396
radius: Optional[float] = 1.0,
397397
color: Optional[np.ndarray] = np.array([0.3, 0.3, 0.3]),
398-
objid: Optional[int] = 1,
398+
obj_name: Optional[str] = "light_1",
399399
light_type: Optional[str] = "sphere",
400400
):
401401

@@ -413,7 +413,7 @@ def add_camera(
413413
self,
414414
pos: List[float],
415415
rotation_xyz: List[float],
416-
objid: Optional[int] = 1,
416+
obj_name: Optional[str] = "camera_1",
417417
):
418418
new_camera = component_module.USDCamera(
419419
stage=self.stage, obj_name=str(objid))

python/mujoco/usd/shapes.py

Whitespace-only changes.

0 commit comments

Comments
 (0)