functional-grasp-demos / genesis_compat.py
yianW's picture
Initial upload: 7 grasp demos with replay code and assets
285ba65 verified
"""
Genesis version compatibility layer.
Provides consistent API across Genesis 0.3.x and 0.4.x:
- detect_collision_parallel: batch collision detection across all envs
- Mesh morph: handles parse_glb_with_zup vs file_meshes_are_zup
"""
import numpy as np
import genesis as gs
_GENESIS_VERSION = tuple(int(x) for x in gs.__version__.split(".")[:3])
def _detect_collision_parallel_impl(
entity,
with_entity=None,
exclude_self_contact=False,
envs_idx=None,
unsafe=False,
):
"""
Batch collision detection across all parallel envs.
Ported from Genesis 0.3.x RigidEntity.detect_collision_parallel().
Works with Genesis 0.4.x by accessing the same solver internals.
Returns:
numpy.ndarray: bool array of shape (n_envs,) indicating collision.
"""
if not unsafe:
entity.solver.collider.clear()
entity.solver.collider.detection()
# Pull current contact buffers
n_collision = entity.solver.collider._collider_state.n_contacts.to_numpy()
geom_A = entity.solver.collider._collider_state.contact_data.geom_a.to_numpy()
geom_B = entity.solver.collider._collider_state.contact_data.geom_b.to_numpy()
n_collision = np.asarray(n_collision).reshape(-1) # [B]
geom_A = np.asarray(geom_A) # [max_contacts, B]
geom_B = np.asarray(geom_B) # [max_contacts, B]
max_contacts, B = geom_A.shape
if envs_idx is None:
envs_idx = np.arange(B, dtype=int)
else:
envs_idx = np.asarray(envs_idx, dtype=int)
valid_rows = np.arange(max_contacts)[:, None] < n_collision[None, :]
selfA = (geom_A >= entity.geom_start) & (geom_A < entity.geom_end)
selfB = (geom_B >= entity.geom_start) & (geom_B < entity.geom_end)
if with_entity is not None:
otherA = (geom_A >= with_entity.geom_start) & (geom_A < with_entity.geom_end)
otherB = (geom_B >= with_entity.geom_start) & (geom_B < with_entity.geom_end)
hits = ((selfA & otherB) | (selfB & otherA)) & valid_rows
else:
hits = (selfA | selfB) & valid_rows
if exclude_self_contact:
hits &= ~(selfA & selfB)
collided = np.any(hits, axis=0) # [B]
return collided[envs_idx]
def patch_entity_compat(entity, n_envs):
"""Add detect_collision_parallel() to entity if missing (Genesis 0.4+)."""
if hasattr(entity, "detect_collision_parallel"):
return # Already has it (Genesis 0.3.x)
import types
def detect_collision_parallel(
with_entity=None, exclude_self_contact=False, envs_idx=None, *, unsafe=False
):
return _detect_collision_parallel_impl(
entity, with_entity, exclude_self_contact, envs_idx, unsafe=unsafe
)
entity.detect_collision_parallel = detect_collision_parallel
def mesh_kwargs_compat(**kwargs):
"""Convert Mesh kwargs for Genesis version compatibility.
Genesis 0.3.x: GLBs are auto-handled (no flag needed).
Genesis 0.4.x: Need file_meshes_are_zup=True to match old behavior.
"""
if _GENESIS_VERSION >= (0, 4, 0):
# Convert parse_glb_with_zup -> file_meshes_are_zup
if "parse_glb_with_zup" in kwargs:
kwargs["file_meshes_are_zup"] = kwargs.pop("parse_glb_with_zup")
else:
# 0.3.x: remove both flags (not needed, can break things)
kwargs.pop("parse_glb_with_zup", None)
kwargs.pop("file_meshes_are_zup", None)
return kwargs
def create_mesh_morph(**kwargs):
"""Create gs.morphs.Mesh with version-compatible kwargs."""
return gs.morphs.Mesh(**mesh_kwargs_compat(**kwargs))