Skip to content

Refactor and fix UnityVersion __getitem__#365

Merged
K0lb3 merged 2 commits intoK0lb3:masterfrom
dv1x3r:unityversion
Feb 19, 2026
Merged

Refactor and fix UnityVersion __getitem__#365
K0lb3 merged 2 commits intoK0lb3:masterfrom
dv1x3r:unityversion

Conversation

@dv1x3r
Copy link
Copy Markdown
Contributor

@dv1x3r dv1x3r commented Feb 16, 2026

I encountered an issue exporting meshes from a bundle with UnityVersion 5.3p6.

Sample: bundle_5.3p6.zip

Using the following minimal example:

for obj in env.objects:    
    if obj.type.name == "Mesh":
        data = obj.parse_as_object()
        filename = data.m_Name+".obj"
        file_path = os.path.join(export_path, env.file.name, "models", filename)
        with open(file_path, "wt", newline="") as f:
            f.write(data.export())

I get this error:

File ~/.venv/lib/python3.13/site-packages/UnityPy/helpers/MeshHelper.py:165, in MeshHandler.process(self)
    159 if mesh.m_Use16BitIndices is not None:
    160     self.m_Use16BitIndices = bool(mesh.m_Use16BitIndices)
    161 elif (
    162     self.version >= (2017, 4)
    163     or
    164     # version == (2017, 3, 1) & patched - px string
--> 165     self.version[:2] == (2017, 3)
    166     and mesh.m_MeshCompression == 0
    167 ):
    168     self.m_Use16BitIndices = mesh.m_IndexFormat == 0
    169 self.copy_from_mesh()

File ~/.venv/lib/python3.13/site-packages/UnityPy/helpers/UnityVersion.py:91, in UnityVersion.__getitem__(self, idx)
     89 elif idx == 4:
     90     return self.type_number
---> 91 raise IndexError("Invalid UnityVersion index")

IndexError: Invalid UnityVersion index

The current UnityVersion.__getitem__ implementation does not support slicing, and always raises index error.

In this PR I did not change the (2017, 3) condition (I think it would still be better to add explicit parentheses because of the mixed and and or conditions) and instead added slice support.

@K0lb3
Copy link
Copy Markdown
Owner

K0lb3 commented Feb 19, 2026

Thanks, I missed the slice issue somehow.
And you're right, some explicit parentheses would be better at that place.

You can make a PR for it as well if you want, or I will add it at some point when I rewrite the mesh code.
Which should be soonish, as I plan to replace the python arrays with native arrays with typed sizes, likely via numpy or lighter via memoryviews, so that numpy isn't a dependency.

@K0lb3 K0lb3 merged commit 4b28e64 into K0lb3:master Feb 19, 2026
@dv1x3r
Copy link
Copy Markdown
Contributor Author

dv1x3r commented Feb 19, 2026

@K0lb3 thank you!

I have run into another issue with the mesh exporter that I am trying to debug right now. For some reason, some meshes are still failing to export with ValueError: Vertex data access out of bounds error. They work fine with AssetStudio tho. Sample: Login.unity3d.zip

I will investigate further first and, if successful, submit a PR with the necessary adjustments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants