KhronosGroup / glTF-Blender-IO

Blender glTF 2.0 importer and exporter
https://docs.blender.org/manual/en/latest/addons/import_export/scene_gltf2.html
Apache License 2.0
1.48k stars 316 forks source link

Patching vertices of an imported glTF file #2042

Open vvoovv opened 10 months ago

vvoovv commented 10 months ago

Coordinates in my glTF file have huge values (millions of units). I suspect that leads to some numerical errors after the import to Blender.

That's why I'd like to apply an offset to all vertices of the imported glTF before a Blender mesh is created.

I thought the right place to do it is the method gather_import_mesh_before_hook of a custom class glTF2ImportUserExtension. The method gather_import_mesh_before_hook gets gltf_mesh as an argument. However I don't see how to patch the vertices of gltf_mesh in an easy way.

As I see in the method do_primitives in the file _io_scene_gltf2/blender/imp/gltf2_blendermesh.py that getting the original coordinates involves some sort of decoding:

vs = BinaryData.decode_accessor(gltf, prim.attributes['POSITION'], cache=True)
vert_locs = np.concatenate((vert_locs, vs[unique_indices]))

Then the Blender mesh is filled with values:

mesh.vertices.foreach_set('co', squish(vert_locs))

That all probably means that the patched vertices must be encoded back before filling the Blender mesh with values.

If an easy way of patching the original vertices does not exist, it would be good to have an additional hook for patching squish(vert_locs).

Please advise.

julienduroure commented 10 months ago

Hello, We may have to add another hook to handle data you want to modify

vvoovv commented 10 months ago

That would be certainly great!