Jump to: navigation, search

Pyhton Scripts

Revision as of 16:33, 8 April 2016 by Frankiezafe (Talk | contribs) (BatchBonesModification)

Python scripts vault used during the project.

BoneMatrixCopy

import bpy, math
from mathutils import Vector, Matrix
# configuration ############
ARMATURE_NAME = 'makehuman'
BONE_NAME = 'RightArm'
# getting the right bone, no safety net...
scn = bpy.context.scene
armature = scn.objects[ ARMATURE_NAME ].data
bone = armature.bones[ BONE_NAME ]
# orientation of the bone is represented as a 4x4 matrix
# see https://www.blender.org/api/blender_python_api_2_59_0/bpy.types.Bone.html#bpy.types.Bone.matrix_local
bone_mat = bone.matrix_local
# getting the active object
target = scn.objects.active
# copy of the matrix ############
target.matrix_local = bone_mat
target.rotation_mode = 'QUATERNION'
target.rotation_mode = 'XYZ'
print( 'done, matrix of bone', armature.name, ':', bone.name, 'copied on',  target.name )

script on github

BonesAndVertexgroupsBulkRenaming

import bpy

# right and left markers
NEEDLE = "R_"
REPLACE = "L_"

# name of the armature and the related mesh
ARMATURE_NAME = 'armature_skeleton'
MESH_NAME = 'skeleton'

scn = bpy.context.scene
armature = scn.objects[ ARMATURE_NAME ].data
mesh = scn.objects[ MESH_NAME ]

# renaming all bones
print( "armature:", ARMATURE_NAME, armature )
for b in armature.bones:
   b.name = b.name.replace( NEEDLE, REPLACE )
   print( b.name )
   
# renaming all vertex groups
print( "mesh:", MESH_NAME, mesh )
for vg in mesh.vertex_groups:
   vg.name = vg.name.replace( NEEDLE, REPLACE )
   print( vg.name )

script on github

BonesVertexgroupsSync

import bpy

# name of the armature and the related mesh
ARMATURE_NAME = 'armature_skeleton'
MESH_NAME = 'skeleton'
REMOVE_VERTEXGROUPS = True

scn = bpy.context.scene
armature = scn.objects[ ARMATURE_NAME ].data
mesh = scn.objects[ MESH_NAME ]

print( mesh.vertex_groups )

# creation of missing vertex groups
for b in armature.bones:
   found = False
   for vg in mesh.vertex_groups:
       if vg.name == b.name:
           found = True
   if found == False:
       mesh.vertex_groups.new( b.name )
       print( b.name, "vertex group created" )

# deletion of vertex groups
if REMOVE_VERTEXGROUPS == True:
   for vg in mesh.vertex_groups:
       found = False
       for b in armature.bones:
           if vg.name == b.name:
               found = True
       if found == False:
           print( vg.name, "vertex group removed" )
           mesh.vertex_groups.remove( vg )

script on github

BatchBonesModification

import bpy

# name of the armature
ARMATURE_NAME = 'armature_skeleton'
NAME_FILTER = 'spine'

def doContinue( name ):
   global NAME_FILTER
   if NAME_FILTER == :
       return True
   if name.find( NAME_FILTER ) != -1:
       return True
   return False

scn = bpy.context.scene
armature = scn.objects[ ARMATURE_NAME ]

# modification on bones
# modifiable params are listed in doc
# https://www.blender.org/api/blender_python_api_2_77_release/bpy.types.Bone.html#bpy.types.Bone
print( '**** bones:', len(armature.pose.bones) )
for b in armature.data.bones:
   if not doContinue( b.name ):
       continue
   print( b.name )
   b.use_inherit_rotation = True
   b.use_inherit_scale = False

# modification on pose bones
# modifiable params are listed in doc 
# https://www.blender.org/api/blender_python_api_2_77_release/bpy.types.PoseBone.html
print( '**** pose bones:', len(armature.pose.bones) )
for pb in armature.pose.bones:
   if not doContinue( pb.name ):
       continue
   print( pb.name )
   pb.lock_location = (False,False,False)
   pb.lock_rotation = (False,False,False)
   pb.lock_scale = (False,False,False)

script on github

GlueVertices

script on github