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.49k stars 319 forks source link

Collection instances duplicate meshes when the objects has modifiers #1952

Open eliasdaler opened 1 year ago

eliasdaler commented 1 year ago

When the object has a modifier, instancing it via group collection still produces multiple meshes even if "Apply Modifiers" checkbox is checked.

Repro:

  1. Ctrl+2 - Apply sub div modifier to a default cube
  2. Ctrl+G -> Create "CubeCollection"
  3. Shift+A -> Collection instance -> CubeCollection
  4. Export to gltf with "Apply Modifiers" on.

This is what you get:

    "nodes":[
        {
            "mesh":0,
            "name":"Cube"
        },
        {
            "mesh":1,
            "name":"Cube"
        },
        {
            "children":[
                1
            ],
            "name":"CubeCollection",
            "translation":[
                -2.7222368717193604,
                0,
                -4.626335620880127
            ]
        }
    ],

The created cube doesn't use "0" mesh, it has its own mesh. Making more collection instances makes even more meshes.

Expected behavior The mesh shouldn't be duplicated in gltf file and should be reused instead.

.blend file/ .gltf Here

Version

julienduroure commented 1 year ago

Confirmed. When using "Apply modifier", we can't keep mesh instancing, as we don't know if modifiers are the same, and with same parameters. But your case is a special cases, where objects are instanced via collection instances.

I will check if this is something we can detect correctly, and then keep mesh instancing. Setting as enhancement for now

shrinktofit commented 1 year ago

we can't keep mesh instancing, as we don't know if modifiers are the same, and with same parameters.

A naive idea is: if two meshes have same mesh data but have modifiers, insert a "deduping" pass to exporting -- if the two result mesh, after applying modifiers, are exactly same(exactly same vertices, edge, faces...) -- use one of them.