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!!!
context.window, context.screen
etc to beNone
in headless mode. Where are you getting the error for edit mode change operator? Recommend usingmyObj = context.object
A new cube may not always have the name "Cube" $\endgroup$bpy
as a python module on ubuntu. No problems calling operator from python shell $\endgroup$>>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$import bpy
is very important. My error was that I putimport 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 useimport bpy
DIRECTLY before your blender scripting. I have now also tried to run a more advanced script and it works! $\endgroup$