10 from .pymeshio import pmx
11 from .pymeshio.pmx import reader
14 def convert_coord(pos):
16 Left handed y-up to Right handed z-up
18 return (pos.x, pos.z, pos.y)
21 return bl.createVector(v.x, v.y, v.z)
24 def get_object_name(fmt, index, name):
28 len_list=[len(name[:i].encode('utf-8')) for i in range(1, len(name)+1, 1)]
30 prefix=fmt.format(index)
31 max_length=21-len(prefix)
32 for str_len in len_list:
33 if str_len>max_length:
36 name=prefix+name[:letter_count]
37 print("%s(%d)" % (name, letter_count))
40 def __import_joints(joints, rigidbodies):
41 print("create joints")
42 container=bl.object.createEmpty('Joints')
44 True, False, False, False, False, False, False, False, False, False,
45 False, False, False, False, False, False, False, False, False, False,
47 material=bl.material.create('joint')
48 material.diffuse_color=(1, 0, 0)
50 for i, c in enumerate(joints):
51 bpy.ops.mesh.primitive_uv_sphere_add(
55 location=(c.position.x, c.position.z, c.position.y),
58 meshObject=bl.object.getActive()
59 constraintMeshes.append(meshObject)
60 mesh=bl.object.getData(meshObject)
61 bl.mesh.addMaterial(mesh, material)
62 meshObject.name=get_object_name("j{0:02}:", i, c.name)
63 #meshObject.draw_transparent=True
64 #meshObject.draw_wire=True
65 meshObject.draw_type='SOLID'
67 meshObject.rotation_euler=(-rot.x, -rot.z, -rot.y)
69 meshObject[bl.CONSTRAINT_NAME]=c.name
70 meshObject[bl.CONSTRAINT_A]=rigidbodies[c.rigidbody_index_a].name
71 meshObject[bl.CONSTRAINT_B]=rigidbodies[c.rigidbody_index_b].name
72 meshObject[bl.CONSTRAINT_POS_MIN]=VtoV(c.translation_limit_min)
73 meshObject[bl.CONSTRAINT_POS_MAX]=VtoV(c.translation_limit_max)
74 meshObject[bl.CONSTRAINT_ROT_MIN]=VtoV(c.rotation_limit_min)
75 meshObject[bl.CONSTRAINT_ROT_MAX]=VtoV(c.rotation_limit_max)
76 meshObject[bl.CONSTRAINT_SPRING_POS]=VtoV(c.spring_constant_translation)
77 meshObject[bl.CONSTRAINT_SPRING_ROT]=VtoV(c.spring_constant_rotation)
79 for meshObject in reversed(constraintMeshes):
80 bl.object.makeParent(container, meshObject)
84 def __importRigidBodies(rigidbodies, bones):
85 print("create rigid bodies")
87 container=bl.object.createEmpty('RigidBodies')
89 True, False, False, False, False, False, False, False, False, False,
90 False, False, False, False, False, False, False, False, False, False,
92 material=bl.material.create('rigidBody')
94 for i, rigid in enumerate(rigidbodies):
95 if rigid.bone_index==-1:
99 bone=bones[rigid.bone_index]
100 pos=rigid.shape_position
101 size=rigid.shape_size
103 if rigid.shape_type==0:
104 bpy.ops.mesh.primitive_ico_sphere_add(
105 location=(pos.x, pos.z, pos.y),
108 bpy.ops.transform.resize(
109 value=(size.x, size.x, size.x))
110 elif rigid.shape_type==1:
111 bpy.ops.mesh.primitive_cube_add(
112 location=(pos.x, pos.z, pos.y),
115 bpy.ops.transform.resize(
116 value=(size.x, size.z, size.y))
117 elif rigid.shape_type==2:
118 bpy.ops.mesh.primitive_cylinder_add(
119 location=(pos.x, pos.z, pos.y),
122 bpy.ops.transform.resize(
123 value=(size.x, size.x, size.y))
127 meshObject=bl.object.getActive()
128 mesh=bl.object.getData(meshObject)
129 rigidMeshes.append(meshObject)
130 bl.mesh.addMaterial(mesh, material)
131 meshObject.name=get_object_name("r{0:02}:", i, rigid.name)
132 #meshObject.draw_transparent=True
133 #meshObject.draw_wire=True
134 meshObject.draw_type='WIRE'
135 rot=rigid.shape_rotation
136 meshObject.rotation_euler=(-rot.x, -rot.z, -rot.y)
138 meshObject[bl.RIGID_NAME]=rigid.name
139 meshObject[bl.RIGID_SHAPE_TYPE]=rigid.shape_type
140 meshObject[bl.RIGID_PROCESS_TYPE]=rigid.mode
141 meshObject[bl.RIGID_BONE_NAME]=bone.name
142 meshObject[bl.RIGID_GROUP]=rigid.collision_group
143 meshObject[bl.RIGID_INTERSECTION_GROUP]=rigid.no_collision_group
144 meshObject[bl.RIGID_WEIGHT]=rigid.param.mass
145 meshObject[bl.RIGID_LINEAR_DAMPING]=rigid.param.linear_damping
146 meshObject[bl.RIGID_ANGULAR_DAMPING]=rigid.param.angular_damping
147 meshObject[bl.RIGID_RESTITUTION]=rigid.param.restitution
148 meshObject[bl.RIGID_FRICTION]=rigid.param.friction
150 for meshObject in reversed(rigidMeshes):
151 bl.object.makeParent(container, meshObject)
155 def __create_a_material(m, name, textures_and_images):
161 pymeshio.pmx.Material
165 list of (texture, image)
167 material = bl.material.create(name)
169 material.diffuse_shader='FRESNEL'
170 material.diffuse_color=[m.diffuse_color.r, m.diffuse_color.g, m.diffuse_color.b]
171 material.alpha=m.alpha
173 material.specular_shader='TOON'
174 material.specular_color=[m.specular_color.r, m.specular_color.g, m.specular_color.b]
175 material.specular_toon_size=int(m.specular_factor)
177 material.mirror_color=[m.ambient_color.r, m.ambient_color.g, m.ambient_color.b]
183 material.preview_render_type='FLAT'
184 material.use_transparency=True
186 if m.texture_index!=-1:
187 bl.material.addTexture(material, textures_and_images[m.texture_index][0])
190 def __create_armature(bones, display_slots):
196 list of pymeshio.pmx.Bone
198 armature, armature_object=bl.armature.create()
201 bl.armature.makeEditable(armature_object)
203 bone=bl.armature.createBone(armature, b.name)
205 bone.head=bl.createVector(*convert_coord(b.position))
206 if not b.getConnectionFlag():
207 bone.tail=bl.createVector(*convert_coord(b.position))
208 elif not b.getVisibleFlag():
209 bone.tail=bone.head+bl.createVector(0, 1, 0)
212 bl_bones=[create_bone(b) for b in bones]
215 for b, bone in zip(bones, bl_bones):
216 assert(b.name==bone.name)
217 if b.parent_index!=-1:
218 #print("%s -> %s" % (bones[b.parent_index].name, b.name))
219 parent_bone=bl_bones[b.parent_index]
220 bone.parent=parent_bone
221 if b.getConnectionFlag() and b.tail_index!=-1:
222 assert(b.tail_index!=0)
223 tail_bone=bl_bones[b.tail_index]
224 bone.tail=tail_bone.head
225 bl.bone.setConnected(tail_bone)
227 print("no parent %s" % b.name)
228 bl.armature.update(armature)
230 # create ik constraint
232 pose = bl.object.getPose(armature_object)
233 for b, bone in zip(bones, bl_bones):
236 assert(len(ik.link)<16)
237 p_bone=pose.bones[bones[ik.target_index].name]
239 constraint=bl.armature.createIkConstraint(
240 armature_object, p_bone, bone.name,
241 ik.link, ik.limit_radian, ik.loop)
242 bl.armature.makeEditable(armature_object)
243 bl.armature.update(armature)
247 pose = bl.object.getPose(armature_object)
248 for i, ds in enumerate(display_slots):
250 g=bl.object.createBoneGroup(armature_object, ds.name, "THEME%02d" % (i+1))
251 for t, index in ds.references:
253 name=bones[index].name
255 pose.bones[name].bone_group=g
256 except KeyError as e:
257 print("pose %s is not found" % name)
260 return armature_object
262 def _execute(filepath):
268 model=reader.read_from_file(filepath)
270 print("fail to load %s" % filepath)
274 # メッシュをまとめるエンプティオブジェクト
275 model_name=model.english_name
276 if len(model_name)==0:
277 model_name=os.path.basename(filepath)
278 root_object=bl.object.createEmpty(model_name)
279 root_object[bl.MMD_MB_NAME]=model.name
280 root_object[bl.MMD_MB_COMMENT]=model.comment
281 root_object[bl.MMD_COMMENT]=model.english_comment
284 armature_object=__create_armature(model.bones, model.display_slots)
286 bl.object.makeParent(root_object, armature_object)
289 texture_dir=os.path.dirname(filepath)
290 textures_and_images=[bl.texture.create(os.path.join(texture_dir, t))
291 for t in model.textures]
292 print(textures_and_images)
294 index_generator=(i for i in model.indices)
295 # 頂点配列。(Left handed y-up) to (Right handed z-up)
296 vertices=[convert_coord(pos)
297 for pos in (v.position for v in model.vertices)]
299 for i, m in enumerate(model.materials):
303 material=__create_a_material(m, m.name, textures_and_images)
308 # object名はutf-8で21byteまで
309 mesh, mesh_object=bl.mesh.create(get_object_name("{0:02}:", i, m.name))
310 bl.mesh.addMaterial(mesh, material)
312 bl.object.deselectAll()
313 bl.object.activate(mesh_object)
314 bl.object.makeParent(root_object, mesh_object)
319 indices=[next(index_generator)
320 for _ in range(m.vertex_count)]
321 used_indices=set(indices)
322 bl.mesh.addGeometry(mesh, vertices,
323 [(indices[i], indices[i+1], indices[i+2])
324 for i in range(0, len(indices), 3)])
325 assert(len(model.vertices), len(mesh.vertices))
329 hasTexture=bl.material.hasTexture(material)
331 index_gen=(i for i in indices)
332 image=(textures_and_images.get[m.texture_index]
333 if m.texture_index in textures_and_images
335 for i, face in enumerate(mesh.faces):
336 bl.face.setMaterial(face, 0)
338 uv0=model.vertices[next(index_gen)].uv
339 uv1=model.vertices[next(index_gen)].uv
340 uv2=model.vertices[next(index_gen)].uv
341 bl.mesh.setFaceUV(mesh, i, face, [# fix uv
353 bl.modifier.addArmature(mesh_object, armature_object)
354 # set vertex attributes(normal, bone weights)
355 bl.mesh.useVertexUV(mesh)
356 for i, (v, mvert) in enumerate(zip(model.vertices, mesh.vertices)):
357 bl.vertex.setNormal(mvert, convert_coord(v.normal))
358 if isinstance(v.deform, pmx.Bdef1):
359 bl.object.assignVertexGroup(mesh_object,
360 model.bones[v.deform.index0].name, i, 1.0)
361 elif isinstance(v.deform, pmx.Bdef2):
362 bl.object.assignVertexGroup(mesh_object,
363 model.bones[v.deform.index0].name, i, v.deform.weight0)
364 bl.object.assignVertexGroup(mesh_object,
365 model.bones[v.deform.index1].name, i, 1.0-v.deform.weight0)
367 raise Exception("unknown deform: %s" % v.deform)
373 bl.object.pinShape(mesh_object, True)
375 baseShapeBlock=bl.object.addShapeKey(mesh_object, bl.BASE_SHAPE_NAME)
377 for m in model.morphs:
378 new_shape_key=bl.object.addShapeKey(mesh_object, m.name)
380 if isinstance(o, pmx.VertexMorphOffset):
381 bl.shapekey.assign(new_shape_key,
383 mesh.vertices[o.vertex_index].co+
384 bl.createVector(*convert_coord(o.position_offset)))
386 raise Exception("unknown morph type: %s" % o)
388 bl.object.setActivateShapeKey(mesh_object, 0)
390 #############################
391 # clean up not used vertices
393 #############################
394 bl.mesh.vertsDelete(mesh, [i for i in range(len(mesh.vertices))
395 if i not in used_indices])
397 # import rigid bodies
398 rigidbody_object=__importRigidBodies(model.rigidbodies, model.bones)
400 bl.object.makeParent(root_object, rigidbody_object)
403 joint_object=__import_joints(model.joints, model.rigidbodies)
405 bl.object.makeParent(root_object, joint_object)