Add a Cube when registering Add-on
Worked:
- Normal blender startup
- Reload event
- Re/enable add-on
Failed
For directly open a blend file to start the blender, please use
bpy.app.handlers.version_update.append(..)
def register():
# register_class(...)
bpy.app.timers.register(test_fn)
def test_fn(): # make sure your start up file have a 3D viewport
area = next(a for a in bpy.context.screen.areas if a.type == "VIEW_3D")
region = next(r for r in area.regions if r.type == "WINDOW")
with bpy.context.temp_override(area=area, region=region):
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
# or test your ops
Valid for both cases
def register():
# register_class(...)
bpy.app.handlers.version_update.append(after_scene_code)
bpy.app.timers.register(after_scene_code)
def after_scene_code():
if after_scene_code.is_register is True:
return
after_scene_code.is_register = True
test_fn()
after_scene_code.is_register = False
def unregister():
after_scene_code.is_register = False
if after_scene_code in bpy.app.handlers.version_update:
bpy.app.handlers.version_update.remove(after_scene_code)
def test_fn(): # make sure your start up file have a 3D viewport
area = next(a for a in bpy.context.screen.areas if a.type == "VIEW_3D")
region = next(r for r in area.regions if r.type == "WINDOW")
with bpy.context.temp_override(area=area, region=region):
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
# or test your ops