1
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ I've never run Blender in this way, but are context overrides available? $\endgroup$ Dec 23, 2020 at 18:12
  • $\begingroup$ @ChrisHayes yes there is. In my case I cannot copy the context since it is giving me none. Is there a way to create a context and use it? $\endgroup$
    – swarthan
    Dec 23, 2020 at 18:22
  • $\begingroup$ A little further down that page (right above "Submodules") is an example where they pass a context which isn't copied from the current context. Assuming you can get a window/screen/area from bpy.data, that might be functional. $\endgroup$ Dec 23, 2020 at 18:35
  • $\begingroup$ @ChrisHayes Thanks man, I stuck with the required keys to create a new context. Anyway I found a way to override. $\endgroup$
    – swarthan
    Dec 24, 2020 at 12:22
  • $\begingroup$ Great to hear! It'd be awesome if you could post an answer detailing your solution, to help out anyone else who runs into this. $\endgroup$ Dec 24, 2020 at 17:55

1 Answer 1

2
$\begingroup$

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

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .