Fix broken OBJ and PNG loading when a VFS is provided to mj_loadXML.

At present, if a VFS is used but an OBJ or PNG asset doesn't exist in the VFS, the model compilation will fail without trying to first locate the asset on disk.

This breaks the Python bindings `MjModel.from_xml_string` almost entirely when OBJ assets are involved.

Also modify error messages to disambiguate between different modes of asset loading failures.

PiperOrigin-RevId: 526637686
Change-Id: Iba02cc94989083bc2835a3e67dfc78c0a21047e9
This commit is contained in:
Saran Tunyasuvunakool
2023-04-24 07:36:51 -07:00
committed by Copybara-Service
parent 3db483c248
commit 66def34e0e
2 changed files with 25 additions and 19 deletions
+16 -10
View File
@@ -1983,7 +1983,7 @@ void mjCHField::LoadCustom(string filename, int default_provider) {
const void* buffer = 0;
mjResource* resource = nullptr;
if((resource = mju_openResource(filename.c_str(), default_provider)) == nullptr) {
if ((resource = mju_openResource(filename.c_str(), default_provider)) == nullptr) {
// default to OS filesystem
if (!default_provider || (resource = mju_openResource(filename.c_str(), 0)) == nullptr) {
throw mjCError(this, "could not open hfield file '%s'", filename.c_str());
@@ -1994,7 +1994,7 @@ void mjCHField::LoadCustom(string filename, int default_provider) {
// still not found
if (!buffer || buffer_sz < 1) {
throw mjCError(this, "could not open hfield file '%s'", filename.c_str());
throw mjCError(this, "could not read hfield file '%s'", filename.c_str());
}
if (buffer_sz < 2*sizeof(int)) {
@@ -2041,8 +2041,11 @@ void mjCHField::LoadPNG(string filename, int default_provider) {
const void* inbuffer = 0;
mjResource* resource = nullptr;
if((resource = mju_openResource(filename.c_str(), default_provider)) == nullptr) {
throw mjCError(this, "could not open PNG file '%s'", filename.c_str());
if ((resource = mju_openResource(filename.c_str(), default_provider)) == nullptr) {
// try reading from filesystem
if (!default_provider || (resource = mju_openResource(filename.c_str(), 0)) == nullptr) {
throw mjCError(this, "could not open PNG file '%s' %d", filename.c_str(), default_provider);
}
}
int inbuffer_sz = mju_readResource(resource, &inbuffer);
@@ -2050,7 +2053,7 @@ void mjCHField::LoadPNG(string filename, int default_provider) {
// still not found
if (!inbuffer || inbuffer_sz < 1) {
mju_closeResource(resource);
throw mjCError(this, "could not open PNG file '%s'", filename.c_str());
throw mjCError(this, "could not read PNG file '%s'", filename.c_str());
}
// load PNG from file or memory
@@ -2434,8 +2437,11 @@ void mjCTexture::LoadPNG(string filename, int default_provider,
const void* inbuffer = 0;
mjResource* resource = nullptr;
if((resource = mju_openResource(filename.c_str(), default_provider)) == nullptr) {
throw mjCError(this, "could not open PNG file '%s'", filename.c_str());
if ((resource = mju_openResource(filename.c_str(), default_provider)) == nullptr) {
// try reading from filesystem
if (!default_provider || (resource = mju_openResource(filename.c_str(), 0)) == nullptr) {
throw mjCError(this, "could not open PNG file '%s' %d", filename.c_str(), default_provider);
}
}
int inbuffer_sz = mju_readResource(resource, &inbuffer);
@@ -2443,7 +2449,7 @@ void mjCTexture::LoadPNG(string filename, int default_provider,
// still not found
if (!inbuffer || inbuffer_sz < 1) {
mju_closeResource(resource);
throw mjCError(this, "could not open PNG file '%s'", filename.c_str());
throw mjCError(this, "could not read PNG file '%s'", filename.c_str());
}
// load PNG from file or memory
@@ -2469,7 +2475,7 @@ void mjCTexture::LoadCustom(string filename, int default_provider,
const void* buffer = 0;
mjResource* resource = nullptr;
if((resource = mju_openResource(filename.c_str(), default_provider)) == nullptr) {
if ((resource = mju_openResource(filename.c_str(), default_provider)) == nullptr) {
// default to OS filesystem
if (!default_provider || (resource = mju_openResource(filename.c_str(), 0)) == nullptr) {
throw mjCError(this, "could not open texture file '%s'", filename.c_str());
@@ -2481,7 +2487,7 @@ void mjCTexture::LoadCustom(string filename, int default_provider,
// still not found
if (!buffer || buffer_sz < 0) {
mju_closeResource(resource);
throw mjCError(this, "could not open texture file '%s'", filename.c_str());
throw mjCError(this, "could not read texture file '%s'", filename.c_str());
}
// read dimensions