# How to find out why "context is incorrect"? [duplicate]

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?

• 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. Aug 5 '14 at 19:05
• 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. Aug 5 '14 at 20:05

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.