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

Export overwrites objects from other scenes when called from operator #1690

Closed SolarLune closed 1 year ago

SolarLune commented 2 years ago

Hello! First of all, thank you all for the work you do in maintaining and working on the GLTF exporter - it's a real life-saver when it comes to getting data out of Blender in a comfortable and customizeable way. Really appreciate it!

As for the core issue, I'm running into this problem with my add-on that makes use of the GLTF exporter for my homemade 3D renderer, Tetra3D. I'm running Blender 3.2.1, with the packaged GLTF exporter.

When I export a GLTF file from the menu, the resulting file seems to be fine. However, when I call to export a GLTF file from the exporter operator (either triggered through GUI button press or through a save_post handler), the exporter doesn't export the right objects in the right slots (in this particular example, collection instances across multiple scenes). I believe at its core, the GLTF exporter isn't looking at the scene it should be when exporting through an operator, and may be tied to issue #1073 - I'm not sure on this one.

Here's a simple example blend file that exemplifies the issue - I have a Cube in Scene A, and in Scene B a collection instance of that same Cube.

exportTest.zip

If you export the blend file as a GLTF file from the menu, you'll get something like this:

"asset" : {
        "generator" : "Khronos glTF Blender I/O v3.2.43",
        "version" : "2.0"
    },
    "scene" : 0,
    "scenes" : [
        {
            "name" : "Scene A",
            "nodes" : [
                0
            ]
        },
        {
            "name" : "Scene B",
            "nodes" : [
                2
            ]
        }
    ],
    "nodes" : [
        {
            "mesh" : 0,
            "name" : "Cube"
        },
        {
            "mesh" : 0,
            "name" : "Cube"
        },
        {
            "children" : [
                1
            ],
            "name" : "Instanced",
            "translation" : [
                4.643479347229004,
                0,
                0
            ]
        }
    ],
...

Which is, to my understanding, correct. However, the bug arises when exporting through the bpy.ops.export_scene.gltf() operator. So, I've also packaged in the blend file a script that you can run in the text editor that will add a "test export" button to the object's Properties panel, which you can press to export the GLTF file through code (to a file named exportTest.gltf) and thereby recreate the bug. Pressing the button will export the GLTF file as before, but the contents will be incorrect and change depending on which scene you have selected.

If you have Scene A selected, you'll get this output:

    "asset" : {
        "generator" : "Khronos glTF Blender I/O v3.2.43",
        "version" : "2.0"
    },
    "scene" : 1,
    "scenes" : [
        {
            "name" : "Scene A",
            "nodes" : [
                0
            ]
        },
        {
            "name" : "Scene B",
            "nodes" : [
                1
            ]
        }
    ],
    "nodes" : [
        {
            "mesh" : 0,
            "name" : "Cube"
        },
        {
            "mesh" : 0,
            "name" : "Cube"
        }
    ],
...

Note that the instanced collection is no longer there.

If you have Scene B selected, you'll get the following output:

    "asset" : {
        "generator" : "Khronos glTF Blender I/O v3.2.43",
        "version" : "2.0"
    },
    "scene" : 1,
    "scenes" : [
        {
            "name" : "Scene A",
            "nodes" : [
                1
            ]
        },
        {
            "name" : "Scene B",
            "nodes" : [
                3
            ]
        }
    ],
    "nodes" : [
        {
            "mesh" : 0,
            "name" : "Cube"
        },
        {
            "children" : [
                0
            ],
            "name" : "Instanced",
            "translation" : [
                4.643479347229004,
                0,
                0
            ]
        },
        {
            "mesh" : 0,
            "name" : "Cube"
        },
        {
            "children" : [
                2
            ],
            "name" : "Instanced",
            "translation" : [
                4.643479347229004,
                0,
                0
            ]
        }
    ],
...

Note here that both objects have been replaced with instanced collections.

Also, in both of the above outputs, the "scene" property is incorrect and points to the last scene in the blend file, rather than whichever one you have selected when running the operator.

As an aside, in my own testing, I don't believe this is a problem if you run the operator with the mouse hovering over the 3D view, which may help to indicate where the issue may lie.

SolarLune commented 2 years ago

OK, so I tested again and it doesn't have to be an instanced collection - any object can be in the second scene. If you export from Scene A, then both scenes will have the cube from Scene A. If you export from Scene B, then both scenes will have whatever object is in Scene B.

julienduroure commented 2 years ago

Hello, Can you please share the .blend file(s) You zip contains only the glTF. I need the .blend file to reproduce your issue. Thanks!

SolarLune commented 2 years ago

Whoops, my mistake! Here you go:

gltfBuggedExportOperator.zip

Just to sum up the problem demonstrated in this blend file:

When exporting a GLTF file from the menu, everything's fine.

However, when exporting a GLTF file from an operator, the objects exported for each scene are just duplicates of the ones in the current scene.

To see this, unzip the blend file and run the "Text" script. Press the "test export" button in the object panel, and examine the outputted "exportTest.gltf" file (which will be in the same directory as the blend file wherever you unzipped it). The object information in the GLTF file will be incorrect for whichever scene is not selected when exporting.

julienduroure commented 2 years ago

Confirmed

SolarLune commented 1 year ago

Hey, thanks for fixing this - I tested out the fix and it resolves the issue. Thanks a lot!