I was trying to use Blender from external python console using RPC. I was able to establish the connections and some basic operations. But, I cannot change the mode of an object, it is showing poll()
error, context is incorrect
. I know that these operations requires a context to run. Since it is running from external, current context is None. Is there is an alternative option to do so?
1 Answer
As in the docs, some operations requires context to perform (For example: To select edit mode: bpy.ops.object.mode_set(mode='EDIT')
) When we try to run blender from external console, we won't get the corresponding context results the error is thrown. To avoid these types of error, we need to override the context.
For this example, the required context can be obtained using the following code:
override = bpy.context.copy()
for window in bpy.data.window_managers[0].windows:
screen = window.screen
workspace = window.workspace
for area in screen.areas:
if area.type == 'VIEW_3D':
override['window'] = window
override['screen'] = screen
override['area'] = area
override['workspace'] = workspace
override['active_object'] = bpy.context.view_layer.objects.active
break
bpy.ops.object.mode_set(override, mode='EDIT')
IMPORTANT
For every operation, the context would be different. So, you need to find the correct context for every operation in a trial and error method. The above context can only be used in the case of changing to edit mode
bpy.data
, that might be functional. $\endgroup$