6
$\begingroup$

I have been trying to write my first addon for Blender over the last couple of days. One error I came across quite often is the context is incorrect error for an operation of bpy.ops.

I learned that one can ask the poll function of an operator if the context is correct, but this does not tell why the context is correct/incorrect. What would be the best way to figure that out? Only way I could think of is to look into the code of the poll function. If that is the best way, what is the fastest way to find the appropriate piece of code?

$\endgroup$

marked as duplicate by CoDEmanX, gandalf3, iKlsR Oct 20 '14 at 22:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ I haven't really looked at Python in Blender, but I think the reason is that you're not in the correct Mode; When you're using Blender normally, not trough Python, there's certain operations that you can only perform in, let's say, Edit Mode; I think that's you're problem. Then again, I've never used Python. $\endgroup$ – someonewithpc Aug 5 '14 at 19:05
  • $\begingroup$ yes, I agree, that can be one reason. But I believe there are more reasons. For example, the join function merges all selected objects into the active object. If the active object however is a camera, you will get an "context is incorrect" error. $\endgroup$ – user1809923 Aug 5 '14 at 20:05
5
$\begingroup$

You can find out (part of) the context you are in by:

cont = bpy.context.area.type
print(str(cont))

This in itself will help you determine where you are (contextually speaking). Determining where you need to be is another story. Each operation that you do in Blender requires that you be in the correct context. The way I think of context is, what window (context) does the operation occur in when you do the action by clicking with the mouse? You also may need to refer to the API or other info, but one thing I found to help is knowing what contexts exist!

And easy way to find that out is:

bpy.context.area.type = '?'

This throws a useful error:

TypeError: bpy_struct: item.attr = val: enum "?" not found in ('VIEW_3D', 'TIMELINE', 'GRAPH_EDITOR', 'DOPESHEET_EDITOR', 'NLA_EDITOR', 'IMAGE_EDITOR', 'SEQUENCE_EDITOR', 'CLIP_EDITOR', 'TEXT_EDITOR', 'NODE_EDITOR', 'LOGIC_EDITOR', 'PROPERTIES', 'OUTLINER', 'USER)

Well, there are your context types . . .

I don't know if that has successfully answered your question, because I do not write plugins, yet, but it might has something that helps get you past the current issue in it.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.