merging with main mujoco repo

This commit is contained in:
Abhishek Joshi
2024-04-02 16:39:04 -04:00
136 changed files with 48702 additions and 2845 deletions
+2 -2
View File
@@ -234,10 +234,10 @@
"$$\n",
"\\begin{aligned}\n",
"x^* &= \\arg \\min_x f(x)\\\\\n",
"\\textrm{s.t.} &\\quad l \\succcurlyeq x \\succcurlyeq u\n",
"\\textrm{s.t.} &\\quad l \\preccurlyeq x \\preccurlyeq u\n",
"\\end{aligned}\n",
"$$\n",
"Where $l$ and $u$ are respectively lower and upper bound vectors and the inequalities $l \\succcurlyeq x \\succcurlyeq u$ are read elementwise. Solving for the minimizing $\\delta x$ is now a bit more involved than solving a linear system, and the optimization method is now referred to as [Sequential Quadratic Programming](https://en.wikipedia.org/wiki/Sequential_quadratic_programming) (SQP). Fortunately MuJoCo has an efficient box-constrained QP solver: the [mju_boxQP](https://mujoco.readthedocs.io/en/latest/APIreference/APIfunctions.html#mju-boxqp) function."
"Where $l$ and $u$ are respectively lower and upper bound vectors and the inequalities $l \\preccurlyeq x \\preccurlyeq u$ are read elementwise. Solving for the minimizing $\\delta x$ is now a bit more involved than solving a linear system, and the optimization method is now referred to as [Sequential Quadratic Programming](https://en.wikipedia.org/wiki/Sequential_quadratic_programming) (SQP). Fortunately MuJoCo has an efficient box-constrained QP solver: the [mju_boxQP](https://mujoco.readthedocs.io/en/latest/APIreference/APIfunctions.html#mju-boxqp) function."
]
},
{
+4 -4
View File
@@ -623,7 +623,7 @@ class MuJoCoBindingsTest(parameterized.TestCase):
self.assertEqual(struct, struct2)
self.assertNotEqual(struct, 3)
self.assertNotEqual(struct, None)
self.assertIsNotNone(struct)
# mutable structs shouldn't declare __hash__
with self.assertRaises(TypeError):
@@ -705,7 +705,8 @@ class MuJoCoBindingsTest(parameterized.TestCase):
# Check that the output argument must have the correct dtype.
with self.assertRaises(TypeError):
mujoco.mju_rotVecQuat(vec, quat, res=np.zeros(3, int))
res = np.zeros(3, np.int32)
mujoco.mju_rotVecQuat(res, vec, quat)
def test_getsetstate(self): # pylint: disable=invalid-name
mujoco.mj_step(self.model, self.data)
@@ -850,8 +851,7 @@ Euler integrator, semi-implicit in velocity.
self.assertEqual(mujoco.mjtEnableBit.mjENBL_OVERRIDE, 1<<0)
self.assertEqual(mujoco.mjtEnableBit.mjENBL_ENERGY, 1<<1)
self.assertEqual(mujoco.mjtEnableBit.mjENBL_FWDINV, 1<<2)
self.assertEqual(mujoco.mjtEnableBit.mjENBL_SENSORNOISE, 1<<4)
self.assertEqual(mujoco.mjtEnableBit.mjNENABLE, 7)
self.assertEqual(mujoco.mjtEnableBit.mjNENABLE, 6)
self.assertEqual(mujoco.mjtGeom.mjGEOM_PLANE, 0)
self.assertEqual(mujoco.mjtGeom.mjGEOM_HFIELD, 1)
self.assertEqual(mujoco.mjtGeom.mjGEOM_SPHERE, 2)
+71
View File
@@ -0,0 +1,71 @@
#usda 1.0
(
defaultPrim = "World"
doc = """Generated from Composed Stage of root layer
"""
endTimeCode = 1
startTimeCode = 0
timeCodesPerSecond = 24
upAxis = "Z"
)
def Xform "World"
{
def Xform "Light_Xform_0"
{
double3 xformOp:translate
uniform token[] xformOpOrder = ["xformOp:translate"]
def SphereLight "Light_0"
{
bool inputs:normalize = 1
float inputs:radius = 0.3
bool treatAsPoint = 0
}
}
def Xform "CubeMesh_Xform_white_box"
{
matrix4d xformOp:transform.timeSamples = {
0: ( (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 1) ),
}
uniform token[] xformOpOrder = ["xformOp:transform"]
def Mesh "CubeMesh_white_box" (
apiSchemas = ["MaterialBindingAPI"]
)
{
int[] faceVertexCounts = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
int[] faceVertexIndices = [4, 7, 5, 4, 6, 7, 0, 2, 4, 2, 6, 4, 0, 1, 2, 1, 3, 2, 1, 5, 7, 1, 7, 3, 2, 3, 7, 2, 7, 6, 0, 4, 1, 1, 4, 5]
rel material:binding = </World/_materials/Material_white_box>
point3f[] points = [(-1, -1, -1), (1, -1, -1), (-1, -1, 1), (1, -1, 1), (-1, 1, -1), (1, 1, -1), (-1, 1, 1), (1, 1, 1)]
texCoord2f[] primvars:UVMap = [] (
interpolation = "faceVarying"
)
int[] primvars:UVMap:indices = [0, 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]
uniform token subdivisionScheme = "none"
token visibility.timeSamples = {
0: "inherited",
}
}
}
def "_materials"
{
def Material "Material_white_box"
{
token outputs:surface.connect = </World/_materials/Material_white_box/Principled_BSDF.outputs:surface>
def Shader "Principled_BSDF"
{
uniform token info:id = "UsdPreviewSurface"
color3f inputs:diffuseColor = (1, 1, 1)
float inputs:metallic = 0.5
float inputs:opacity = 1
float inputs:roughness = 0.5
token outputs:surface
}
}
}
}
+14 -126
View File
@@ -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()))
+1 -1
View File
@@ -14,6 +14,6 @@ if __name__ == "__main__":
exp.update_scene(d)
exp.save_scene(filetype="usd")
exp.save_scene(filetype="usda")
+22 -16
View File
@@ -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)
+70
View File
@@ -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()
View File
+7 -1
View File
@@ -169,7 +169,10 @@ def _file_loader(path: str) -> _LoaderWithPathType:
"""Loads an MJCF model from file path."""
def load(path=path) -> Tuple[mujoco.MjModel, mujoco.MjData, str]:
m = mujoco.MjModel.from_xml_path(path)
if len(path) >= 4 and path[-4:] == '.mjb':
m = mujoco.MjModel.from_binary_path(path)
else:
m = mujoco.MjModel.from_xml_path(path)
d = mujoco.MjData(m)
return m, d, path
@@ -199,6 +202,9 @@ def _reload(
path = load_tuple[2] if len(load_tuple) == 3 else ''
simulate.load(m, d, path)
# Make sure any load_error message is cleared
simulate.load_error = ''
if notify_loaded:
notify_loaded()