Replace name attribute with setters and getters in the C API of mjSpec.

PiperOrigin-RevId: 778816015
Change-Id: Ieffb7a5bce37e887ff009f9a02d2434435e79ca1
This commit is contained in:
Alessio Quaglino
2025-07-03 02:29:49 -07:00
committed by Copybara-Service
parent 1c5d47c94b
commit 594e17074a
22 changed files with 290 additions and 400 deletions
@@ -334,14 +334,14 @@ class ModelWriter {
}
void WriteMesh(const mjsMesh *mesh, const pxr::SdfPath &parent_path) {
auto name = GetAvailablePrimName(*mesh->name, pxr::UsdGeomTokens->Mesh,
auto name = GetAvailablePrimName(*mjs_getName(mesh->element), pxr::UsdGeomTokens->Mesh,
parent_path);
pxr::SdfPath subcomponent_path =
CreatePrimSpec(data_, parent_path, name, pxr::UsdGeomTokens->Xform);
pxr::SdfPath mesh_path =
CreatePrimSpec(data_, subcomponent_path, kTokens->sourceMesh,
pxr::UsdGeomTokens->Mesh);
mesh_paths_[*mesh->name] = subcomponent_path;
mesh_paths_[*mjs_getName(mesh->element)] = subcomponent_path;
if (write_physics_) {
ApplyApiSchema(data_, mesh_path, MjcPhysicsTokens->MeshCollisionAPI);
@@ -725,8 +725,9 @@ class ModelWriter {
void WriteMaterial(mjsMaterial *material, const pxr::SdfPath &parent_path) {
// Create a Material prim.
auto name = GetAvailablePrimName(
*material->name, pxr::UsdShadeTokens->Material, parent_path);
auto name =
GetAvailablePrimName(*mjs_getName(material->element),
pxr::UsdShadeTokens->Material, parent_path);
pxr::SdfPath material_path =
CreatePrimSpec(data_, parent_path, name, pxr::UsdShadeTokens->Material);
@@ -1008,9 +1009,9 @@ class ModelWriter {
std::unordered_map<std::string, std::vector<mjsKey *>> keyframes_map;
mjsKey *keyframe = mjs_asKey(mjs_firstElement(spec_, mjOBJ_KEY));
while (keyframe) {
std::string keyframe_name = keyframe->name->empty()
std::string keyframe_name = mjs_getName(keyframe->element)->empty()
? MjcPhysicsTokens->Keyframe
: *keyframe->name;
: *mjs_getName(keyframe->element);
keyframes_map[keyframe_name].push_back(keyframe);
keyframe = mjs_asKey(mjs_nextElement(spec_, keyframe->element));
}
@@ -1174,7 +1175,9 @@ class ModelWriter {
pxr::SdfPath WriteMeshGeom(const mjsGeom *geom,
const pxr::SdfPath &body_path) {
std::string mj_name = geom->name->empty() ? *geom->meshname : *geom->name;
std::string mj_name = mjs_getName(geom->element)->empty()
? *geom->meshname
: *mjs_getName(geom->element);
auto name =
GetAvailablePrimName(mj_name, pxr::UsdGeomTokens->Mesh, body_path);
pxr::SdfPath subcomponent_path =
@@ -1196,8 +1199,8 @@ class ModelWriter {
pxr::SdfPath WriteSiteGeom(const mjsSite *site,
const pxr::SdfPath &body_path) {
auto name =
GetAvailablePrimName(*site->name, pxr::UsdGeomTokens->Cube, body_path);
auto name = GetAvailablePrimName(*mjs_getName(site->element),
pxr::UsdGeomTokens->Cube, body_path);
int site_idx = mjs_getId(site->element);
const mjtNum *size = &model_->site_size[site_idx * 3];
@@ -1254,8 +1257,8 @@ class ModelWriter {
pxr::SdfPath WriteBoxGeom(const mjsGeom *geom,
const pxr::SdfPath &body_path) {
auto name =
GetAvailablePrimName(*geom->name, pxr::UsdGeomTokens->Cube, body_path);
auto name = GetAvailablePrimName(*mjs_getName(geom->element),
pxr::UsdGeomTokens->Cube, body_path);
int geom_idx = mjs_getId(geom->element);
mjtNum *geom_size = &model_->geom_size[geom_idx * 3];
@@ -1282,8 +1285,8 @@ class ModelWriter {
pxr::SdfPath WriteCapsuleGeom(const mjsGeom *geom,
const pxr::SdfPath &body_path) {
auto name = GetAvailablePrimName(*geom->name, pxr::UsdGeomTokens->Capsule,
body_path);
auto name = GetAvailablePrimName(*mjs_getName(geom->element),
pxr::UsdGeomTokens->Capsule, body_path);
int geom_idx = mjs_getId(geom->element);
mjtNum *geom_size = &model_->geom_size[geom_idx * 3];
@@ -1310,8 +1313,8 @@ class ModelWriter {
pxr::SdfPath WriteCylinderGeom(const mjsGeom *geom,
const pxr::SdfPath &body_path) {
auto name = GetAvailablePrimName(*geom->name, pxr::UsdGeomTokens->Cylinder,
body_path);
auto name = GetAvailablePrimName(*mjs_getName(geom->element),
pxr::UsdGeomTokens->Cylinder, body_path);
int geom_idx = mjs_getId(geom->element);
mjtNum *geom_size = &model_->geom_size[geom_idx * 3];
@@ -1340,8 +1343,8 @@ class ModelWriter {
pxr::SdfPath WriteEllipsoidGeom(const mjsGeom *geom,
const pxr::SdfPath &body_path) {
auto name = GetAvailablePrimName(*geom->name, pxr::UsdGeomTokens->Sphere,
body_path);
auto name = GetAvailablePrimName(*mjs_getName(geom->element),
pxr::UsdGeomTokens->Sphere, body_path);
int geom_idx = mjs_getId(geom->element);
mjtNum *geom_size = &model_->geom_size[geom_idx * 3];
@@ -1362,8 +1365,8 @@ class ModelWriter {
pxr::SdfPath WriteSphereGeom(const mjsGeom *geom,
const pxr::SdfPath &body_path) {
auto name = GetAvailablePrimName(*geom->name, pxr::UsdGeomTokens->Sphere,
body_path);
auto name = GetAvailablePrimName(*mjs_getName(geom->element),
pxr::UsdGeomTokens->Sphere, body_path);
int geom_idx = mjs_getId(geom->element);
mjtNum *geom_size = &model_->geom_size[geom_idx * 3];
return WriteSphere(name, geom_size, body_path);
@@ -1399,8 +1402,8 @@ class ModelWriter {
pxr::SdfPath WritePlaneGeom(const mjsGeom *geom,
const pxr::SdfPath &body_path) {
auto name =
GetAvailablePrimName(*geom->name, pxr::UsdGeomTokens->Plane, body_path);
auto name = GetAvailablePrimName(*mjs_getName(geom->element),
pxr::UsdGeomTokens->Plane, body_path);
int geom_idx = mjs_getId(geom->element);
mjtNum *geom_size = &model_->geom_size[geom_idx * 3];
return WritePlane(name, geom_size, body_path);
@@ -1409,8 +1412,8 @@ class ModelWriter {
void WriteSite(mjsSite *site, const mjsBody *body) {
const int body_id = mjs_getId(body->element);
const auto &body_path = body_paths_[body_id];
auto name =
GetAvailablePrimName(*site->name, pxr::UsdGeomTokens->Xform, body_path);
auto name = GetAvailablePrimName(*mjs_getName(site->element),
pxr::UsdGeomTokens->Xform, body_path);
// Create a geom primitive and set its purpose to guide so it won't be
// rendered.
@@ -1519,7 +1522,8 @@ class ModelWriter {
}
mjsDefault *spec_default = mjs_getDefault(geom->element);
pxr::TfToken valid_class_name = GetValidPrimName(*spec_default->name);
pxr::TfToken valid_class_name =
GetValidPrimName(*mjs_getName(spec_default->element));
pxr::SdfPath geom_class_path = class_path_.AppendChild(valid_class_name);
if (!data_->HasSpec(geom_class_path)) {
pxr::SdfPath class_path =
@@ -1645,7 +1649,7 @@ class ModelWriter {
break;
default:
TF_WARN("Unsupported joint type '%d' for joint '%s'. Skipping.",
(int)type, joint->name->c_str());
(int)type, mjs_getName(joint->element)->c_str());
return;
}
}
@@ -1658,7 +1662,7 @@ class ModelWriter {
int body0_id_usd = model_->body_parentid[body_id];
const pxr::SdfPath &body1_path_usd = body_paths_[body1_id_usd];
auto joint_name = joint ? *joint->name : "FixedJoint";
auto joint_name = joint ? *mjs_getName(joint->element) : "FixedJoint";
pxr::TfToken joint_name_token =
GetAvailablePrimName(joint_name, kTokens->joint, body1_path_usd);
pxr::SdfPath joint_path = CreatePrimSpec(data_, body1_path_usd,
@@ -1892,7 +1896,7 @@ class ModelWriter {
void WriteCamera(mjsCamera *spec_cam, const mjsBody *body) {
const auto &body_path = body_paths_[mjs_getId(body->element)];
auto name = GetAvailablePrimName(*spec_cam->name,
auto name = GetAvailablePrimName(*mjs_getName(spec_cam->element),
pxr::UsdGeomTokens->Camera, body_path);
// Create a root Xform for the world body with the model name if it exists
// otherwise called 'World'.
@@ -1950,7 +1954,8 @@ class ModelWriter {
void WriteLight(mjsLight *light, const mjsBody *body) {
const auto &body_path = body_paths_[mjs_getId(body->element)];
auto name = GetAvailablePrimName(*light->name, kTokens->light, body_path);
auto name = GetAvailablePrimName(*mjs_getName(light->element),
kTokens->light, body_path);
// Create a root Xform for the world body with the model name if it exists
// otherwise called 'World'.
pxr::SdfPath light_path =
@@ -1978,7 +1983,7 @@ class ModelWriter {
mjsBody *parent = mjs_getParent(body->element);
int parent_id = mjs_getId(parent->element);
pxr::SdfPath parent_path = body_paths_[parent_id];
pxr::TfToken body_name = GetValidPrimName(*body->name);
pxr::TfToken body_name = GetValidPrimName(*mjs_getName(body->element));
// Create Xform prim for body.
pxr::SdfPath body_path = CreatePrimSpec(data_, parent_path, body_name,
@@ -2046,7 +2051,8 @@ class ModelWriter {
// Create classes if necessary
mjsDefault *spec_default = mjs_getDefault(body->element);
pxr::TfToken body_class_name = GetValidPrimName(*spec_default->name);
pxr::TfToken body_class_name =
GetValidPrimName(*mjs_getName(spec_default->element));
pxr::SdfPath body_class_path = class_path_.AppendChild(body_class_name);
if (!data_->HasSpec(body_class_path)) {
CreateClassSpec(data_, class_path_, body_class_name);
@@ -2069,7 +2075,7 @@ class ModelWriter {
pxr::VtArray<pxr::TfToken>{kTokens->xformOpTransform});
pxr::VtDictionary customData;
customData[kTokens->body_name] = *body->name;
customData[kTokens->body_name] = *mjs_getName(body->element);
SetPrimMetadata(data_, body_path, pxr::SdfFieldKeys->CustomData,
customData);
+20 -16
View File
@@ -556,7 +556,7 @@ void ParseMjcPhysicsGeneralActuatorAPI(mjSpec* spec,
const std::string* name) {
pxr::UsdPrim prim = act.GetPrim();
mjsActuator* mj_act = mjs_addActuator(spec, nullptr);
mjs_setString(mj_act->name, prim.GetPath().GetAsString().c_str());
mjs_setName(mj_act->element, prim.GetPath().GetAsString().c_str());
mjs_setString(mj_act->target, name->c_str());
mj_act->trntype = tran_type;
@@ -732,7 +732,7 @@ void ParseMjcPhysicsJointAPI(mjsJoint* mj_joint,
mju_warning(
"springdamper attribute for joint %s has incorrect size %zu, "
"expected 2.",
mj_joint->name->c_str(), springdamper.size());
mjs_getName(mj_joint->element)->c_str(), springdamper.size());
}
}
@@ -748,7 +748,7 @@ void ParseMjcPhysicsJointAPI(mjsJoint* mj_joint,
mju_warning(
"solreflimit attribute for joint %s has incorrect size %zu, "
"expected %d.",
mj_joint->name->c_str(), solreflimit.size(), mjNREF);
mjs_getName(mj_joint->element)->c_str(), solreflimit.size(), mjNREF);
}
}
@@ -764,7 +764,7 @@ void ParseMjcPhysicsJointAPI(mjsJoint* mj_joint,
mju_warning(
"solimplimit attribute for joint %s has incorrect size %zu, "
"expected %d.",
mj_joint->name->c_str(), solimplimit.size(), mjNIMP);
mjs_getName(mj_joint->element)->c_str(), solimplimit.size(), mjNIMP);
}
}
@@ -780,7 +780,8 @@ void ParseMjcPhysicsJointAPI(mjsJoint* mj_joint,
mju_warning(
"solreffriction attribute for joint %s has incorrect size %zu, "
"expected %d.",
mj_joint->name->c_str(), solreffriction.size(), mjNREF);
mjs_getName(mj_joint->element)->c_str(), solreffriction.size(),
mjNREF);
}
}
@@ -796,7 +797,8 @@ void ParseMjcPhysicsJointAPI(mjsJoint* mj_joint,
mju_warning(
"solimpfriction attribute for joint %s has incorrect size %zu, "
"expected %d.",
mj_joint->name->c_str(), solimpfriction.size(), mjNIMP);
mjs_getName(mj_joint->element)->c_str(), solimpfriction.size(),
mjNIMP);
}
}
@@ -898,7 +900,7 @@ void ParseUsdPhysicsCollider(mjSpec* spec,
}
mjsGeom* geom = mjs_addGeom(parent, nullptr);
mjs_setString(geom->name, prim.GetPath().GetAsString().c_str());
mjs_setName(geom->element, prim.GetPath().GetAsString().c_str());
geom->contype = 1;
geom->conaffinity = 1;
@@ -983,7 +985,7 @@ void ParseUsdPhysicsCollider(mjSpec* spec,
}
std::string mesh_name = usd_mesh.GetPath().GetAsString();
mjs_setString(mesh->name, mesh_name.c_str());
mjs_setName(mesh->element, mesh_name.c_str());
mjs_setFloat(mesh->uservert, uservert.data(), uservert.size());
mjs_setInt(mesh->userface, userface.data(), userface.size());
@@ -1047,7 +1049,7 @@ void ParseUsdPhysicsJoint(mjSpec* spec, const pxr::UsdPrim& prim, mjsBody* body,
mjsJoint* mj_joint = mjs_addJoint(body, nullptr);
mj_joint->type = type;
mjs_setString(mj_joint->name, prim.GetPath().GetAsString().c_str());
mjs_setName(mj_joint->element, prim.GetPath().GetAsString().c_str());
if (prim.IsA<pxr::UsdPhysicsRevoluteJoint>()) {
pxr::UsdPhysicsRevoluteJoint revolute(prim);
@@ -1123,7 +1125,8 @@ void ParseUsdPhysicsJoint(mjSpec* spec, const pxr::UsdPrim& prim, mjsBody* body,
if (prim.HasAPI<pxr::MjcPhysicsActuatorAPI>()) {
ParseMjcPhysicsGeneralActuatorAPI(spec, pxr::MjcPhysicsActuatorAPI(prim),
mjtTrn::mjTRN_JOINT, mj_joint->name);
mjtTrn::mjTRN_JOINT,
mjs_getName(mj_joint->element));
}
if (prim.HasAPI<pxr::MjcPhysicsJointAPI>()) {
@@ -1136,7 +1139,7 @@ void ParseMjcPhysicsSite(mjSpec* spec, const pxr::MjcPhysicsSiteAPI& site_api,
pxr::UsdGeomXformCache& xform_cache) {
auto prim = site_api.GetPrim();
mjsSite* site = mjs_addSite(parent, 0);
mjs_setString(site->name, site_api.GetPrim().GetPath().GetAsString().c_str());
mjs_setName(site->element, site_api.GetPrim().GetPath().GetAsString().c_str());
SetLocalPoseFromPrim(site_api.GetPrim(), parent_prim, site, xform_cache);
// Convert USD type to MuJoCo type.
@@ -1152,7 +1155,7 @@ void ParseMjcPhysicsSite(mjSpec* spec, const pxr::MjcPhysicsSiteAPI& site_api,
ParseMjcPhysicsGeneralActuatorAPI(
spec, pxr::MjcPhysicsActuatorAPI(prim),
slider_crank ? mjtTrn::mjTRN_SLIDERCRANK : mjtTrn::mjTRN_SITE,
site->name);
mjs_getName(site->element));
}
}
@@ -1187,7 +1190,7 @@ void ParseMjcPhysicsKeyframe(mjSpec* spec,
if (n_time_samples == 0) {
// If no time samples, we create a single keyframe.
mjsKey* key = mjs_addKey(spec);
mjs_setString(key->name, prim.GetName().GetString().c_str());
mjs_setName(key->element, prim.GetName().GetString().c_str());
setKeyframeData(key, qpos_attr, &key->qpos);
setKeyframeData(key, qvel_attr, &key->qvel);
setKeyframeData(key, act_attr, &key->act);
@@ -1203,7 +1206,7 @@ void ParseMjcPhysicsKeyframe(mjSpec* spec,
mjsKey* key = mjs_addKey(spec);
std::string key_name =
prim.GetName().GetString() + "_" + std::to_string(keyframe_id++);
mjs_setString(key->name, key_name.c_str());
mjs_setName(key->element, key_name.c_str());
key->time = time;
setKeyframeData(key, qpos_attr, &key->qpos, &time);
setKeyframeData(key, qvel_attr, &key->qvel, &time);
@@ -1221,7 +1224,7 @@ mjsBody* ParseUsdPhysicsRigidbody(
pxr::UsdGeomXformCache& xform_cache) {
pxr::UsdPrim prim = rigidbody_api.GetPrim();
mjsBody* body = mjs_addBody(parent, nullptr);
mjs_setString(body->name, prim.GetPath().GetAsString().c_str());
mjs_setName(body->element, prim.GetPath().GetAsString().c_str());
SetLocalPoseFromPrim(prim, parent_prim, body, xform_cache);
if (prim.HasAPI<pxr::UsdPhysicsMassAPI>()) {
@@ -1230,7 +1233,8 @@ mjsBody* ParseUsdPhysicsRigidbody(
if (prim.HasAPI<pxr::MjcPhysicsActuatorAPI>()) {
ParseMjcPhysicsGeneralActuatorAPI(spec, pxr::MjcPhysicsActuatorAPI(prim),
mjtTrn::mjTRN_BODY, body->name);
mjtTrn::mjTRN_BODY,
mjs_getName(body->element));
}
mujoco::usd::SetUsdPrimPathUserValue(body->element, prim.GetPath());