Skip to content

Instantly share code, notes, and snippets.

@GRGSIBERIA
Created January 26, 2014 18:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GRGSIBERIA/8637323 to your computer and use it in GitHub Desktop.
Save GRGSIBERIA/8637323 to your computer and use it in GitHub Desktop.
ボーンの情報とウェイトの情報をCSVに書き出すMayaスクリプト
#-*- encoding: utf-8
import maya.cmds as cmds
import maya.mel as mel
import csv
import codecs
# ルートボーンが選択されている状態で実行する必要あり
def GetBoneNames(root):
cmds.select(root, hierarchy=True)
return cmds.ls(sl=True)
def GetParentBone(name):
parent = cmds.listRelatives(name, p=True) # 親ボーン名を取得
if parent != None:
return parent[0]
return ""
def ConstructParentHash(bones):
bone_hash = {}
for bone in bones:
bone_hash[bone] = GetParentBone(bone)
return bone_hash
def ConstructPositionHash(bones):
positions = {}
for bone in bones:
# ボーンの座標を取得
positions[bone] = cmds.xform(bone, q=True, ws=True, t=True)
return positions
def GetPolyCount(mesh):
return cmds.polyEvaluate(mesh, v=True)
def BoneWeightMap(skin_cluster, attr_name, names):
weights = {}
for bname in names:
weight = cmds.skinPercent(skin_cluster, attr_name, transform=bname, q=True)
if weight > 0.0 and weight != None:
weights[bname] = weight
return weights
def CreateWeightForCSV(weights, max_influence):
result_weights = []
for k, v in sorted(weights.items()):
result_weights.append([k, v])
length = len(result_weights)
if length > max_influence:
length = max_influence
row = []
for j in range(length):
row.extend(result_weights[j])
return row
def GetSkinCluster(mesh):
history = cmds.listHistory(mesh)
for h in history:
if cmds.objectType(h, isType='skinCluster'):
return h
return None
mesh = cmds.ls(sl=True)[0]
root = cmds.ls(sl=True)[1]
skin_cluster = GetSkinCluster(mesh)
names = GetBoneNames(root)
parents = ConstructParentHash(names)
positions = ConstructPositionHash(names)
with codecs.open('c:/output_bones.csv', 'w', 'shift_jis') as f:
w = csv.writer(f, lineterminator='\r\n', delimiter=',')
for name in names:
row = [name, parents[name]] + positions[name]
w.writerow(row)
poly_count = GetPolyCount(mesh)
with codecs.open('c:/output_weights.csv', 'w', 'shift_jis') as f:
w = csv.writer(f, lineterminator='\r\n', delimiter=',')
for i in range(poly_count):
attr_name = mesh + ".vtx[" + str(i) + "]"
weights = BoneWeightMap(skin_cluster, attr_name, names)
row = CreateWeightForCSV(weights, 2) # 第2引数が1頂点あたりの最大Influence数
w.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment