Skip to content

Commit 0e11047

Browse files
Google DeepMindcopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 809083979 Change-Id: I5c6641b73c76153c15f5cdf858b788e1433acc81
1 parent 26ea20a commit 0e11047

File tree

16 files changed

+98
-79
lines changed

16 files changed

+98
-79
lines changed

doc/includes/references.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,8 @@ typedef struct mjsCompiler_ { // compiler options
18451845
mjtByte saveinertial; // save explicit inertial clause for all bodies to XML
18461846
int alignfree; // align free joints with inertial frame
18471847
mjLROpt LRopt; // options for lengthrange computation
1848+
mjString* meshdir; // mesh and hfield directory
1849+
mjString* texturedir; // texture directory
18481850
} mjsCompiler;
18491851
typedef struct mjSpec_ { // model specification
18501852
mjsElement* element; // element type
@@ -1853,8 +1855,6 @@ typedef struct mjSpec_ { // model specification
18531855
// compiler data
18541856
mjsCompiler compiler; // compiler options
18551857
mjtByte strippath; // automatically strip paths from mesh files
1856-
mjString* meshdir; // mesh and hfield directory
1857-
mjString* texturedir; // texture directory
18581858

18591859
// engine data
18601860
mjOption option; // physics options

include/mujoco/mjspec.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ typedef struct mjsCompiler_ { // compiler options
151151
mjtByte saveinertial; // save explicit inertial clause for all bodies to XML
152152
int alignfree; // align free joints with inertial frame
153153
mjLROpt LRopt; // options for lengthrange computation
154+
mjString* meshdir; // mesh and hfield directory
155+
mjString* texturedir; // texture directory
154156
} mjsCompiler;
155157

156158

@@ -161,8 +163,6 @@ typedef struct mjSpec_ { // model specification
161163
// compiler data
162164
mjsCompiler compiler; // compiler options
163165
mjtByte strippath; // automatically strip paths from mesh files
164-
mjString* meshdir; // mesh and hfield directory
165-
mjString* texturedir; // texture directory
166166

167167
// engine data
168168
mjOption option; // physics options

python/mujoco/introspect/structs.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7928,6 +7928,20 @@
79287928
type=ValueType(name='mjLROpt'),
79297929
doc='options for lengthrange computation',
79307930
),
7931+
StructFieldDecl(
7932+
name='meshdir',
7933+
type=PointerType(
7934+
inner_type=ValueType(name='mjString'),
7935+
),
7936+
doc='mesh and hfield directory',
7937+
),
7938+
StructFieldDecl(
7939+
name='texturedir',
7940+
type=PointerType(
7941+
inner_type=ValueType(name='mjString'),
7942+
),
7943+
doc='texture directory',
7944+
),
79317945
),
79327946
)),
79337947
('mjSpec',
@@ -7959,20 +7973,6 @@
79597973
type=ValueType(name='mjtByte'),
79607974
doc='automatically strip paths from mesh files',
79617975
),
7962-
StructFieldDecl(
7963-
name='meshdir',
7964-
type=PointerType(
7965-
inner_type=ValueType(name='mjString'),
7966-
),
7967-
doc='mesh and hfield directory',
7968-
),
7969-
StructFieldDecl(
7970-
name='texturedir',
7971-
type=PointerType(
7972-
inner_type=ValueType(name='mjString'),
7973-
),
7974-
doc='texture directory',
7975-
),
79767976
StructFieldDecl(
79777977
name='option',
79787978
type=ValueType(name='mjOption'),

python/mujoco/specs.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,22 @@ PYBIND11_MODULE(_specs, m) {
593593
},
594594
py::arg("degree"), py::arg("sequence") = py::none(),
595595
py::arg("orientation"), py::return_value_policy::copy);
596+
mjSpec.def_property(
597+
"meshdir",
598+
[](MjSpec& self) -> std::string_view {
599+
return *self.ptr->compiler.meshdir;
600+
},
601+
[](MjSpec& self, std::string_view meshdir) {
602+
*(self.ptr->compiler.meshdir) = meshdir;
603+
});
604+
mjSpec.def_property(
605+
"texturedir",
606+
[](MjSpec& self) -> std::string_view {
607+
return *self.ptr->compiler.texturedir;
608+
},
609+
[](MjSpec& self, std::string_view texturedir) {
610+
*(self.ptr->compiler.texturedir) = texturedir;
611+
});
596612

