How to apply a pose from the asset browser to my character in Python?

I have made many human characters in Character creator 3 and imported it to Blender 3.4 via the CC/iC Blender tools add-on.
Using the gui, I have saved many Pose Assets in the “Char_Poses” asset library in Asset browser. Now I want to make a Python script to automatically import the chracters (one at a time), change their poses and save the camera images for each pose.
In Python, I am able to import the characters, select the armature and go into pose mode. But for some reason, I am not able to apply the saved Pose Assets.
I have tried this command displayed in the info section

bpy.ops.poselib.apply_pose_asset(blend_factor=1, flipped=False)

but I couldn’t figure out how to select my pose from the asset library. In the official documentation, it just says “Apply the given Pose Action to the rig”.

This is my code for reference:

import bpy
view_layer = bpy.context.view_layer
bpy.ops.object.select_all(action=‘SELECT’)
bpy.ops.object.delete(use_global=True, confirm=True)
bpy.ops.object.select_all(action=‘DESELECT’)
bpy.ops.cc3.importer(filepath=“/home/zoavaidy/CharacterCreator/dataset/cc4_char_0001/BlenderFBX/cc4_char_0001.Fbx”, param=“IMPORT_MORPH”)
obj = bpy.data.objects[‘cc4_char_0001’]
bpy.ops.object.select_all(action=‘DESELECT’)
view_layer.objects.active = obj
bpy.ops.object.select_all(action=‘DESELECT’)
view_layer.objects.active = obj

Can someone help me out?

To apply the pose you can use a context override, because certain operators need their own area, rather than being accessible from any area (eg: 3d view, text editor, etc).

This means that you will have to “borrow” the context from some other area, but you can’t create it from scratch. Important to note therefore, that you will have to let a visible window area, that is the asset browser. Otherwise the line a = [... w.screen.areas ...] fails and you won’t be able to get the result.

import bpy

w = bpy.context.window
a = [a for a in w.screen.areas if a.type == 'FILE_BROWSER']
with bpy.context.temp_override(window=w, area=a[0]):
    bpy.ops.poselib.apply_pose_asset()

I understood this, but how do i select a particular pose from all the saved assets? I want to do it completely with Python. So I won’t be selecting the desired pose from the GUI

I found another post asking the same doubt. It seems there is still no solution for it.

scripting - How can I change the active item in Asset Browser with Python? - Blender Stack Exchange

This must be it:

https://blender.stackexchange.com/questions/255413/apply-a-pose-from-the-pose-library-via-the-api-for-blender-3-0

I was searching for this very thing and found the answers on various different sites (mainly stack exchange) but came across this. This is an issue I’ve had to face also

import bpy

def get_poses():
    
    poses = []
    
    for action in bpy.data.actions:
        if action.asset_data != None:
            poses.append(action.name)
    
    return(poses)        


def apply_pose(action):
    area = [area for window in bpy.context.window_manager.windows for area in window.screen.areas if area.ui_type == 'ASSETS']
    
    bpy.ops.pose.transforms_clear()
    
    w = bpy.context.window
    
    for a in area:
        for s in a.spaces:
            if s.type == 'FILE_BROWSER':
                space = s
        
    
    with bpy.context.temp_override(window=w, area=area[0]):
        space.deselect_all()
        space.activate_asset_by_id(bpy.data.actions[action].id_data)

        bpy.ops.poselib.apply_pose_asset(blend_factor=1)
        
def key_frame_poses():
    
    bpy.ops.anim.keying_set_active_set(type='LocRotScale')
    
    if bpy.context.active_object.type == 'ARMATURE' and bpy.context.mode == 'POSE':
        
        scene = bpy.context.scene
        rig = bpy.context.active_object
        
        action_exists = False
        
        for act in bpy.data.actions:
            if act.name == "Pose Library All Poses":
                action = act
                action_exists = True
            else:
                action_exists = False
        
        if action_exists:
            bpy.data.actions.new(name = "Pose Library All Poses")
        
        rig.animation_data.action = action
    
        pose_asset_names = get_poses()
    
        for i, pose_name in enumerate(pose_asset_names):
    
            scene.frame_set(i + 1)  # Set the current frame
            apply_pose(pose_name)
        
            bpy.ops.anim.keyframe_insert_menu(type='LocRotScale')

key_frame_poses()

My script works in blender 4.0. You might be able to see the snippet I stole from this thread in the code here haha

Useful for anyone else looking to do something similar so I’ll post it here because it came to the top of the search results