context is incorrect (control Blender in real time from C++)

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
ys77
Posts: 0
Joined: Tue May 14, 2013 4:06 pm

context is incorrect (control Blender in real time from C++)

Post by ys77 »

My name is ys77.
This is the first time I use this site.

My goal is to control human's armature in real time from my C++ program.
I am trying to control blender by sending Python command using interprocess communication from my C++ program (e.x. socket).
Blender is launched with "-P call_serversocket.py". The python script calls Daemon server which receives Python commands sent by my C++ program and executes the commands with Python function "exec()".

(scripts is shown in http://blender.jp/modules/newbb/viewtop ... umpost7773)

I have already succeeded sending the following commands:

bpy.ops.mesh.primitive_cube_add()
bpy.data.objects[\"Cube\"].location.x += 5

However, the following commands cannot be executed with an error.

bpy.ops.object.mode_set(mode='EDIT', toggle=False)
bpy.ps.import_anim.bvh(filepath="****.bvh")

The Error is
"***.poll() failed, context is incorrect"

What is the reason that this error is occurred?
What should I do to avoid this error?

I will be really grateful if you give me some help.
Sincerely,
ys77

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX »

Did you try in object mode? It's probably not working 'cause you are still in edit mode. And if armature is used as target option, you probably need to set an armature object as active object.
I'm sitting, waiting, wishing, building Blender in superstition...

ys77
Posts: 0
Joined: Tue May 14, 2013 4:06 pm

Post by ys77 »

Dear CoDEmanX,

Thank you for your kind suggestion.
However,
"bpy.ops.object.mode_set(mode='OBJECT', toggle=False)"
cannot be done.

To be more precise,
When I executed the following python command with "exec()" in Python,
----------------------------------
exec( import bpy )
exec( bpy.ops.import_anim.bvh(filepath="C:\Research\la_bvh_sample00.bvh") )
----------------------------------

I got th following error.
-----------------------------
parsing bvh 'C:\\Research\\la_bvh_sample00.bvh'...0.0500
Traceback (most recent call last):
File "C:\Blender\2.66\scripts\addons\io_anim_bvh\__init__.py", line 142, in execute
return import_bvh.load(self, context, **keywords)
File "C:\Blender\2.66\scripts\addons\io_anim_bvh\import_bvh.py", line 572, in load
global_matrix=global_matrix,
File "C:\Blender\2.66\scripts\addons\io_anim_bvh\import_bvh.py", line 371, in bvh_node_dict2armature
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
File "C:\Blender\2.66\scripts\modules\bpy\ops.py", line 188, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect

location: C:\Blender\2.66\scripts\modules\bpy\ops.py:188
-----------------------------

I will appreciate if you tell me how to solve this error.
Best Regards,
ys77

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX »

mode_set usually fails if context.object is None (which is the case after you deleted an object). But if it's None, mode should already be 'OBJECT'. You can test if context is valid by using:

Code: Select all

if bpy.ops.object.mode_set.poll():
    bpy.ops.object.mode_set(mode='OBJECT')
else:
    print("mode_set() context is incorrect, current mode is", bpy.context.mode)
I'm sitting, waiting, wishing, building Blender in superstition...

Post Reply