2
$\begingroup$

According to the docs

bpy.ops.pose.group_select() Select bones in active Bone Group

How can the active bone group be set?

Context:

import bpy
is_debug = True

def debug(msg):
    if is_debug:
        print( "Frame:%d %s" % (bpy.context.scene.frame_current, msg ))         

def find_bone_names_by_group(passedPose, passedGroupName):
    result = []
    for b in passedPose.bones:
        if hasattr( b.bone_group, "name"):
            if b.bone_group.name == passedGroupName:
                result.append(b.bone.name)
    return result

def dump(obj,name="obj"):
   for attr in dir(obj):
       if hasattr( obj, attr ):
           print( "%s.%s = %s" % (name,attr, getattr(obj, attr)))

def assign_pose(rig, bone_group_name, pose_name):

    pl = rig.pose_library
    pm=pl.pose_markers[ pose_name ]
    #dump(pm)
    debug("pose_marker=" +str(pm))
    p = rig.pose

    bones_to_modify = find_bone_names_by_group(p, bone_group_name)
    if len(bones_to_modify ) < 1:
        print("No bones found in group [" + bone_group_name + "].")
        return

    action =  bpy.data.actions[pl.name]
    debug("assign_pose " + pose_name)
    for agrp in action.groups:
        if agrp.name in bones_to_modify:
            i = p.bones.find(agrp.name)
            if i != -1:
                pb = p.bones[i]
                debug("Found bone:" + pb.name )
                # check if group has any keyframes.
                for fc in agrp.channels:
                    tmpValue = fc.evaluate(bpy.context.scene.frame_current)
                    # Determine where to assign this value based upon the data_path.
                    if fc.data_path.find("location") != -1:
                        pb.location[fc.array_index] = tmpValue
                    if fc.data_path.find("rotation_quaternion") != -1:
                        pb.rotation_quaternion[fc.array_index] = tmpValue
                bpy.ops.anim.keyframe_insert(type='BUILTIN_KSI_LocRot',confirm_success=is_debug)  
                #bpy.ops.anim.keyframe_insert(type='DEFAULT', confirm_success=True)  
            else:
                print("Pose bone [" + agrp.name + "] not found.")                      


txt = bpy.data.texts["pose.txt"].as_string()
for line in txt.splitlines():
    if line.find(" ") != -1 and line.find("#") == -1:
        sframe,rig_and_poses= line.split(" ")
        rest=rig_and_poses.split(":")
        print("-"*40)
        rig_name=rest[0]
        poses=rest[1]
        pose_list=poses.split(",")
        bpy.context.scene.frame_set( int( sframe ))
        for assignment in pose_list:
            bone_group_name,pose_name=assignment.split("=")
            rig = bpy.data.objects.get(rig_name)
            if rig == None:
                print("rig name=" + rig_name + " not found.")
                break
            debug("assign rig:%s bone_group:%s, pose:%s" % (rig, bone_group_name, pose_name))
            assign_pose(rig, bone_group_name, pose_name)                 
$\endgroup$
3
$\begingroup$

Example:

import bpy

obj = bpy.data.objects['Armature']
bone_groups = obj.pose.bone_groups

bone_groups.active = bone_groups[string_key]
#or
bone_groups.active_index = new_index
$\endgroup$
1
  • $\begingroup$ The indices relate to the order of the bone groups in the list. In the UI the first element is shown at the top and the last element is shown at the bottom. You will probably want to change the bone group directly via .active in most cases. But sometimes you might want to refer to a given position in that list. In this case you can change the active group via .active_index. The order roughly resembles the creation order. $\endgroup$ – pink vertex Feb 9 '14 at 12:57
3
$\begingroup$

Pose bone groups are accessible via Object.pose.bone_groups, the active group is readable and writeable using the active property:

bone_groups.active

You can actually select the bones of a group without the operator. What we need is the bone group and a way to select bones. The bone group is stored in a pose bone itself:

PoseBone.bone_group

So we iterate over all pose bones, check the bone group and need to select the bones that are in your desired group. PoseBones don't have a .select property, we need to use their corresponding Bones and set the .select = True on them:

ob = bpy.context.object # assuming this is an armature
pb_group = ob.pose.bone_groups['Group'] # the pose bone group we wanna select

for pbone, bone in zip(ob.pose.bones, ob.data.bones):
    if pbone.bone_group == pb_group:
        bone.select = True
        bone.select_head = True
        bone.select_tail = True
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.