597613
// ============================= MJSBODY =====================================
598614
mjsBody.def(

src/user/user_api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ int mjs_isWarning(mjSpec* s) {
297297
void mj_deleteSpec(mjSpec* s) {
298298
if (s) {
299299
mjCModel* model = static_cast<mjCModel*>(s->element);
300-
delete model;
300+
model->Release();
301301
}
302302
}
303303

src/user/user_flexcomp.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ bool mjCFlexcomp::Make(mjsBody* body, char* error, int error_sz) {
170170
break;
171171

172172
case mjFCOMPTYPE_MESH:
173-
res = MakeMesh(model, error, error_sz);
173+
res = MakeMesh(model, compiler, error, error_sz);
174174
break;
175175

176176
case mjFCOMPTYPE_GMSH:
177-
res = MakeGMSH(model, error, error_sz);
177+
res = MakeGMSH(model, compiler, error, error_sz);
178178
break;
179179

180180
case mjFCOMPTYPE_DIRECT:
@@ -1075,7 +1075,7 @@ template <typename T> static T* VecToArray(std::vector<T>& vector, bool clear =
10751075

10761076

10771077
// make mesh
1078-
bool mjCFlexcomp::MakeMesh(mjCModel* model, char* error, int error_sz) {
1078+
bool mjCFlexcomp::MakeMesh(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz) {
10791079
// strip path
10801080
if (!file.empty() && model->spec.strippath) {
10811081
file = mjuu_strippath(file);
@@ -1092,7 +1092,7 @@ bool mjCFlexcomp::MakeMesh(mjCModel* model, char* error, int error_sz) {
10921092
}
10931093

10941094
// load resource
1095-
std::string filename = mjuu_combinePaths(mjs_getString(model->spec.meshdir), file);
1095+
std::string filename = mjuu_combinePaths(mjs_getString(compiler->meshdir), file);
10961096
mjResource* resource = nullptr;
10971097

10981098

@@ -1194,7 +1194,7 @@ static int findstring(const char* buffer, int buffer_sz, const char* str) {
11941194

11951195

11961196
// load points and elements from GMSH file
1197-
bool mjCFlexcomp::MakeGMSH(mjCModel* model, char* error, int error_sz) {
1197+
bool mjCFlexcomp::MakeGMSH(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz) {
11981198
// strip path
11991199
if (!file.empty() && model->spec.strippath) {
12001200
file = mjuu_strippath(file);
@@ -1208,7 +1208,7 @@ bool mjCFlexcomp::MakeGMSH(mjCModel* model, char* error, int error_sz) {
12081208
// open resource
12091209
mjResource* resource = nullptr;
12101210
try {
1211-
std::string filename = mjuu_combinePaths(mjs_getString(model->spec.meshdir), file);
1211+
std::string filename = mjuu_combinePaths(mjs_getString(compiler->meshdir), file);
12121212
resource = mjCBase::LoadResource(mjs_getString(model->spec.modelfiledir),
12131213
filename, 0);
12141214
} catch (mjCError err) {

src/user/user_flexcomp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class mjCFlexcomp {
5757
bool MakeGrid(char* error, int error_sz);
5858
bool MakeBox(char* error, int error_sz, int dim, bool open = true);
5959
bool MakeSquare(char* error, int error_sz);
60-
bool MakeMesh(mjCModel* model, char* error, int error_sz);
61-
bool MakeGMSH(mjCModel* model, char* error, int error_sz);
60+
bool MakeMesh(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz);
61+
bool MakeGMSH(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz);
6262
void LoadGMSH(mjCModel* model, mjResource* resource);
6363
void LoadGMSH41(char* buffer, int binary, int nodeend, int nodebegin,
6464
int elemend, int elembegin);

src/user/user_mesh.cc

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,6 @@ void mjCMesh::NameSpace(const mjCModel* m) {
303303
if (modelfiledir_.empty()) {
304304
modelfiledir_ = FilePath(m->spec_modelfiledir_);
305305
}
306-
if (meshdir_.empty()) {
307-
meshdir_ = FilePath(m->spec_meshdir_);
308-
}
309306
if (!plugin_instance_name.empty()) {
310307
plugin_instance_name = m->prefix + plugin_instance_name + m->suffix;
311308
}
@@ -705,12 +702,12 @@ void mjCMesh::TryCompile(const mjVFS* vfs) {
705702
}
706703

707704
// copy paths from model if not already defined
705+
mujoco::user::FilePath meshdir_;
706+
meshdir_ = FilePath(mjs_getString(compiler->meshdir));
707+
708708
if (modelfiledir_.empty()) {
709709
modelfiledir_ = FilePath(model->modelfiledir_);
710710
}
711-
if (meshdir_.empty()) {
712-
meshdir_ = FilePath(model->meshdir_);
713-
}
714711

715712
// remove path from file if necessary
716713
if (model->strippath) {
@@ -3131,9 +3128,6 @@ void mjCSkin::NameSpace(const mjCModel* m) {
31313128
if (modelfiledir_.empty()) {
31323129
modelfiledir_ = FilePath(m->spec_modelfiledir_);
31333130
}
3134-
if (meshdir_.empty()) {
3135-
meshdir_ = FilePath(m->spec_meshdir_);
3136-
}
31373131
}
31383132

31393133

@@ -3224,9 +3218,8 @@ void mjCSkin::Compile(const mjVFS* vfs) {
32243218
if (modelfiledir_.empty()) {
32253219
modelfiledir_ = FilePath(model->modelfiledir_);
32263220
}
3227-
if (meshdir_.empty()) {
3228-
meshdir_ = FilePath(model->meshdir_);
3229-
}
3221+
mujoco::user::FilePath meshdir_;
3222+
meshdir_ = FilePath(mjs_getString(compiler->meshdir));
32303223

32313224
FilePath filename = meshdir_ + FilePath(file_);
32323225
mjResource* resource = LoadResource(modelfiledir_.Str(), filename.Str(), vfs);

src/user/user_model.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ mjCModel::mjCModel() {
140140
elemtype = mjOBJ_MODEL;
141141
spec_comment_.clear();
142142
spec_modelfiledir_.clear();
143-
spec_meshdir_.clear();
144-
spec_texturedir_.clear();
143+
meshdir_.clear();
144+
texturedir_.clear();
145145
spec_modelname_ = "MuJoCo Model";
146146

147147
//------------------------ auto-computed statistics
@@ -201,10 +201,12 @@ mjCModel& mjCModel::operator=(const mjCModel& other) {
201201
this->spec = other.spec;
202202
*static_cast<mjCModel_*>(this) = static_cast<const mjCModel_&>(other);
203203
*static_cast<mjSpec*>(this) = static_cast<const mjSpec&>(other);
204+
PointToLocal();
204205

205206
// copy attached specs first so that we can resolve references to them
206-
for (const auto* s : other.specs_) {
207-
specs_.push_back(mj_copySpec(s));
207+
for (auto* s : other.specs_) {
208+
specs_.push_back(s);
209+
static_cast<mjCModel*>(s->element)->AddRef();
208210
compiler2spec_[&s->compiler] = specs_.back();
209211
}
210212

@@ -869,13 +871,11 @@ void mjCModel::PointToLocal() {
869871
spec.comment = &spec_comment_;
870872
spec.modelfiledir = &spec_modelfiledir_;
871873
spec.modelname = &spec_modelname_;
872-
spec.meshdir = &spec_meshdir_;
873-
spec.texturedir = &spec_texturedir_;
874+
spec.compiler.meshdir = &meshdir_;
875+
spec.compiler.texturedir = &texturedir_;
874876
comment = nullptr;
875877
modelfiledir = nullptr;
876878
modelname = nullptr;
877-
meshdir = nullptr;
878-
texturedir = nullptr;
879879
}
880880

881881

@@ -885,8 +885,6 @@ void mjCModel::CopyFromSpec() {
885885
comment_ = spec_comment_;
886886
modelfiledir_ = spec_modelfiledir_;
887887
modelname_ = spec_modelname_;
888-
meshdir_ = spec_meshdir_;
889-
texturedir_ = spec_texturedir_;
890888
}
891889

892890

src/user/user_model.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ class mjCModel_ : public mjsElement {
148148
std::string spec_comment_;
149149
std::string spec_modelfiledir_;
150150
std::string spec_modelname_;
151-
std::string spec_meshdir_;
152-
std::string spec_texturedir_;
153151
};
154152

155153
// mjCModel contains everything needed to generate the low-level model.
@@ -330,7 +328,18 @@ class mjCModel : public mjCModel_, private mjSpec {
330328
// check for repeated names in list
331329
void CheckRepeat(mjtObj type);
332330

331+
// increment and decrement reference count
332+
void AddRef() { ++refcount; }
333+
int GetRef() const { return refcount; }
334+
void Release() {
335+
if (--refcount == 0) {
336+
delete this;
337+
}
338+
}
339+
333340
private:
341+
int refcount = 1;
342+
334343
// settings for each defaults class
335344
std::vector<mjCDef*> defaults_;
336345

0 commit comments

Comments
 (0)