Speed up FBX export of vertex colors with numpy #104454

Merged
Bastien Montagne merged 1 commits from Mysteryem/blender-addons:fbx_numpy_vertex_colors_pr_standalone into main 2023-03-06 15:30:14 +01:00

1 Commits

Author SHA1 Message Date
Thomas Barlow 5d7e53eeba Speed up FBX export of vertex colors with numpy
Use buffer matching the single precision float type of the vertex color data in foreach_get to avoid having to iterate and cast every element in the C foreach_getset function when single precision float is not 64 bits (it's usually 32 bits). Numpy can do the casting to the 64 bit fbx type much faster with .astype when needed. The largest relative speedup from this occurs with getting the 'color' property of Color attributes because the 'color_srgb' property and Byte Color attributes appear to have additional costs, which are presumably the conversion to sRGB and/or the conversion from byte to single precision float.

Find unique colors and their indices using numpy.

Use numpy to expand 'POINT' domain vertex colors to "ByPolygonVertex" using the loop vertex indices.

'POINT' domain vertex colors at about 1000+ vertices of a subdivided default cube:
~12-14 times faster for Byte Color
~16-22 times faster for Color (~12-19 for 'color_srgb')

'CORNER' domain vertex colors at about 1000+ loops of a subdivided default cube:
~5-7 times faster for Byte Color
~7-9 times faster for Color (~6-7 for 'color_srgb')

This does change the exported fbx because the vertex colors are sorted (albeit not in order because they are viewed as a different type for sorting performance), causing the "Colors" array to be in a different order and the "ColorIndex" array to match the new ordering, but the full array of colors reconstructed by indexing the "Colors" array with each "ColorIndex" in sequence does remain the same, so while the exported file may be different, the result of importing the different file will still be the same.
2023-03-06 15:29:06 +01:00