Thursday, March 16, 2023

doodle

Hi,

this is a simple doodle to help with resetting bones of an armature to their defaults in pose mode of blender 2.79.  it was helpful first writing out the methods and then later putting the methods into a class.  i show below the methods that were written first commented out.  for example it can reset to default all unselected bones (i think that will come in handy when making poses/actions for facial rigging)



import bpy

class PoseResetor(object):
    """class to help with reseting the pose of an armature.
    assumes pose is static non-animated
    """
    def __init__(self,armature):
        self._armature = armature
    def resetAll(self):
        """reset to default all bones
        """
        bones = self.getBonesByMode("ALL")
        self.resetGivenBonesOfArmature(bones)
        
    def resetUnselected(self):
        """reset to default unselected bones
        """
        bones = self.getBonesByMode("UNSELECTED")
        self.resetGivenBonesOfArmature(bones)
    
    def resetGivenBonesOfArmature(self, bones):
        """
        reset the bones of armature
        assumes in pose mode
        @param - bones a list of str bone names 
        """
        
        armature = self._armature
        
        #save current selection - get what is selected
        curSel = []
        curSel = self.getBonesByMode("SELECTED")
        
        #select bones
        self._selectBones(bones)
        bpy.ops.pose.transforms_clear()
    
        #restore selection
        self._selectBones(curSel)
        
    def getBonesByMode(self, mode):
        """
        get bones in armature by mode
        @param mode - 'UNSELECTED', 'SELECTED', 'ALL
        """
        armature = self._armature
        
        result = []
        armatObj = bpy.data.objects[armature]
        numBones = len(armatObj.pose.bones)
        for i in range(0,numBones):
            if mode == 'UNSELECTED':
                if not armatObj.pose.bones[i].bone.select:
                    result.append(armatObj.pose.bones[i].name)
            elif mode == 'SELECTED':
                if armatObj.pose.bones[i].bone.select:
                    result.append(armatObj.pose.bones[i].name)            
            elif mode == 'ALL':
                result.append(armatObj.pose.bones[i].name)
                
        return result
        
    def _selectBones(self, bones):
        """select given bones - overwrites selection
        @param - bones a list of str bone names 
        """ 
        armature = self._armature
        bpy.ops.pose.select_all(action='DESELECT')
        armatObj = bpy.data.objects[armature]
        for bone in bones:
            armatObj.pose.bones[bone].bone.select = True    


#the method way
"""
def resetGivenBonesOfArmature(armature, bones):
    #reset the bones of armature
    #assumes in pose mode
    #save current selection - get what is selected
    curSel = []
    curSel = getBonesByMode(armature, "SELECTED")
    
    #select bones
    _selectBones(armature, bones)
    bpy.ops.pose.transforms_clear()

    #restore selection
    _selectBones(armature, curSel)
    
def getBonesByMode(armature, mode):
    #get bones in armature by mode
    #@param mode - 'UNSELECTED', 'SELECTED'
    
    result = []
    armatObj = bpy.data.objects[armature]
    numBones = len(armatObj.pose.bones)
    for i in range(0,numBones):
        if mode == 'UNSELECTED':
            if not armatObj.pose.bones[i].bone.select:
                result.append(armatObj.pose.bones[i].name)
        elif mode == 'SELECTED':
            if armatObj.pose.bones[i].bone.select:
                result.append(armatObj.pose.bones[i].name)            
            
    return result

    
def _selectBones(armature, bones):
    #overwrites selection
    
    bpy.ops.pose.select_all(action='DESELECT')
    armatObj = bpy.data.objects[armature]
    for bone in bones:
        armatObj.pose.bones[bone].bone.select = True
"""    


"""
import imp
testTool = imp.load_source("tool","/Users/Nathaniel/Documents/src_blender/python/snippets/anim_snippets.py") #change to path to python file

#bones = testTool.getBonesByMode("Armature","UNSELECTED")
#testTool.resetGivenBonesOfArmature("Armature",bones)

#object way - after first wrote methods
pr = testTool.PoseResetor("Armature")
pr.resetAll()
#pr.resetUnselected()
""" 
Happy Sketching!