Update XML parsing to store asset path so that assets can be recovered and sent to external renderers

PiperOrigin-RevId: 567347908
Change-Id: I4d8e1abe35be3a67643ff925af709921280856f8
This commit is contained in:
Matthew Bennice
2023-09-21 10:27:04 -07:00
committed by Copybara-Service
parent 4f34cacf35
commit 7f8018a3f3
15 changed files with 132 additions and 15 deletions
+6 -4
View File
@@ -450,7 +450,7 @@ mjModel* mj_makeModel(int nq, int nv, int nu, int na, int nbody, int nbvh, int n
int ntuple, int ntupledata, int nkey, int nmocap, int nplugin,
int npluginattr, int nuser_body, int nuser_jnt, int nuser_geom,
int nuser_site, int nuser_cam, int nuser_tendon, int nuser_actuator,
int nuser_sensor, int nnames) {
int nuser_sensor, int nnames, int npaths) {
intptr_t offset = 0;
// allocate mjModel
@@ -519,6 +519,7 @@ mjModel* mj_makeModel(int nq, int nv, int nu, int na, int nbody, int nbvh, int n
+ nskin + nhfield + ntex + nmat + npair + nexclude + neq
+ ntendon + nu + nsensor + nnumeric + ntext + ntuple
+ nkey + nplugin);
m->npaths = npaths;
#define X(name) \
if ((m->name) < 0) { \
@@ -597,7 +598,7 @@ mjModel* mj_copyModel(mjModel* dest, const mjModel* src) {
src->ntuple, src->ntupledata, src->nkey, src->nmocap, src->nplugin,
src->npluginattr, src->nuser_body, src->nuser_jnt, src->nuser_geom,
src->nuser_site, src->nuser_cam, src->nuser_tendon, src->nuser_actuator,
src->nuser_sensor, src->nnames);
src->nuser_sensor, src->nnames, src->npaths);
}
if (!dest) {
mjERROR("failed to make mjModel. Invalid sizes.");
@@ -762,7 +763,7 @@ mjModel* mj_loadModel(const char* filename, const mjVFS* vfs) {
ints[28], ints[29], ints[30], ints[31], ints[32], ints[33], ints[34],
ints[35], ints[36], ints[37], ints[38], ints[39], ints[40], ints[41],
ints[42], ints[43], ints[44], ints[45], ints[46], ints[47], ints[48],
ints[49], ints[50], ints[51], ints[52]);
ints[49], ints[50], ints[51], ints[52], ints[53]);
if (!m || m->nbuffer != sizes[getnsize()-1]) {
mju_closeResource(r);
mju_warning("Corrupted model, wrong size parameters");
@@ -1791,7 +1792,8 @@ const char* mj_validateReferences(const mjModel* m) {
X(name_numericadr, nnumeric, nnames , 0 ) \
X(name_textadr, ntext, nnames , 0 ) \
X(name_tupleadr, ntuple, nnames , 0 ) \
X(name_keyadr, nkey, nnames , 0 )
X(name_keyadr, nkey, nnames , 0 ) \
X(mesh_assetpathadr, nmesh, npaths , 0 )
#define X(adrarray, nadrs, ntarget, numarray) { \
int *nums = (numarray); \
+1 -1
View File
@@ -62,7 +62,7 @@ mjModel* mj_makeModel(int nq, int nv, int nu, int na, int nbody, int nbvh, int n
int ntuple, int ntupledata, int nkey, int nmocap, int nplugin,
int npluginattr, int nuser_body, int nuser_jnt, int nuser_geom,
int nuser_site, int nuser_cam, int nuser_tendon, int nuser_actuator,
int nuser_sensor, int nnames);
int nuser_sensor, int nnames, int npaths);
// copy mjModel; allocate new if dest is NULL
MJAPI mjModel* mj_copyModel(mjModel* dest, const mjModel* src);
+41 -3
View File
@@ -275,6 +275,7 @@ void mjCModel::Clear(void) {
ntupledata = 0;
npluginattr = 0;
nnames = 0;
npaths = 0;
memory = -1;
nstack = -1;
nemax = 0;
@@ -1002,6 +1003,18 @@ void mjCModel::SetSizes(void) {
for (int i=0; i<nkey; i++) nnames += (int)keys[i]->name.length() + 1;
for (int i=0; i<nplugin; i++) nnames += (int)plugins[i]->name.length() + 1;
// npaths
npaths = 0;
for (int i=0; i<nmesh; i++) {
if (meshes[i]->file().empty()) {
continue;
}
npaths += (int)meshes[i]->file().length() + 1;
}
if (npaths == 0) {
npaths = 1;
}
// nemax
for (int i=0; i<neq; i++)
if (equalities[i]->type==mjEQ_CONNECT) {
@@ -1197,7 +1210,6 @@ void mjCModel::LengthRange(mjModel* m, mjData* data) {
}
// process names from one list: concatenate, compute addresses
template <class T>
static int namelist(vector<T*>& list, int adr, int* name_adr, char* names, int* map) {
@@ -1313,6 +1325,30 @@ void mjCModel::CopyNames(mjModel* m) {
}
void mjCModel::CopyPaths(mjModel* m) {
// start with 0 address.
size_t adr = 0;
m->paths[0] = 0;
for (unsigned int i=0; i<meshes.size(); i++) {
if (meshes[i]->file().empty()) {
m->mesh_assetpathadr[i] = -1;
continue;
}
m->mesh_assetpathadr[i] = adr;
// copy path
size_t path_size = meshes[i]->file().size();
memcpy(m->paths+adr, meshes[i]->file().c_str(), path_size);
adr += path_size;
// append 0
m->paths[adr] = 0;
adr++;
}
}
// copy objects inside kinematic tree
void mjCModel::CopyTree(mjModel* m) {
@@ -2703,7 +2739,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) {
nnumeric, nnumericdata, ntext, ntextdata,
ntuple, ntupledata, nkey, nmocap, nplugin, npluginattr,
nuser_body, nuser_jnt, nuser_geom, nuser_site, nuser_cam,
nuser_tendon, nuser_actuator, nuser_sensor, nnames);
nuser_tendon, nuser_actuator, nuser_sensor, nnames, npaths);
if (!m) {
throw mjCError(0, "could not create mjModel");
}
@@ -2712,6 +2748,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) {
m->opt = option;
m->vis = visual;
CopyNames(m);
CopyPaths(m);
CopyTree(m);
// assign plugin slots and copy plugin config attributes
@@ -2918,7 +2955,8 @@ bool mjCModel::CopyBack(const mjModel* m) {
neq!=m->neq || ntendon!=m->ntendon || nwrap!=m->nwrap || nsensor!=m->nsensor ||
nnumeric!=m->nnumeric || nnumericdata!=m->nnumericdata || ntext!=m->ntext ||
ntextdata!=m->ntextdata || nnames!=m->nnames || nM!=m->nM || nD!=m->nD ||
nB!=m->nB || nemax!=m->nemax || nconmax!=m->nconmax || njmax!=m->njmax) {
nB!=m->nB || nemax!=m->nemax || nconmax!=m->nconmax || njmax!=m->njmax ||
npaths!=m->npaths) {
errInfo = mjCError(0, "incompatible models in CopyBack");
return false;
}
+2
View File
@@ -182,6 +182,7 @@ class mjCModel {
void AutoSpringDamper(mjModel*);// automatic stiffness and damping computation
void LengthRange(mjModel*, mjData*); // compute actuator lengthrange
void CopyNames(mjModel*); // copy names, compute name addresses
void CopyPaths(mjModel*); // copy paths, compute path addresses
void CopyObjects(mjModel*); // copy objects outside kinematic tree
void CopyTree(mjModel*); // copy objects inside kinematic tree
@@ -235,6 +236,7 @@ class mjCModel {
int ntupledata; // number of objects in all tuple fields
int npluginattr; // number of chars in all plugin config attributes
int nnames; // number of chars in all names
int npaths; // number of chars in all paths
int nM; // number of non-zeros in sparse inertia matrix
int nD; // number of non-zeros in sparse dof-dof matrix
int nB; // number of non-zeros in sparse body-dof matrix
+1
View File
@@ -321,6 +321,7 @@ mjCModel* mjParseXML(const char* filename, const mjVFS* vfs, char* error, int er
// this is separate from the Parser to allow multiple URDFs to be loaded.
model->strippath = true;
model->fusestatic = true;
model->discardvisual = true;
parser.SetModel(model);
parser.Parse(root);
-3
View File
@@ -87,9 +87,6 @@ void mjXURDF::Parse(
int id_parent, id_child;
urPrefix = prefix;
// set compiler defaults suitable for URDF
model->discardvisual = true;
// parse MuJoCo sections (not part of URDF)
XMLElement* mjc = FindSubElem(root, "mujoco");
if (mjc) {