Jump to: navigation, search

Difference between revisions of "Pyhton Scripts"

(BoneMatrixCopy)
(BonesAndVertexgroupsBulkRenaming)
Line 54: Line 54:
  
 
[https://github.com/Blender-Brussels/bpy-bge-library/blob/master/users/frankiezafe/bone_renaming.py script on github]
 
[https://github.com/Blender-Brussels/bpy-bge-library/blob/master/users/frankiezafe/bone_renaming.py 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 )
 +
 +
[https://github.com/Blender-Brussels/bpy-bge-library/blob/master/users/frankiezafe/tanukis/BonesVertexgroupsSync.py script on github]

Revision as of 12:08, 8 April 2016

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