mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
3.02k stars 583 forks source link

Fix passing read only vertex color buffer when loading GLTF #2233

Closed RealDanTheMan closed 5 months ago

RealDanTheMan commented 5 months ago

Without the fix GLTF loading will fail assigning vertex color properly down the line when colors are converted to RGBA which attempts to modify the buffer contents directly, as a result raising exception.

https://github.com/mikedh/trimesh/blob/7853a5ebae3b35275f4d8d65cbc48bcd3a3c8a40/trimesh/visual/color.py#L583

RealDanTheMan commented 5 months ago

Proposed solution only creates a copy when vertex colors are valid, when their length == vertex count

mikedh commented 5 months ago

Looks good thanks for the fix! Is there a sample mesh we could add to unit tests for this (or something in tests/corpus maybe?)

RealDanTheMan commented 5 months ago

Hey @mikedh, I have added the test case along with a simple subdivided cube model for it, the cube is filled with magenta vertex color.

The test case has to actually validate the values stored in the vertex color to prove success, because even if the vertex color access fails it falls back on some default or pre-calculated values, in my test cases without the fix they were reading [102 102 102 255] instead of expected [255 0 255 255]

mikedh commented 5 months ago

Awesome test and fix looks good, thanks for the PR!