[Bf-python] Changing the scene in a script run from the command line

Martin Poirier theeth at yahoo.com
Thu Sep 9 23:48:13 CEST 2010


Hi,

--- On Thu, 9/9/10, Tom Haines <thaines at gmail.com> wrote:

> Hello all, and especially
> ideasman_42, who it was suggested to me
> might be the person most likely to be able to help me on
> irc.
> Unfortunately with me being in the UK and him in Australia
> the time
> zones are kinda problematic, hence using the mailing list.
> 
> I am exporting a collada file from the command line with
> Blender 2.5,
> specifically by running a .py file using the -P option,
> with -b to
> avoid the gui appearing (Though it also fails if the -b
> option is
> omitted.). This works, but the collada exporter exports
> entire scenes,
> hence I want to set the scene before doing the export. My
> best effort
> consists of:
> 
> bpy.context.screen.scene =
> bpy.data.scenes['scene_to_export']
> bpy.ops.wm.collada_export(filepath='output_file.dae')
> 
> but the first line does not work (I have tried other ways,
> but they
> all fundamentally involve setting the scene of the current
> screen.).
> It fails silently, and neither changes the scene value of
> the screen
> or bpy.context.scene, which is what I really want to
> change. Looking
> at the api the only place I can see where the option to set
> the scene
> is as part of a screen, and whilst that works when running
> the gui
> (i.e. if entered into the console.) when run from the
> command line it
> exports the contents of whatever scene was set when the
> .blend file
> was saved, rather than the desired scene.

You should be able to do it like this (assuming I don't mess up the syntax):

bpy.ops.wm.collada_export(C={"scene":bpy.data.scenes['scene_to_export']}, filepath='output_file.dae')

This sets up a custom context for that operator, its values will overwrite whatever is in bpy.context.

Martin





More information about the Bf-python mailing list