Is it possible to instantiate a secondary scene object?

Hi!

I am currently working on a script that extends the Khronos glTF exporter with a batch-export functionality.
I thought that one way of doing that would be by creating a new scene object, copy everything that needs to be exported for each batch into it and then pass that new scene object to the glTF exporter.

Soooo… is that possible? If so how?

1 Like

it’s certainly possible, though I’m not entirely sure why you’d want to do it. copying a scene object is as simple as calling bpy.data.scenes[0].copy(). just remember that all of the data will be linked to the original scene, so you’ll have to manually unlink and re-create relevant blocks of data for the scene. scenes have four major things to keep track of (in terms of data): thew view layer, world, scene collection, and objects.

if you’re just exporting geometry you probably don’t care about the view layer or world, so you’d just need to clear out the collection and create a new one that’s linked to the new scene, then add whatever objects you care about to that collection.

relevant “how?” snippets:

# create a copy of a scene
my_new_scene = bpy.data.scenes[0].copy()
# clear out the collection in your new scene
for c in bpy.data.collections: 
    my_new_scene.collection.children.unlink(c)
# create a new collection and link it to your new scene
my_new_collection = bpy.data.collections.new("my new collection")
my_new_scene.collection.children.link(my_new_collection)
# populate the new collection with objects you care about. 
# in this example, only meshes
for o in bpy.data.objects:
    if hasattr(o, "data") and isinstance(o.data, bpy.types.Mesh):
        my_new_collection.objects.link(o)

Thank you, I’ll give that a shot. Looks like this is exactly what I need :slight_smile: