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:
e=e.parent
self.ik_list.append(
IKSolver(target, link, chainLength,
- c[cSetting.ITERATIONS], c.influence))
+ int(c[cSetting.ITERATIONS] * 0.1),
+ c[cSetting.ROTWEIGHT]
+ ))
def __checkConnection(self, b, p):
if Blender.Armature.CONNECTED in b.options:
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):
############################################################
self.__createOneSkinMesh(child)
def write(self, path):
+ print('write')
io=pmd.IO()
io.name=self.name
io.comment="blender export"
io.version=1.0
# 頂点
+ 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.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
+ 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_"):
bone.pos.z=b.pos[1] if not near(b.pos[1], 0) else 0
# IK
+ print('ik')
for ik in self.builder.ik_list:
solver=io.addIK()
solver.index=self.builder.getIndex(ik.target)
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
+ 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)