How to pass custom context to complex operator?

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

toxikman
Posts: 12
Joined: Thu May 17, 2012 7:15 pm

How to pass custom context to complex operator?

Postby toxikman » Mon May 21, 2012 11:40 pm

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.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Tue May 22, 2012 12:38 am

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
I'm sitting, waiting, wishing, building Blender in superstition...

toxikman
Posts: 12
Joined: Thu May 17, 2012 7:15 pm

Postby toxikman » Tue May 22, 2012 1:46 am

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/blender_python_api_2_63_5/bpy.ops.html#overriding-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.

ldo
Posts: 544
Joined: Sun Nov 07, 2010 11:30 pm
Location: Hamilton, New Zealand

Postby ldo » Tue May 22, 2012 7:28 am

That’s new. It’s not in the previous version of the API: http://www.blender.org/documentation/bl ... y.ops.html

toxikman
Posts: 12
Joined: Thu May 17, 2012 7:15 pm

Postby toxikman » Tue May 22, 2012 9:12 am

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.

toxikman
Posts: 12
Joined: Thu May 17, 2012 7:15 pm

Postby toxikman » Fri Jun 01, 2012 9:58 pm

*bump*

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Fri Jun 01, 2012 11:48 pm

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.
I'm sitting, waiting, wishing, building Blender in superstition...

dvochin
Posts: 46
Joined: Mon Aug 20, 2012 9:39 pm

Postby dvochin » Wed Jul 24, 2013 2:00 am

For those who need to call bpy.ops operator from VIEW_3D context I created a solution at http://www.blender.org/forum/viewtopic.php?p=105783#105783 :)


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest