# Context Problem in Blender 2.80 python module

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.ops.object.select_all(action='DESELECT')
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?

• Please provide some example of how you are running this from command line, where and how you are overriding context. – batFINGER Jul 1 '19 at 13:11
• I now added some additional information and did some corrections. – Robert Roth Jul 2 '19 at 7:24
• 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" – batFINGER Jul 2 '19 at 13:55
• 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 – batFINGER Jul 3 '19 at 19:14
• ...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! – Robert Roth Jul 4 '19 at 5:41

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
• 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!