[PYTHON] NEED an explanation about CONTEXT- (context incorrect, invalid window, etc)

Hi everyone,
I’ve been scripting blender for a very little time, for my work.
I’m not programming a tool so I never try to modify the interface, I’m just calling a python script on a blend file, filling it with geometry, saving it, and then, I quit Blender - all by script. A very simple one (mostly polyline extrusion).
From times to times, I fall on a very mysterious error:
“Operator bpy.ops.xxxxx.poll() failed, context is incorrect”
After a few inquiries, I hardly learned a thing about context in Blender: this has something to do with, for example, the area above which your cursor is when you perform a task.
Until now, I’ve managed to dodge this lack of knowledge, but today is the day: I’ve decided to understand!
Well. Help me.
I’m unable to find any information about managing the context while scripting.
How does it work? How to somehow force the right context at the right time? How to know which context is needed for which Operation?

you have to provide more info
like error text
i mean there can be so many things to create this error

wiki tut on scripting
and a section on context

http://wiki.blender.org/index.php/Dev:2.5/Source/Architecture/Context

happy bl

Hi Ricky, Thanks for the link!
Here is the last error I had:“invalid window”

C:\Program Files\Blender Foundation\Blender>blender “C:[…]\81.blend” -P “C:[…]\buildGeometry.py”
Read new prefs: C:\Users\Administrator\AppData\Roaming\Blender Foundation\Blende
r\2.69\config\userpref.blend
found bundled python: C:\Program Files\Blender Foundation\Blender\2.69\python
read blend: C:[…]\81.blend
Traceback (most recent call last):
File “<string>”, line 1, in <module>
File “<string>”, line 40, in <module>
File “C:[…]\modelImport.py”, li
ne 23, in convertCurvesToMeshes
bpy.ops.object.convert(target = ‘CURVE’, keep_original=‘False’)
File “C:\Program Files\Blender Foundation\Blender\2.69\scripts\modules\bpy\ops
.py”, line 188, in call
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.convert.poll() failed, context is incorrec
t
<!> event has invalid window

I’m beginning to digest the wiki about context, thank you again

I’ve noticed one more thing:
The active area may be found here: bpy.context.area
If I try to force the type of this area to ‘VIEW_3D’: bpy.context.area.type = ‘VIEW_3D’
I get the following error: AttributeError: ‘NoneType’ object has no attribute ‘type’
So I try to figure out what is this area: print(dir(bpy.context.area))
–>[‘bool’, ‘class’, ‘delattr’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘le’, ‘lt’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’]
I see that there is no region or space or nothing.
Why is it void? How to change that?

Okay, I got it working for this time.
I didn’t have an active object. I took the first selected object I had, and it worked out.
But it was hard to figure out what it was, the error displayed “invalid window”, so I searched around window/area/screen contexts for a long time.
The thing is that for every bpy.ops operators, a different context seems to be needed, and I don’t have a “checklist” for the different context objects. I guess that practicing and a better understanding of the bContext struct will help me later.
I understood there isn’t a whole context object that I can change directly.
From now on, every time I get a context error I’ll check the following:
current active object
current selected object(s)
current mode
current area type
Feel free to add any other line to my “checklist” :wink:

glad you got the missng object
which is a must i guess for doing anything !

next time go to python forum for this type of problem

and also give a copy of script to follow what it should be doing!
and a description

happy bl