Tooltip: 'Export PMD file for MikuMikuDance.'
"""
__author__= ["ousttrue"]
-__version__= "0.2"
+__version__= "1.0"
__url__=()
__bpydoc__="""
0.1 20100318 first implementation.
0.2 20100519 refactoring. use C extension.
+1.0 20100530 implement, basic features.
"""
import Blender
import os
def sort(self):
self.offsets.sort(lambda l, r: l[0]-r[0])
+ def __str__(self):
+ return "<Morph %s>" % self.name
class IKSolver(object):
__slots__=['target', 'effector', 'length', 'iterations', 'weight']
indices=self.vertexArray.getMappedIndices(index)
for i in indices:
baseMorph.add(i, pos)
- indexRelativeMap[i]=relativeIndex
+ indexRelativeMap[relativeIndex]=i
relativeIndex+=1
break
print(len(baseMorph.offsets))
morph.add(indexRelativeMap[i], offset)
assert(len(morph.offsets)==len(baseMorph.offsets))
+ # sort skinmap
+ if len(self.morphList)>1:
+ original=self.morphList[:]
+ def getIndex(morph):
+ for i, v in enumerate(englishmap.skinMap):
+ if v[0]==morph.name:
+ return i
+ #print(morph)
+ return 0
+ self.morphList.sort(lambda l, r: getIndex(l)-getIndex(r))
+
def __getOrCreateMorph(self, name, type):
for m in self.morphList:
if m.name==name:
class Bone(object):
- __slots__=['index', 'name', 'pos', 'parent_index', 'tail_index', 'type']
- def __init__(self, name, pos):
+ __slots__=['index', 'name', 'ik_index',
+ 'pos', 'tail', 'parent_index', 'tail_index', 'type', 'isConnect']
+ def __init__(self, name, pos, tail):
self.index=-1
self.name=name
- self.pos=[pos.x, pos.y, pos.z]
+ self.pos=pos
+ self.tail=tail
self.parent_index=None
self.tail_index=None
self.type=0
+ self.isConnect=False
+ self.ik_index=0
+
+ def __eq__(self, rhs):
+ return self.index==rhs.index
def __str__(self):
return "<Bone %s %d>" % (self.name, self.type)
self.ik_list=[]
def build(self, armatureObj):
- if armatureObj:
- armature=armatureObj.getData()
- for b in armature.bones.values():
- if b.name=='center':
- # root bone
- bone=Bone(b.name, b.head['ARMATURESPACE'])
- self.__addBone(bone)
- self.__getBone(bone, b)
-
- for b in armature.bones.values():
- if not b.parent and b.name!='center':
- # root bone
- bone=Bone(b.name, b.head['ARMATURESPACE'])
- self.__addBone(bone)
- self.__getBone(bone, b)
+ if not armatureObj:
+ return
+ print("gather bones")
+ armature=armatureObj.getData()
+ for b in armature.bones.values():
+ if b.name=='center':
+ # root bone
+ bone=Bone(b.name,
+ b.head['ARMATURESPACE'][0:3],
+ b.tail['ARMATURESPACE'][0:3])
+ self.__addBone(bone)
+ self.__getBone(bone, b)
+
+ for b in armature.bones.values():
+ if not b.parent and b.name!='center':
+ # root bone
+ bone=Bone(b.name,
+ b.head['ARMATURESPACE'][0:3],
+ b.tail['ARMATURESPACE'][0:3])
+ self.__addBone(bone)
+ self.__getBone(bone, b)
+
+ print("check connection")
+ for b in armature.bones.values():
+ if not b.parent:
+ self.__checkConnection(b, None)
+
+ print("gather ik")
pose = armatureObj.getPose()
cSetting=Blender.Constraint.Settings
for b in pose.bones.values():
for c in b.constraints:
if c.type==Blender.Constraint.Type.IKSOLVER:
####################
+ # IK target
+ ####################
+ assert(c[cSetting.TARGET]==armatureObj)
+ target=self.__boneByName(
+ c[Blender.Constraint.Settings.BONE])
+ target.type=2
+
+ ####################
# IK effector
####################
# IK 接続先
link=self.__boneByName(b.name)
- link_tail=self.bones[link.tail_index]
- if link_tail.type==7:
- # replace ...先 to IK接続先
- link_tail.type=6
+ link.type=6
# IK chain
- e=b
+ e=b.parent
chainLength=c[cSetting.CHAINLEN]
for i in range(chainLength):
# IK影響下
- self.__boneByName(e.name).type=4
+ chainBone=self.__boneByName(e.name)
+ chainBone.type=4
+ chainBone.ik_index=target.index
e=e.parent
+ self.ik_list.append(
+ IKSolver(target, link, chainLength,
+ int(c[cSetting.ITERATIONS] * 0.1),
+ c[cSetting.ROTWEIGHT]
+ ))
- ####################
- # IK target
- ####################
- assert(c[cSetting.TARGET]==armatureObj)
- target=self.__boneByName(
- c[Blender.Constraint.Settings.BONE])
- target.type=2
+ def __checkConnection(self, b, p):
+ if Blender.Armature.CONNECTED in b.options:
+ parent=self.__boneByName(p.name)
+ parent.isConnect=True
- self.ik_list.append(
- IKSolver(target, link_tail, chainLength,
- c[cSetting.ITERATIONS], c.influence))
- # 非末端IK
- target_tail=self.bones[target.tail_index]
- if target_tail.type!=7:
- target_bone=armature.bones[target.name]
- bone=Bone(target.name+'_t',
- target_bone.tail['ARMATURESPACE'])
- bone.type=7
- self.__addBone(bone)
- bone.parent_index=target.index
- target.tail_index=bone.index
+ for c in b.children:
+ self.__checkConnection(c, b)
+
+ def sortBy(self, boneMap):
+ """
+ boneMap順に並べ替える
+ """
+ original=self.bones[:]
+ def getIndex(bone):
+ for i, k_v in enumerate(boneMap):
+ if k_v[0]==bone.name:
+ return i
+ print(bone)
+
+ self.bones.sort(lambda l, r: getIndex(l)-getIndex(r))
+ sortMap={}
+ for i, b in enumerate(self.bones):
+ src=original.index(b)
+ sortMap[src]=i
+ for b in self.bones:
+ b.index=sortMap[b.index]
+ if b.parent_index:
+ b.parent_index=sortMap[b.parent_index]
+ if b.tail_index:
+ b.tail_index=sortMap[b.tail_index]
+ if b.ik_index>0:
+ b.ik_index=sortMap[b.ik_index]
def getIndex(self, bone):
for i, b in enumerate(self.bones):
return i
assert(false)
+ def indexByName(self, name):
+ return self.getIndex(self.__boneByName(name))
+
def __boneByName(self, name):
return self.bones[self.boneMap[name]]
def __getBone(self, parent, b):
- if not Blender.Armature.CONNECTED in b.options:
- #print b, b.options
- pass
-
if len(b.children)==0:
- # 末端の非表示ボーン
- bone=Bone(b.name+'_t', b.tail['ARMATURESPACE'])
- bone.type=7
- self.__addBone(bone)
- assert(parent)
- bone.parent_index=parent.index
- parent.tail_index=bone.index
+ parent.type=7
return
for i, c in enumerate(b.children):
- bone=Bone(c.name, c.head['ARMATURESPACE'])
+ bone=Bone(c.name,
+ c.head['ARMATURESPACE'][0:3],
+ c.tail['ARMATURESPACE'][0:3])
self.__addBone(bone)
if parent:
bone.parent_index=parent.index
print(self.oneSkinMesh)
self.name=root.o.name
+ # skeleton
+ self.builder=BoneBuilder()
+ self.builder.build(self.armatureObj)
+ self.builder.sortBy(englishmap.boneMap)
+ def getIndex(ik):
+ for i, v in enumerate(englishmap.boneMap):
+ if v[0]==ik.target.name:
+ return i
+ return len(englishmap.boneMap)
+ self.builder.ik_list.sort(lambda l, r: getIndex(l)-getIndex(r))
+
def __createOneSkinMesh(self, node):
############################################################
# search armature modifier
self.__createOneSkinMesh(child)
def write(self, path):
+ print('write')
io=pmd.IO()
io.name=self.name
io.comment="blender export"
io.version=1.0
- # skeleton
- builder=BoneBuilder()
- builder.build(self.armatureObj)
-
# 頂点
+ print('vertices')
for pos, normal, uv, b0, b1, weight in self.oneSkinMesh.vertexArray.zip():
# convert right-handed z-up to left-handed y-up
v=io.addVertex()
v.normal.z=normal[1]
v.uv.x=uv[0]
v.uv.y=uv[1]
- v.bone0=builder.boneMap[b0] if b0 in builder.boneMap else 0
- v.bone1=builder.boneMap[b1] if b1 in builder.boneMap else 0
+ v.bone0=self.builder.boneMap[b0] if b0 in self.builder.boneMap else 0
+ v.bone1=self.builder.boneMap[b1] if b1 in self.builder.boneMap else 0
v.weight0=int(100*weight)
v.edge_flag=0 # edge flag, 0: enable edge, 1: not edge
# 面とマテリアル
+ print('faces and materials')
vertexCount=self.oneSkinMesh.getVertexCount()
for m, indices in self.oneSkinMesh.vertexArray.indexArrays.items():
m=Blender.Material.Get(m)
#io.indices.append(indices[i+2])
# bones
- for b in builder.bones:
+ print('bones')
+ for b in self.builder.bones:
+ if b.name.endswith("_t"):
+ if b.name.startswith("arm twist1_") or b.name.startswith("arm twist2_"):
+ # skip
+ print "skip %s" % b.name
+ continue
+
bone=io.addBone()
- unicode=englishmap.getUnicodeBoneName(b.name)
- if unicode:
- cp932=unicode.encode('cp932')
- else:
- cp932=b.name
- bone_name="%s\n" % cp932
+ v=englishmap.getUnicodeBoneName(b.name)
+ assert(v)
+ cp932=v[1].encode('cp932')
+ bone_name="%s" % cp932
assert(len(bone_name)<20)
bone.name=bone_name
- bone_english_name="%s\n" % b.name
+ bone_english_name="%s" % b.name
assert(len(bone_english_name)<20)
bone.english_name=bone_english_name
- bone.type=b.type
+ if len(v)>=3:
+ # has type
+ if v[2]==5:
+ b.ik_index=self.builder.indexByName('eyes')
+ bone.type=v[2]
+ else:
+ bone.type=b.type
+
+ # parent index
bone.parent_index=b.parent_index if b.parent_index!=None else 0xFFFF
- bone.tail_index=b.tail_index if b.tail_index!=None else 0
- # ToDo
- bone.ik_index=0xFFFF
+
+ # tail index
+ if b.tail_index!=None:
+ if bone.type==9:
+ bone.tail_index=0
+ else:
+ bone.tail_index=b.tail_index
+ else:
+ bone.tail_index=0
+
+ bone.ik_index=b.ik_index
+
# convert right-handed z-up to left-handed y-up
- bone.pos.x=b.pos[0]
- bone.pos.y=b.pos[2]
- bone.pos.z=b.pos[1]
+ bone.pos.x=b.pos[0] if not near(b.pos[0], 0) else 0
+ bone.pos.y=b.pos[2] if not near(b.pos[2], 0) else 0
+ bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
# IK
- for ik in builder.ik_list:
+ print('ik')
+ for ik in self.builder.ik_list:
solver=io.addIK()
- solver.index=builder.getIndex(ik.target)
- solver.target=builder.getIndex(ik.effector)
+ solver.index=self.builder.getIndex(ik.target)
+ solver.target=self.builder.getIndex(ik.effector)
solver.length=ik.length
- b=builder.bones[ik.effector.parent_index]
+ b=self.builder.bones[ik.effector.parent_index]
for i in xrange(solver.length):
- solver.children.append(builder.getIndex(b))
- b=builder.bones[b.parent_index]
+ solver.children.append(self.builder.getIndex(b))
+ b=self.builder.bones[b.parent_index]
solver.iterations=ik.iterations
solver.weight=ik.weight
# 表情
+ print('shape keys')
for i, m in enumerate(self.oneSkinMesh.morphList):
# morph
morph=io.addMorph()
- unicode=englishmap.getUnicodeSkinName(m.name)
- if unicode:
- cp932=unicode.encode('cp932')
- else:
- cp932=m.name
+ v=englishmap.getUnicodeSkinName(m.name)
+ assert(v)
+ cp932=v[1].encode('cp932')
morph.name="%s\n" % cp932
morph.english_name="%s\n" % m.name
- morph.type=m.type
+ m.type=v[2]
+ morph.type=v[2]
for index, offset in m.offsets:
# convert right-handed z-up to left-handed y-up
morph.append(index, offset[0], offset[2], offset[1])
morph.vertex_count=len(m.offsets)
- # 表情枠
- if i>0:
+ # 表情枠
+ print('display list')
+ # type==0はbase
+ for i, m in enumerate(self.oneSkinMesh.morphList):
+ if m.type==3:
+ io.face_list.append(i)
+ for i, m in enumerate(self.oneSkinMesh.morphList):
+ if m.type==2:
+ io.face_list.append(i)
+ for i, m in enumerate(self.oneSkinMesh.morphList):
+ if m.type==1:
+ io.face_list.append(i)
+ for i, m in enumerate(self.oneSkinMesh.morphList):
+ if m.type==4:
io.face_list.append(i)
# ボーン表示枠
- boneDisplayName=io.addBoneDisplayName()
- boneDisplayName.name="bones\n"
- boneDisplayName.english_name="bones\n"
- displayIndex=1
- for i, b in enumerate(builder.bones):
+ print('bone display list')
+ def createBoneDisplayName(name, english):
+ boneDisplayName=io.addBoneDisplayName()
+ boneDisplayName.name=name.decode('utf-8').encode('cp932')
+ boneDisplayName.english_name=english
+ boneDisplayName=createBoneDisplayName("IK\n", "IK\n")
+ boneDisplayName=createBoneDisplayName("体(上)\n", "Body[u]\n")
+ boneDisplayName=createBoneDisplayName("髪\n", "Hair\n")
+ boneDisplayName=createBoneDisplayName("腕\n", "Arms\n")
+ boneDisplayName=createBoneDisplayName("指\n", "Fingers\n")
+ boneDisplayName=createBoneDisplayName("体(下)\n", "Body[l]\n")
+ boneDisplayName=createBoneDisplayName("足\n", "Legs\n")
+ for i, b in enumerate(self.builder.bones):
+ if i==0:
+ continue
if b.type in [6, 7]:
- io.addBoneDisplay(i, displayIndex)
+ continue
+ io.addBoneDisplay(i, getBoneDisplayGroup(b))
# English
+ print('english')
io.english_name="blender export"
io.english_coment="blender export"
io.getToonTexture(i).name="toon%02d.bmp\n" % i
# 書き込み
- return io.write(path.encode(FS_ENCODING))
-
+ return io.write(path)
+
+
+def getBoneDisplayGroup(bone):
+ boneGroups=[
+ [ # IK
+ "necktie IK", "hair IK_L", "hair IK_R", "leg IK_L", "leg IK_R",
+ "toe IK_L", "toe IK_R",
+ ],
+ [ # 体(上)
+ "upper body", "neck", "head", "eye_L", "eye_R",
+ "necktie1", "necktie2", "necktie3", "eyes",
+ "eyelight_L", "eyelight_R",
+ ],
+ [ # 髪
+ "front hair1", "front hair2", "front hair3",
+ "hair1_L", "hair2_L", "hair3_L",
+ "hair4_L", "hair5_L", "hair6_L",
+ "hair1_R", "hair2_R", "hair3_R",
+ "hair4_R", "hair5_R", "hair6_R",
+ ],
+ [ # 腕
+ "shoulder_L", "arm_L", "arm twist_L", "elbow_L",
+ "wrist twist_L", "wrist_L", "sleeve_L",
+ "shoulder_R", "arm_R", "arm twist_R", "elbow_R",
+ "wrist twist_R", "wrist_R", "sleeve_R",
+ ],
+ [ # 指
+ "thumb1_L", "thumb2_L", "fore1_L", "fore2_L", "fore3_L",
+ "middle1_L", "middle2_L", "middle3_L",
+ "third1_L", "third2_L", "third3_L",
+ "little1_L", "little2_L", "little3_L",
+ "thumb1_R", "thumb2_R", "fore1_R", "fore2_R", "fore3_R",
+ "middle1_R", "middle2_R", "middle3_R",
+ "third1_R", "third2_R", "third3_R",
+ "little1_R", "little2_R", "little3_R",
+ ],
+ [ # 体(下)
+ "lower body", "waist accessory",
+ "front skirt_L", "back skirt_L",
+ "front skirt_R", "back skirt_R",
+ ],
+ [ # 足
+ "leg_L", "knee_L", "ankle_L",
+ "leg_R", "knee_R", "ankle_R",
+ ],
+ ]
+ index=1
+ for g in boneGroups:
+ if bone.name in g:
+ return index
+ index+=1
+ print(bone)
+ return -1
def export_pmd(filename):
filename=filename.decode(INTERNAL_ENCODING)