How to find out why the context of operator is incorrect

Hello,

I’m just playing with the blender’s python API to figure myself around and found the following problem. I want to execute an operator, let’s say like this:

bpy.ops.view3d.object_as_camera()

but I get an exception which tells me that the context is incorrect. Now I know that the operator has a method called poll which must return True for the operator to execute. But my question is, how do I find out, for any operator, what are the preconditions that must be met for the context of the operator to be correct (in other words, for the poll() to return True)? The Blender Python API documentation is not helpful in this. I can probably examine the source code of the operator in question, but is there a more straightforward way?

I wish I knew how to explain this better.
I’ll try, but in case I don’t, here’s the page in the manual:

http://www.blender.org/documentation/blender_python_api_2_59_3/bpy.context.html

How are you calling the operator? Is this a script in the text editor or commands in the console?

If you describe in detail how you are going about this, people will be able to be much more helpful.

In a probably-not-very-helpful nutshell, when or wherever you are calling bpy.ops.view3d.object_as_camera(), probably isn’t the view3d context unless you are calling it from within the 3d view. have no fear, this kind of problem disappears promptly once you understand it ( everybody runs into it at first )

in bpy.ops.____… the _____ is a hint at which context is applicable.

when defining custom operators, @poll is for preventing the op from being used out of context. It’s just a protective mechanism. without poll, the op just goes ahead and runs.

bpy.ops.view3d.object_as_camera() is expecting to be run in the 3d viewport, so it polls first and decides that in this case, it can’t run because it’s not being run in a viewport. Common causes, perhaps:

– you have typed this into the text editor and alt+P’d it. Running the script to define and register it is one thing. Calling it from the text editor is another thing. The text editor has (thankfully) its own context. The only thing provided there is context.edit_text. So if you’re doing this from the text editor, define it, register it, but don’t call it. Use the spacebar menu in the viewport, search for the op by name, and run it ‘from within’ the viewport. That way, it has an appropriate context.

– perhaps you have built a panel, with buttons, and one of those buttons ‘calls’ an operator. The panel gets to specify what type of space/region to appear in, and also there is bl_context which may be set by the panel. this defaults to “” which I ASSUME means it defaults to a generic context. I’m not sure whether this generic context would be ‘screen’, ‘scene’, or ‘object’ or something else, but I do know that different values for a panel’s bl_context matter significantly. The manual has had a TODO explain different combinations yada FOR EVAR…

– perhaps you are merely typing this command into the console, and getting


Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "/Applications/blender.app/Contents/MacOS/2.60/scripts/modules/bpy/ops.py", line 180, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.view3d.object_as_camera.poll() failed, context is incorrect

again, console has a context that is not the view3d context.

ok, i’ll admit i haven’t explained anything that well. I haven’t finished my first cup of coffee yet. Hope this helps. Check the manual, play with some templates in the text editor ( simple operator, simple panel … )

try making this the simple operator’s execute:


def execute(self,context):
    print(self)
    print(dir(self))
    print(context)
    print(dir(context))
    return {'FINISHED'}

it can be very informative.

holler back with more details and we’ll get it figured out for real.