0
$\begingroup$

I'm trying to make an addon for blender, but I'm having some difficulty with it I'm trying to use a script to select 2 specific bones with a certain name pattern and then parent the first bone to the second and, for the life of me, I can't seem to get it right

What I have tried doing is using

bpy.ops.object.select_pattern(pattern="Bone.a", extend=False) 

and

bpy.ops.object.select_pattern(pattern="Bone.b", extend=true)

to select the two bones and parent with

bpy.ops.armature.parent_set(type='OFFSET')

No go

The script runs, but no bones are parented. When I simply do the selection part of the script, neither are active bones, but are merely selected.

no active bones

I've also tried this script:

  ## Parent and stretch bones ##
    # Get the active object and enter edit mode
    obj = bpy.context.active_object
    bpy.ops.object.mode_set(mode='EDIT')
    current_bone_num = 0
    
    # Loop through all bones and parent based on name
    for bone in obj.data.edit_bones:
        if "_TWEAKER" not in bone.name: # If not a tweaker bone, find corresponding tweaker bone
            tweaker_name = bone_name + "_TWEAKER"
            tweaker_bone = obj.data.edit_bones.get(tweaker_name)
            if tweaker_bone: # If tweaker bone exists, parent to it with offset
                bone.parent = tweaker_bone
                bone.use_connect = True
        else: # If a tweaker bone, clear parent to avoid duplicate parenting
            bone.parent = None
            bone.use_connect = False

It runs, but no bones get parented. It runs, but nothing happens. For context, bone_name is a defined variable for naming the bone in the UI

My actual naming pattern for naming the bones are as follows:

num_bones = context.scene.num_bones (UI variable input, default is 3)
bone_name = context.scene.bone_name (UI string input, default is "bone")

base_bone = bone_name + "_BI_" + chr(ord('a') + (num_bones-1))
bone_tweaker = base_bone + "_TWEAKER"

Example names it gives me are bone_BI_b and bone_BI_b_TWEAKER

What I'm trying to do is to get the base bone parented to the corresponding tweaker bone that uses the same letter from a - z

$\endgroup$
1
  • $\begingroup$ Thank you Chris I didn't realize I accidently had it marked written as 'https' 🤦‍♀️ $\endgroup$
    – Daitou Ink
    Apr 12, 2023 at 11:12

1 Answer 1

0
$\begingroup$

I was able to figure out the code to use was as follows:

## Parent and stretch bones ##
        
        # switch to edit mode
        bpy.ops.object.mode_set(mode='EDIT')

        # get all the bones in the armature
        obj = bpy.context.active_object
        arm = obj.data
        bone_names = [bone.name for bone in arm.edit_bones]

        # loop through all the bones and parent the ones without TWEAKER to the ones with TWEAKER
        for i in range(len(bone_names)):
            bone_name = bone_names[i]
            if "_TWEAKER" not in bone_name:
                parent_name = bone_name + "_TWEAKER"
                if parent_name in bone_names:
                    arm.edit_bones[bone_name].parent = arm.edit_bones[parent_name]

        # switch back to object mode
        bpy.ops.object.mode_set(mode='OBJECT')
$\endgroup$

You must log in to answer this question.

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