3 ========================
4 MikuMikuDance PMD format
5 ========================
9 * http://blog.goo.ne.jp/torisu_tetosuki/e/209ad341d3ece2b1b4df24abf619d6e4
13 * textencoding: bytes(cp932)
14 * coordinate: left handed y-up(DirectX)
36 weight0: bone0 influence
39 __slots__=['pos', 'normal', 'uv', 'bone0', 'bone1', 'weight0', 'edge_flag']
40 def __init__(self, pos, normal, uv,
41 bone0, bone1, weight0, edge_flag):
48 self.edge_flag=edge_flag
51 return "<%s %s %s, (%d, %d, %d)>" % (
55 self.bone0, self.bone1, self.weight0)
57 def __eq__(self, rhs):
60 and self.normal==rhs.normal
62 and self.bone0==rhs.bone0
63 and self.bone1==rhs.bone1
64 and self.weight0==rhs.weight0
65 and self.edge_flag==rhs.edge_flag
68 def __getitem__(self, key):
79 class Material(object):
80 """pmd material struct.
85 specular_factor: float
90 vertex_count: indices length
91 texture_file: texture file path
94 'diffuse_color', 'alpha',
95 'specular_factor', 'specular_color', 'ambient_color',
96 'toon_index', 'edge_flag',
97 'vertex_count', 'texture_file',
99 def __init__(self, diffuse_color, alpha,
100 specular_factor, specular_color, ambient_color,
101 toon_index, edge_flag, vertex_count, texture_file):
102 self.diffuse_color=diffuse_color
104 self.specular_factor=specular_factor
105 self.specular_color=specular_color
106 self.ambient_color=ambient_color
107 self.toon_index=toon_index
108 self.edge_flag=edge_flag
109 self.vertex_count=vertex_count
110 self.texture_file=texture_file
113 return "<Material [%f, %f, %f, %f]>" % (
114 self.diffuse[0], self.diffuse[1],
115 self.diffuse[2], self.diffuse[3],
118 def __eq__(self, rhs):
120 self.diffuse_color==rhs.diffuse_color
121 and self.alpha==rhs.alpha
122 and self.specular_factor==rhs.specular_factor
123 and self.specular_color==rhs.specular_color
124 and self.ambient_color==rhs.ambient_color
125 and self.toon_index==rhs.toon_index
126 and self.edge_flag==rhs.edge_flag
127 and self.vertex_count==rhs.vertex_count
128 and self.texture_file==rhs.texture_file
133 """pmd material struct.
161 __slots__=['name', 'index', 'type', 'parent', 'ik', 'pos',
162 'children', 'english_name', 'ik_index',
163 'parent_index', 'tail_index', 'tail',
165 def __init__(self, name=b'bone', type=0):
169 self.parent_index=0xFFFF
171 self.tail=common.Vector3(0, 0, 0)
174 self.pos=common.Vector3(0, 0, 0)
178 def __eq__(self, rhs):
181 and self.index==rhs.index
182 and self.type==rhs.type
183 and self.parent_index==rhs.parent_index
184 and self.tail_index==rhs.tail_index
185 and self.tail==rhs.tail
186 and self.ik_index==rhs.ik_index
187 and self.pos==rhs.pos
188 and self.children==rhs.children
189 and self.english_name==rhs.english_name
193 return self.parent_index!=0xFFFF
196 return self.tail_index!=0
198 def display(self, indent=[]):
201 for i, is_end in enumerate(indent):
205 prefix+=' ' if is_end else ' |'
206 uni='%s +%s(%s)' % (prefix, unicode(self), self.english_name)
207 print(uni.encode(ENCODING))
209 uni='%s(%s)' % (unicode(self), self.english_name)
210 print(uni.encode(ENCODING))
212 child_count=len(self.children)
213 for i in range(child_count):
214 child=self.children[i]
216 child.display(indent+[False])
219 child.display(indent+[True])
222 class Bone_Rotate(Bone):
224 def __init__(self, name):
225 super(Bone_Rotate, self).__init__(name, 0)
227 return '<ROTATE %s>' % (self.name)
229 class Bone_RotateMove(Bone):
231 def __init__(self, name):
232 super(Bone_RotateMove, self).__init__(name, 1)
234 return '<ROTATE_MOVE %s>' % (self.name)
238 def __init__(self, name):
239 super(Bone_IK, self).__init__(name, 2)
241 return '<IK %s>' % (self.name)
243 class Bone_IKRotateInfl(Bone):
245 def __init__(self, name):
246 super(Bone_IKRotateInfl, self).__init__(name, 4)
248 return '<IK_ROTATE_INFL %s>' % (self.name)
250 class Bone_RotateInfl(Bone):
252 def __init__(self, name):
253 super(Bone_RotateInfl, self).__init__(name, 5)
255 return '<ROTATE_INFL %s>' % (self.name)
257 class Bone_IKTarget(Bone):
259 def __init__(self, name):
260 super(Bone_IKTarget, self).__init__(name, 6)
262 return '<IK_TARGET %s>' % (self.name)
264 class Bone_Unvisible(Bone):
266 def __init__(self, name):
267 super(Bone_Unvisible, self).__init__(name, 7)
269 return '<UNVISIBLE %s>' % (self.name)
271 class Bone_Rolling(Bone):
273 def __init__(self, name):
274 super(Bone_Rolling, self).__init__(name, 8)
276 return '<ROLLING %s>' % (self.name)
278 class Bone_Tweak(Bone):
280 def __init__(self, name):
281 super(Bone_Tweak, self).__init__(name, 9)
283 return '<TWEAK %s>' % (self.name)
286 def createBone(name, type):
288 return Bone_Rotate(name)
290 return Bone_RotateMove(name)
294 raise Exception("no used bone type: 3(%s)" % name)
296 return Bone_IKRotateInfl(name)
298 return Bone_RotateInfl(name)
300 return Bone_IKTarget(name)
302 return Bone_Unvisible(name)
304 return Bone_Rolling(name)
306 return Bone_Tweak(name)
308 raise Exception("unknown bone type: %d(%s)", type, name)
312 __slots__=['index', 'target', 'iterations', 'weight', 'length', 'children']
313 def __init__(self, index=0, target=0):
321 return "<IK index: %d, target: %d, iterations: %d, weight: %f, children: %s(%d)>" %(self.index, self.target, self.iterations, self.weight, '-'.join([str(i) for i in self.children]), len(self.children))
323 def __eq__(self, rhs):
325 self.index==rhs.index
326 and self.target==rhs.target
327 and self.iterations==rhs.iterations
328 and self.weight==rhs.weight
329 and self.children==rhs.children
334 __slots__=['name', 'type', 'indices', 'pos_list', 'english_name',
336 def __init__(self, name):
344 def append(self, index, x, y, z):
345 self.indices.append(index)
346 self.pos_list.append(common.Vector3(x, y, z))
349 return '<Skin name: "%s", type: %d, vertex: %d>' % (
350 self.name, self.type, len(self.indices))
352 def __eq__(self, rhs):
355 and self.type==rhs.type
356 and self.indices==rhs.indices
357 and self.pos_list==rhs.pos_list
358 and self.english_name==rhs.english_name
359 and self.vertex_count==rhs.vertex_count
363 class BoneGroup(object):
364 __slots__=['name', 'english_name']
365 def __init__(self, name=b'group', english_name=b'center'):
367 self.english_name=english_name
369 def __eq__(self, rhs):
370 return self.name==rhs.name and self.english_name==rhs.english_name
377 RIGIDBODY_KINEMATICS=0
379 RIGIDBODY_PHYSICS_WITH_BONE=2
382 class RigidBody(object):
386 'no_collision_group',
398 def __init__(self, name,
409 shape_size=common.Vector3(),
410 shape_position=common.Vector3(),
411 shape_rotation=common.Vector3()
414 self.bone_index=bone_index
415 self.collision_group=collision_group
416 self.no_collision_group=no_collision_group
417 self.shape_type=shape_type
418 self.shape_size=shape_size
419 self.shape_position=shape_position
420 self.shape_rotation=shape_rotation
422 self.linear_damping=linear_damping
423 self.angular_damping=angular_damping
424 self.restitution=restitution
425 self.friction=friction
428 def __eq__(self, rhs):
431 and self.bone_index==rhs.bone_index
432 and self.collision_group==rhs.collision_group
433 and self.no_collision_group==rhs.no_collision_group
434 and self.shape_type==rhs.shape_type
435 and self.shape_size==rhs.shape_size
436 and self.shape_position==rhs.shape_position
437 and self.shape_rotation==rhs.shape_rotation
438 and self.mass==rhs.mass
439 and self.linear_damping==rhs.linear_damping
440 and self.angular_damping==rhs.angular_damping
441 and self.restitution==rhs.restitution
442 and self.friction==rhs.friction
443 and self.mode==rhs.mode
448 __slots__=[ 'name', 'rigidbody_index_a', 'rigidbody_index_b',
449 'position', 'rotation',
450 'translation_limit_max', 'translation_limit_min',
451 'rotation_limit_max', 'rotation_limit_min',
452 'spring_constant_translation', 'spring_constant_rotation',
454 def __init__(self, name,
455 rigidbody_index_a, rigidbody_index_b,
457 translation_limit_max, translation_limit_min,
458 rotation_limit_max, rotation_limit_min,
459 spring_constant_translation, spring_constant_rotation
462 self.rigidbody_index_a=rigidbody_index_a
463 self.rigidbody_index_b=rigidbody_index_b
464 self.position=position
465 self.rotation=rotation
466 self.translation_limit_max=translation_limit_max
467 self.translation_limit_min=translation_limit_min
468 self.rotation_limit_max=rotation_limit_max
469 self.rotation_limit_min=rotation_limit_min
470 self.spring_constant_translation=spring_constant_translation
471 self.spring_constant_rotation=spring_constant_rotation
473 def __eq__(self, rhs):
476 and self.rigidbody_index_a==rhs.rigidbody_index_a
477 and self.rigidbody_index_b==rhs.rigidbody_index_b
478 and self.position==rhs.position
479 and self.rotation==rhs.rotation
480 and self.translation_limit_max==rhs.translation_limit_max
481 and self.translation_limit_min==rhs.translation_limit_min
482 and self.rotation_limit_max==rhs.rotation_limit_max
483 and self.rotation_limit_min==rhs.rotation_limit_min
484 and self.spring_constant_translation==rhs.spring_constant_translation
485 and self.spring_constant_rotation==rhs.spring_constant_rotation
497 version: pmd version number
501 'version', 'name', 'comment',
502 'english_name', 'english_comment',
503 'vertices', 'indices', 'materials', 'bones',
505 'morph_indices', 'bone_group_list', 'bone_display_list',
507 'rigidbodies', 'joints',
511 def __init__(self, version):
515 self.english_name=b''
516 self.english_comment=b''
523 self.morph_indices=[]
524 self.bone_group_list=[]
525 self.bone_display_list=[]
527 self.toon_textures=[b'']*10
531 self.no_parent_bones=[]
533 def each_vertex(self): return self.vertices
534 def getUV(self, i): return self.vertices[i].uv
537 return '<pmd-%g, "%s" vertex: %d, face: %d, material: %d, bone: %d ik: %d, skin: %d>' % (
538 self.version, self.name, len(self.vertices), len(self.indices),
539 len(self.materials), len(self.bones), len(self.ik_list), len(self.morph_list))
541 def __eq__(self, rhs):
544 and self.comment==rhs.comment
545 and self.english_name==rhs.english_name
546 and self.english_comment==rhs.english_comment
547 and self.vertices==rhs.vertices
548 and self.indices==rhs.indices
549 and self.materials==rhs.materials
550 and self.bones==rhs.bones
551 and self.ik_list==rhs.ik_list
552 and self.morphs==rhs.morphs
553 and self.morph_indices==rhs.morph_indices
554 and self.bone_group_list==rhs.bone_group_list
555 and self.bone_display_list==rhs.bone_display_list
556 and self.toon_textures==rhs.toon_textures
557 and self.rigidbodies==rhs.rigidbodies
558 and self.joints==rhs.joints
562 if self.name!=rhs.name:
563 print(self.name, rhs.name)
565 if self.comment!=rhs.comment:
566 print(self.comment, rhs.comment)
568 if self.english_name!=rhs.english_name:
569 print(self.english_name, rhs.english_name)
571 if self.english_comment!=rhs.english_comment:
572 print(self.english_comment, rhs.english_comment)
574 if self.vertices!=rhs.vertices:
575 print(self.vertices, rhs.vertices)
577 if self.indices!=rhs.indices:
578 print(self.indices, rhs.indices)
580 if self.materials!=rhs.materials:
581 print(self.materials, rhs.materials)
583 if self.bones!=rhs.bones:
584 print(self.bones, rhs.bones)
586 if self.ik_list!=rhs.ik_list:
587 print(self.ik_list, rhs.ik_list)
589 if self.morphs!=rhs.morphs:
590 print(self.morphs, rhs.morphs)
592 if self.morph_indices!=rhs.morph_indices:
593 print(self.morph_indices, rhs.morph_indices)
595 if self.bone_group_list!=rhs.bone_group_list:
596 print(self.bone_group_list, rhs.bone_group_list)
598 if self.bone_display_list!=rhs.bone_display_list:
599 print(self.bone_display_list, rhs.bone_display_list)
601 if self.toon_textures!=rhs.toon_textures:
602 print(self.toon_textures, rhs.toon_textures)
604 if self.rigidbodies!=rhs.rigidbodies:
605 print(self.rigidbodies, rhs.rigidbodies)
607 if self.joints!=rhs.joints:
608 print(self.joints, rhs.joints)