[Unity plugin] Surface a clear error when mesh asset is missing#3268
[Unity plugin] Surface a clear error when mesh asset is missing#3268ingyukoh wants to merge 1 commit into
Conversation
|
A reasonable tweak to make errors with imports from broken MJCFs more instructive. Could also be useful for the future when there are new mesh formats in main MJ that haven't been implemented in Unity yet. Please adjust the wording of the error message based on the feedback! |
| if (Mesh == null) { | ||
| throw new Exception( | ||
| $"Could not load mesh asset '{assetName}'. The MJCF references a mesh " | ||
| + "that was not imported, or another asset of a different type (e.g. a " |
There was a problem hiding this comment.
I thought that non-mesh assets that share the name are no longer the issue. The failure is only when a mesh was not imported, or there was a failure in the import, no?
If that's the case, the second half of the error message sentence is unhelpful/redundant.
When an MJCF references a mesh whose asset failed to import, or whose name collides with another asset of a different type in the Resources folder, Resources.Load<Mesh>(...) returns null. The Mesh field is then dereferenced silently later (in BuildMesh / DebugDraw), producing an opaque NullReferenceException far from the root cause. Throw a descriptive exception at the load site so users hit the import abort path with a message that points at the actual problem. Related to google-deepmind#1354 (typed Resources.Load was already in place; this adds the missing diagnostic on top).
edec9c1 to
6bbe154
Compare
|
Thanks — wording updated. Dropped the type-collision clause since #1354 already handles that case, and replaced it with the unsupported-mesh-format scenario you mentioned. |
|
Thanks for the rephrase. LGTM. Could I get your help with approval @erez-tom? |
Related to #1354.
Summary
When an MJCF references a mesh whose asset failed to import — or whose name collides with another asset of a different type in a Resources folder —
Resources.Load<Mesh>(assetName)inMjMeshShape.FromMjcfreturnsnull, theMeshfield is left null, and downstream code (BuildMesh,DebugDraw) throws an opaqueNullReferenceExceptionfar from the actual cause. This patch surfaces a descriptive exception at the load site so the user hits the importer abort path with a message that points at the real problem.Context
The typed
Resources.Load<Mesh>(assetName)fix BH proposed in #1354 is already inmain. That fix resolves the case where both a Mesh and a non-Mesh asset of the same name exist (the type filter returns the Mesh). It does not, however, help the case where only the non-Mesh asset survives import — there the typed load returnsnulland the failure becomes silent.This PR is intentionally scoped to the diagnostic gap, not the runtime resolution behaviour.
Diff
if (!string.IsNullOrEmpty(assetName)) { Mesh = Resources.Load<Mesh>(assetName); + if (Mesh == null) { + throw new Exception( + $"Could not load mesh asset ''{assetName}''. The MJCF references a mesh " + + "that was not imported, or another asset of a different type (e.g. a " + + "material) shares the same name in a Resources folder."); + } }MjImporterWithAssets.ImportStringalready catchesExceptionand deletes the partial asset directory, so throwing here cleanly aborts the import with the asset cleanup it already does today.Test plan
git diff --check.mat-but-no-.objasset name collision, which I can add in a follow-up if you want it)If you would prefer the diagnostic as a
Debug.LogErrorrather than an exception, happy to swap.