Page 1 of 1

How to pass custom context to complex operator?

Posted: Mon May 21, 2012 11:40 pm
by toxikman
I've searched everywhere, but can't find how to pass a custom context to an operator that takes multiple arguments. This doesn't work, python says the keyword "context" is not a valid property:

Code: Select all

# assume code above generates to_export list
override_context = { 'selected_objects': to_export }
bpy.ops.wm.collada_export(context=override_context, filepath=filepath, check_existing=False, selected=True)
The documentation just gives examples for passing a context to an operator where that's the only argument. I.E:

Code: Select all

override = {'selected_bases': list(bpy.context.scene.object_bases)}
bpy.ops.object.delete(override)
Sorry, I'm a python+blender noob.

Posted: Tue May 22, 2012 12:38 am
by CoDEmanX
it's not working like that, you can't pass a context to operators.

but you can set a context object like
bpy.context.scene.objects.active = ob

however, running operators from py console may not work. Some require to be run from 3d view (e.g. space bar menu)

more info:
http://www.blender.org/documentation/bl ... ml#context

Posted: Tue May 22, 2012 1:46 am
by toxikman
CoDEmanX wrote:it's not working like that, you can't pass a context to operators.

but you can set a context object like
bpy.context.scene.objects.active = ob

however, running operators from py console may not work. Some require to be run from 3d view (e.g. space bar menu)

more info:
http://www.blender.org/documentation/bl ... ml#context
Then why does this page refer to overriding context properties passed into operators? http://www.blender.org/documentation/bl ... ng-context

Sure, I know about setting context.scene.objects.active, but I'm trying this simpler approach so that I can export without changing the active and selected objects.

Posted: Tue May 22, 2012 7:28 am
by ldo
That’s new. It’s not in the previous version of the API: http://www.blender.org/documentation/bl ... y.ops.html

Posted: Tue May 22, 2012 9:12 am
by toxikman
ldo wrote:That’s new. It’s not in the previous version of the API: http://www.blender.org/documentation/bl ... y.ops.html
OK thanks, but it's still useful for my purposes, assuming I can figure out how to pass in a context override when the operator call has arguments.

Posted: Fri Jun 01, 2012 9:58 pm
by toxikman
*bump*

Posted: Fri Jun 01, 2012 11:48 pm
by CoDEmanX
ok, so there seem to be some problems with context overriding, i got blender to crash various times
http://projects.blender.org/tracker/?fu ... group_id=9

despite of that, i see what's the problem with your code:

Code: Select all

# assume code above generates to_export list 
override_context = { 'selected_objects': to_export } 
bpy.ops.wm.collada_export(context=override_context, filepath=filepath, check_existing=False, selected=True) 
you try to pass override_context to the operator using a keyword argument. There is indeed no context parameter.

If you want to pass the custom context, you gotta pass it as first argument, but not keyword arg!

like this:

bpy.ops.wm.collada_export(override_context, filepath=filepath, check_existing=False, selected=True)

"context=" is removed, all following arguments are keyword args. That should do the trick.

Posted: Wed Jul 24, 2013 2:00 am
by dvochin
For those who need to call bpy.ops operator from VIEW_3D context I created a solution at http://www.blender.org/forum/viewtopic. ... 783#105783 :)