5
$\begingroup$

My initial attempt was to call that function directly from the register part. However, I soon realized that this isn't possible for a function that needs context.

I also tried using a load_post handler, but it appears not to fire with a new Blender session, it only triggers when a new file is loaded into an already open Blender.

My latest attempt involved using a timer to call the function. Unfortunately, even though it was successfully registered, the function was never called.

Does anyone have an idea why the timer isn't calling the function, or what else I could try?

$\endgroup$
1

1 Answer 1

5
$\begingroup$

Add a Cube when registering Add-on

Worked:

  • Normal blender startup
  • Reload event
  • Re/enable add-on

Failed

  • Indirectly open blender

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
$\endgroup$
8
  • 1
    $\begingroup$ Does this also work instead of using a timer? blender.stackexchange.com/questions/320853/… $\endgroup$
    – Harry McKenzie
    Commented Jul 17 at 1:45
  • $\begingroup$ In my case, I use timer method to run a develop Operator when press a button to reload my add-on, instead of bpy.app.handlers Since bpy.app.handlers doesn`t trigger reload event. $\endgroup$
    – X Y
    Commented Jul 17 at 1:46
  • $\begingroup$ @HarryMcKenzie McKenzie Yes this also work $\endgroup$
    – X Y
    Commented Jul 17 at 1:53
  • $\begingroup$ @HarryMcKenzie I tested again and it sometimes doesn't work without bpy.context.override .if your operator check the bpy.context.area, and the value is None. $\endgroup$
    – X Y
    Commented Jul 17 at 2:02
  • 1
    $\begingroup$ version_update run when blender start, load_post run when load a blend file. docs.blender.org/api/current/bpy.app.handlers.html $\endgroup$
    – X Y
    Commented Jul 17 at 2:10

You must log in to answer this question.

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