0
$\begingroup$

My current code looks like this:

    context = bpy.context.copy()
    
    bpy.context.scene.transform_orientation_slots[0].type = 'GLOBAL' #This has to be kept at global otherwise the parts won't come in at the insertion direction
    bpy.context.scene.tool_settings.use_snap = True
    bpy.context.scene.tool_settings.use_snap_align_rotation = False

    # Iterate through the areas
    for area in bpy.context.screen.areas:
        if area.type in ('VIEW_3D'):
            old_area = area.type
            area.type = 'FILE_BROWSER'
            area.ui_type = 'ASSETS'
            bpy.context.space_data.params.asset_library_ref = 'Assets Library'
            bpy.context.space_data.params.import_type = 'APPEND'
            context['area'] = area
            bpy.ops.screen.area_dupli(context, 'INVOKE_DEFAULT')
            area.type = old_area

    return {'FINISHED'}

So when I make this as a button operator, the first time I press it, it just turns the 3D viewport into the asset manager and throws an error from the bpy.context.space lines saying:

AttributeError: 'NoneType' object has no attribute 'asset_library_ref'

But if I just change it back to the 3D viewport and press it again, it works? I'm curious why it won't work on the first press. I can't seem to find any other way to set the correct asset library or set it to append.

$\endgroup$
0

1 Answer 1

0
$\begingroup$

Someone figured out an answer for my question on another site.

space_data.params isn’t loaded until the area has been redrawn at least once. This normally won’t happen until after your script ends. You should register a timed callback with no delay, which Blender will call as soon as it dispatches your script and has had a chance to redraw.

            import bpy

            asset_library_ref_target = 'Assets Library'
            asset_library_ref_fallback = 'LOCAL'


            def new_window_with_assets():
                context = bpy.context

                current_windows = set(context.window_manager.windows)
                if 'FINISHED' not in bpy.ops.wm.window_new():
                    return

                window, = set(context.window_manager.windows) - current_windows
                area = window.screen.areas[0]
                area.ui_type = 'ASSETS'

                def defer():
                    params = area.spaces.active.params
                    if not params:
                        return 0

                    try:
                        params.asset_library_ref = asset_library_ref_target
                    except TypeError:
                        # If the reference doesn't exist.
                        params.asset_library_ref = asset_library_ref_fallback
                    params.import_type = 'APPEND'

                bpy.app.timers.register(defer)


            if __name__ == "__main__":
                new_window_with_assets()
$\endgroup$
2
  • $\begingroup$ it would also be nice to add the link to give credits to them. $\endgroup$
    – Harry McKenzie
    Commented Jul 30, 2023 at 2:33
  • $\begingroup$ I wasn't sure if linking to other sites was allowed, so I pretty much just copy pasted what he explained. Here is the link $\endgroup$ Commented Jul 31, 2023 at 3:22

You must log in to answer this question.

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