Scene duplication

Hi.

Let’s say the user has multiple scenes, and some of them may be named Scene.001, etc.

If I do bpy.ops.scene.new(type=‘FULL_COPY’), how do I know which one is the new one?

Normally I’d expect the new scene to update the context and this to work:

bpy.ops.scene.new(type='FULL_COPY')
copy = bpy.context.scene

It does work if you type it into the console. However, that doesn’t appear to be the case if I put this into an addon script (bpy.context.scene seems to update only after the operator is done, which is not useful).

Any ideas how I can determine which scene is the new one? Is this a bug?

This prints the new scene for the me:

import bpy

bpy.ops.scene.new(type='FULL_COPY')

scene = bpy.context.scene
print(scene.name)

What is your exact set up in which it does NOT work?

You might try to get it like this. I did not find an update method.

scene = bpy.data.scenes[-1]

I’m trying to change the way my baking addon works so it does all its baking on a temporary scene so that I can do some destructive things on the scene (merging all the hipoly objects into one in particular). I’ve literally just started on that, so there’s not much there yet, but here’s the source:
http://www.pasteall.org/54874/python
and here’s a test .blend:
http://www.pasteall.org/blend/32452

The interesting bit is on line 231. It should print “Scene.001”, but it prints “Scene” for some reason.

If you have general pointers on how to make the addon better in general, that would be most welcome as well. And thanks. :slight_smile:

I have said it before and I’ll say it again. Use not bpy.ops for it is evil!! EVIL!!. (There’s my Halloween plug).

Consider what you are doing, you are given “the scene” in your execute yet you continually fetch it via the context then execute god know what happens in a bpy.ops call and expect your scene to still remain intact. A better approach is to pass the given scene from execute to all the defs that need that scene (i.e. bpy.data.scenes[0]). To make a copy of the scene just use .copy() then throw it away.

The sooner you quit using bpy.ops and start controlling all aspects via bpy.data the less time you will spend tracking down bugs related to context and hoping that it will still be valid after executing some bpy.ops call.

I don’t like to rely on context either, but I don’t see what choice I have. Call me stupid, but doing a .copy() on a scene doesn’t seem to do anything.

You might try to get it like this. I did not find an update method.

scene = bpy.data.scenes[-1]

That will not work, datablocks are sorted alphabetically, not in creation order (if there’s a scene called “zzz” and you duplicate “aaa”, it will return “zzz” instead of “aaa.001”)

To make a copy of the scene just use .copy() then throw it away.

You can’t copy a scene via RNA, Scene.copy() returns None:

Create a copy of this datablock (not supported for all datablocks)

There’s no way around bpy.ops for scene duplication.