Make mjs_findElement find textures by file name.
This is done if the model is uncompiled and their name is empty. PiperOrigin-RevId: 772885774 Change-Id: I827d5b679637fa0571cfb0a69833ed688df16304
This commit is contained in:
committed by
Copybara-Service
parent
9e2c21a17d
commit
92bc486558
@@ -1001,6 +1001,14 @@ class SpecsTest(absltest.TestCase):
|
|||||||
texture.data = np.zeros((2, 2, 3), dtype=np.uint8).tobytes()
|
texture.data = np.zeros((2, 2, 3), dtype=np.uint8).tobytes()
|
||||||
spec.compile()
|
spec.compile()
|
||||||
|
|
||||||
|
def test_find_unnamed_texture(self):
|
||||||
|
spec = mujoco.MjSpec()
|
||||||
|
texture_file = spec.add_texture(file='file.png')
|
||||||
|
texture_name = spec.add_texture(name='name')
|
||||||
|
self.assertEqual(spec.texture('file'), texture_file)
|
||||||
|
self.assertEqual(spec.texture('name'), texture_name)
|
||||||
|
self.assertIsNone(spec.texture('none'))
|
||||||
|
|
||||||
def test_attach_units(self):
|
def test_attach_units(self):
|
||||||
child = mujoco.MjSpec()
|
child = mujoco.MjSpec()
|
||||||
parent = mujoco.MjSpec()
|
parent = mujoco.MjSpec()
|
||||||
|
|||||||
@@ -936,6 +936,8 @@ mjsElement* mjs_findElement(mjSpec* s, mjtObj type, const char* name) {
|
|||||||
case mjOBJ_LIGHT:
|
case mjOBJ_LIGHT:
|
||||||
case mjOBJ_FRAME:
|
case mjOBJ_FRAME:
|
||||||
return model->FindTree(model->GetWorld(), type, std::string(name)); // recursive search
|
return model->FindTree(model->GetWorld(), type, std::string(name)); // recursive search
|
||||||
|
case mjOBJ_TEXTURE:
|
||||||
|
return model->FindTexture(std::string(name)); // check filename too
|
||||||
default:
|
default:
|
||||||
return model->FindObject(type, std::string(name)); // always available
|
return model->FindObject(type, std::string(name)); // always available
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <filesystem> // NOLINT(build/c++17)
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -1369,6 +1370,23 @@ static T* findobject(std::string_view name, const vector<T*>& list, const mjKeyM
|
|||||||
return list[id->second];
|
return list[id->second];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
mjCBase* mjCModel::FindTexture(std::string_view name) const {
|
||||||
|
for (unsigned int i=0; i < textures_.size(); i++) {
|
||||||
|
if (textures_[i]->name == name) {
|
||||||
|
return textures_[i];
|
||||||
|
}
|
||||||
|
if (textures_[i]->name.empty() &&
|
||||||
|
std::filesystem::path(textures_[i]->spec_file_).filename().stem() == name) {
|
||||||
|
return textures_[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// find object in global lists given string type and name
|
// find object in global lists given string type and name
|
||||||
mjCBase* mjCModel::FindObject(mjtObj type, string name) const {
|
mjCBase* mjCModel::FindObject(mjtObj type, string name) const {
|
||||||
if (!object_lists_[type]) {
|
if (!object_lists_[type]) {
|
||||||
|
|||||||
@@ -248,6 +248,7 @@ class mjCModel : public mjCModel_, private mjSpec {
|
|||||||
mjCDef* FindDefault(std::string name); // find defaults class name
|
mjCDef* FindDefault(std::string name); // find defaults class name
|
||||||
mjCDef* AddDefault(std::string name, mjCDef* parent = nullptr); // add defaults class to array
|
mjCDef* AddDefault(std::string name, mjCDef* parent = nullptr); // add defaults class to array
|
||||||
mjCBase* FindObject(mjtObj type, std::string name) const; // find object given type and name
|
mjCBase* FindObject(mjtObj type, std::string name) const; // find object given type and name
|
||||||
|
mjCBase* FindTexture(std::string_view name) const; // find texture given name
|
||||||
mjCBase* FindTree(mjCBody* body, mjtObj type, std::string name); // find tree object given name
|
mjCBase* FindTree(mjCBody* body, mjtObj type, std::string name); // find tree object given name
|
||||||
mjSpec* FindSpec(std::string name) const; // find spec given name
|
mjSpec* FindSpec(std::string name) const; // find spec given name
|
||||||
mjSpec* FindSpec(const mjsCompiler* compiler_); // find spec given mjsCompiler
|
mjSpec* FindSpec(const mjsCompiler* compiler_); // find spec given mjsCompiler
|
||||||
|
|||||||
Reference in New Issue
Block a user