Simple Python script for a beginner : targeted selection


(LeRan) #1

Hi forum,

Please bear with me, but I don’t speak Python at all and I just need a couple simple scripts for my work with Blender : they should just perform :

  1. select (variant : deselect) all bones of the current object that have a transformation constraint.

  2. enable (variant : disable) all transformation constraints of the current object.

Could you please show me how it’s written ? For the record, I need those things to animate an armature that has its phalanges controlled by one external trigger, and it’s becoming incredibly bothersome to select all 24 phalanges one by one everytime when managing keyframes :confused:


(doakey3) #2

This is an addon that will show up in the left side panel of the 3D View under the tools section. It has 4 buttons that I think will do what you’re looking for.

Just copy the following code and put it into a file called transform_select.py

Then install it by going to file > user preferences > install from file > (navigate to transform_select.py and install it.)


import bpy

bl_info = {
    "name": "Transform Select", 
    "category": "Animation"}
    
class TransformSelectUI(bpy.types.Panel):
    bl_category = "Tools"
    bl_label = "Transform Select"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    
    def draw(self, context):
        scene = context.scene
        layout = self.layout
        
        row = layout.row()
        row.operator('select.transforming_bones')
        row.operator('deselect.transforming_bones')
        
        row = layout.row() 
        row.operator('enable.transforming_bones')
        row.operator('disable.transforming_bones')
        
class SelectTransformingBones(bpy.types.Operator):
    bl_label = "Select"
    bl_idname = "select.transforming_bones"
    bl_description = "Select bones that have a transforms constraint"
    
    @classmethod
    def poll(self, context):
        if context.active_object.type == 'ARMATURE':
            return True
        else:
            return False
    
    def execute(self, context):
        rig = context.active_object
        bones = rig.pose.bones
            
        for bone in bones:
            for constraint in bone.constraints:
                if constraint.type == 'TRANSFORM':
                    context.object.data.bones[bone.name].select = True
        return {'FINISHED'}

class DeselectTransformingBones(bpy.types.Operator):
    bl_label = "Deselect"
    bl_idname = "deselect.transforming_bones"
    bl_description = "Deselect bones that have a transforms constraint"
    
    @classmethod
    def poll(self, context):
        if context.active_object.type == 'ARMATURE':
            return True
        else:
            return False
    
    def execute(self, context):
        rig = context.active_object
        bones = rig.pose.bones
            
        for bone in bones:
            for constraint in bone.constraints:
                if constraint.type == 'TRANSFORM':
                    context.object.data.bones[bone.name].select = False
        return {'FINISHED'}
    
class EnableTransformingBones(bpy.types.Operator):
    bl_label = "Enable"
    bl_idname = "enable.transforming_bones"
    bl_description = "Enable bones that have a transforms constraint"
    
    @classmethod
    def poll(self, context):
        if context.active_object.type == 'ARMATURE':
            return True
        else:
            return False
    
    def execute(self, context):
        rig = context.active_object
        bones = rig.pose.bones
            
        for bone in bones:
            for constraint in bone.constraints:
                if constraint.type == 'TRANSFORM':
                    constraint.influence = 1.0
        return {'FINISHED'}

class EnableTransformingBones(bpy.types.Operator):
    bl_label = "Disable"
    bl_idname = "disable.transforming_bones"
    bl_description = "Disable bones that have a transforms constraint"
    
    @classmethod
    def poll(self, context):
        if context.active_object.type == 'ARMATURE':
            return True
        else:
            return False
    
    def execute(self, context):
        rig = context.active_object
        bones = rig.pose.bones
            
        for bone in bones:
            for constraint in bone.constraints:
                if constraint.type == 'TRANSFORM':
                    constraint.influence = 0.0
        return {'FINISHED'}

def register():
    bpy.utils.register_module(__name__)
    
def unregister():
    bpy.utils.unregister_module(__name__)