"context is incorrect" error occuring for a python script executed on command line


I’m new to blender’s python interace. I was hoping that someone could help me out with an error that I get when I run a python script from the command line. The script runs fine when I run it from within Blender’s script editor and hit run script. But when I run the exact same script from the command line using “blender -P some_script.py”, the script crashes with the following error:

File “C:\Program Files (x86)\Blender Foundation\Blender\2.62\scripts\modules\bpy\ops.py”, line 180, in call
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.anim.change_frame.poll() failed, context is incorrect

I am running the “Object Action” script (bouncing ball) from the following source:

Another thing I tried was to provide an empty .blend file to the blender command on the command line, but this didn’t yield any success either.

Any thoughts on what might be causing this issue? Would really appreciate help on this issue.

I’ve seen many threads on the internet where people have had similar issues of “context is incorrect” occurring when running something from the command line. Apparently it may have something to do with the fact that bpy.ops cannot be used from the command line?

I came across this thread in which they appear to reach this conclusion, but I’m not entirely sure if this applies to the latest build of Blender:

You can not use bpy.ops or reference the context from the command line or while rendering. Just alter your script so you do not use those functions or access data via them. Basically, there is no context when rendering or from the command line. The context is user initiated via the GUI.

This is a limitation of the new API.

Checkout the Gotchas section of the API about your poll failing.

Thanks Atom,

It looks like you’re right. I dug into this a bit more, I think it is possible to use bpy.ops functions as long as the function does not poll on the context. (I’ve actually seen some of these non-poll functions work myself)

It’ll be tricky to work around this bpy.ops functions / not use the context. It means I’ll need to probably get a much deeper understanding of how blender works.