5
$\begingroup$

I run Blender 2.80 as python module (for running on a server with Django), so there is no gui. Apparently passing the correct context doesn't seem to work for me in this case. I have tried many suggested implementations to override context (a lot were for Blender 2.7), but i still get context errors for most operators For example the line: bpy.ops.object.mode_set(mode='EDIT') with or without ...(override, ...

produces the error

bpy.ops.object.mode_set.poll() failed, context is incorrect

and I cannot search through the contexts until for example the correct area is found:

for area in bpy.context.window.screen.areas:
    if area.type == 'VIEW_3D':
        v3d = area.spaces[0]
        rv3d = v3d.region_3d
        for region in area.regions:
            if region.type == 'WINDOW':
                break            

override = {
    'scene'            : bpy.context.scene,
    'region'           : region,
    'area'             : area,
    'space'            : v3d
}

as this will produce the error:

'NoneType' object has no attribute 'screen'

When running the following Code:

print(bpy.context.window)
print(bpy.context.mode)
print(bpy.context.area)
print(bpy.context.region)
print(bpy.context.scene)

the output I get is:

None 
OBJECT 
None 
None 
<bpy_struct, Scene("Scene")>

So maybe there is no fully set context to begin with? Could it be an option to make a new context?

The problem occurs even when i have a .py (with only some other Django methods) where i use import bpy at the start of the file and do bpy.ops.object.mode_set(mode='EDIT') in an if statement that is activated by django at the program startup.

However this code example works normally and exports a .stl file with a rounded cube:

import bpy
bpy.ops.object.select_all(action='SELECT')
bpy.data.objects.remove(bpy.data.objects["Cube"], do_unlink=True)
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.mesh.primitive_cube_add()
myObj = bpy.context.object
myObj.select_set(True)
bpy.context.view_layer.objects.active = myObj
myObj.location = ( 0, 1, 1)
myObj.scale = ( 1/2, 1/2, 1/2)
mySubsurfMod = myObj.modifiers.new(myObj.name, 'SUBSURF')
mySubsurfMod.levels = 4
bpy.ops.export_mesh.stl(filepath="myExportSTL.stl")

Does someone know how to make override context work in blender 2.80? Or maybe at least a good resource for deep level commands to avoid operators? Or is there maybe even a way to see the deep level commands which are accessed by the operators (similar to the info screen) in blender?

Thanks in advance!!!

$\endgroup$
9
  • 2
    $\begingroup$ Please provide some example of how you are running this from command line, where and how you are overriding context. $\endgroup$
    – batFINGER
    Jul 1, 2019 at 13:11
  • 1
    $\begingroup$ Expect context.window, context.screen etc to be None in headless mode. Where are you getting the error for edit mode change operator? Recommend using myObj = context.object A new cube may not always have the name "Cube" $\endgroup$
    – batFINGER
    Jul 2, 2019 at 13:55
  • 1
    $\begingroup$ How about you actually clarify this. Post a script with the operator that doesn't work. Post the command line you are using to call it. I Don't use Django, however do have bpy as a python module on ubuntu. No problems calling operator from python shell $\endgroup$
    – batFINGER
    Jul 3, 2019 at 19:14
  • 1
    $\begingroup$ Thank you batFinger!! By trying your python shell idea i have found a solution to the Problem: I found that in the command window, when I write what you did: >>python >>import bpy >>bpy.ops.object.mode_set(mode='EDIT') it works normally and prints {'FINISHED'} So then i went on to search the cause of the error in where exactly i run my script... $\endgroup$ Jul 4, 2019 at 5:41
  • 1
    $\begingroup$ ...It seems, that the precise location where i do the import bpy is very important. My error was that I put import bpy at the top of my .py (prior to some Django getUserInput function). Therefore i strongly reccomend to others working in Django or other python environments to always use import bpy DIRECTLY before your blender scripting. I have now also tried to run a more advanced script and it works! $\endgroup$ Jul 4, 2019 at 5:41

1 Answer 1

3
+100
$\begingroup$

Some things to check and look out for.

  • Expect context.window, context.screen etc to be None in headless mode.
  • If an operator works in the python console in blender then chances are it needs no special window, screen or region for context. AFAIK the edit mode toggle operator only needs an editable object active.
  • Check the bpy module works in your python shell enter image description here
  • Recommend using myObj = context.object A new cube may not always have the name "Cube" eg Cube in another language
  • The space member of context is context.space_data or from the area as area.spaces.active.

And the answer was, as supplied by the OP

...It seems, that the precise location where i do the import bpy is very important. My error was that I put import bpy at the top of my .py (prior to some Django getUserInput function). Therefore i strongly recommend to others working in Django or other python environments to always use import bpy DIRECTLY before your blender scripting. I have now also tried to run a more advanced script and it works!

$\endgroup$
1
  • $\begingroup$ Interesting! I appreciate the answer, but my script still doesn't work even when moving import bpy to directly before the first call to bpy. $\endgroup$ Jun 22, 2022 at 23:30

You must log in to answer this question.

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