9 from .pymeshio import pmx
10 from .pymeshio.pmx import reader
13 def convert_coord(pos):
15 Left handed y-up to Right handed z-up
17 return (pos.x, pos.z, pos.y)
19 def __create_a_material(m, name, textures_and_images):
29 list of (texture, image)
31 material = bl.material.create(name)
33 material.diffuse_shader='FRESNEL'
34 material.diffuse_color=[m.diffuse_color.r, m.diffuse_color.g, m.diffuse_color.b]
35 material.alpha=m.alpha
37 material.specular_shader='TOON'
38 material.specular_color=[m.specular_color.r, m.specular_color.g, m.specular_color.b]
39 material.specular_toon_size=int(m.specular_factor)
41 material.mirror_color=[m.ambient_color.r, m.ambient_color.g, m.ambient_color.b]
47 material.preview_render_type='FLAT'
48 material.use_transparency=True
50 texture_index=bl.material.addTexture(material, textures_and_images[m.texture_index][0])
53 def __create_armature(bones, display_slots):
59 list of pymeshio.pmx.Bone
61 armature, armature_object=bl.armature.create()
64 bl.armature.makeEditable(armature_object)
66 bone=bl.armature.createBone(armature, b.name)
68 bone.head=bl.createVector(*convert_coord(b.position))
69 if not b.getConnectionFlag():
70 bone.tail=bl.createVector(*convert_coord(b.position))
71 elif not b.getVisibleFlag():
72 bone.tail=bone.head+bl.createVector(0, 1, 0)
75 bl_bones=[create_bone(b) for b in bones]
78 for b, bone in zip(bones, bl_bones):
79 assert(b.name==bone.name)
80 if b.parent_index!=-1:
81 print("%s -> %s" % (bones[b.parent_index].name, b.name))
82 parent_bone=bl_bones[b.parent_index]
83 bone.parent=parent_bone
84 if b.getConnectionFlag() and b.tail_index!=-1:
85 assert(b.tail_index!=0)
86 tail_bone=bl_bones[b.tail_index]
87 bone.tail=tail_bone.head
88 bl.bone.setConnected(tail_bone)
90 print("no parent %s" % b.name)
91 bl.armature.update(armature)
93 # create ik constraint
95 pose = bl.object.getPose(armature_object)
96 for b, bone in zip(bones, bl_bones):
99 assert(len(ik.link)<16)
100 p_bone=pose.bones[bones[ik.target_index].name]
102 constraint=bl.armature.createIkConstraint(
103 armature_object, p_bone, bone.name,
104 ik.link, ik.limit_radian, ik.loop)
105 bl.armature.makeEditable(armature_object)
106 bl.armature.update(armature)
110 pose = bl.object.getPose(armature_object)
111 for i, ds in enumerate(display_slots):
113 g=bl.object.createBoneGroup(armature_object, ds.name, "THEME%02d" % (i+1))
114 for t, index in ds.references:
116 name=bones[index].name
118 pose.bones[name].bone_group=g
119 except KeyError as e:
120 print("pose %s is not found" % name)
123 return armature_object
125 def _execute(filepath):
129 bl.progress_set('load %s' % filepath, 0.0)
132 model=reader.read_from_file(filepath)
134 print("fail to load %s" % filepath)
137 bl.progress_set('loaded', 0.1)
139 # メッシュをまとめるエンプティオブジェクト
140 model_name=model.english_name
141 if len(model_name)==0:
142 model_name=os.path.basename(filepath)
143 root_object=bl.object.createEmpty(model_name)
144 root_object[bl.MMD_MB_NAME]=model.name
145 root_object[bl.MMD_MB_COMMENT]=model.comment
146 root_object[bl.MMD_COMMENT]=model.english_comment
149 armature_object=__create_armature(model.bones, model.display_slots)
151 bl.object.makeParent(root_object, armature_object)
154 texture_dir=os.path.dirname(filepath)
155 textures_and_images=[bl.texture.create(os.path.join(texture_dir, t))
156 for t in model.textures]
157 print(textures_and_images)
159 def get_name(name, fmt, *args):
160 if len(name.encode("utf-8"))<16:
163 return fmt.format(*args)
164 index_generator=(i for i in model.indices)
165 # 頂点配列。(Left handed y-up) to (Right handed z-up)
166 vertices=[convert_coord(pos)
167 for pos in (v.position for v in model.vertices)]
168 for i, m in enumerate(model.materials):
171 #material=__create_a_material(m, get_name(m.name, "material:{0:02}", i), textures_and_images)
172 material=__create_a_material(m, m.name, textures_and_images)
173 mesh, mesh_object=bl.mesh.create("object:{0:02}".format(i))
174 bl.mesh.addMaterial(mesh, material)
176 bl.object.deselectAll()
177 bl.object.activate(mesh_object)
178 bl.object.makeParent(root_object, mesh_object)
180 indices=[next(index_generator)
181 for _ in range(m.vertex_count)]
182 bl.mesh.addGeometry(mesh, vertices,
183 [(indices[i], indices[i+1], indices[i+2])
184 for i in range(0, len(indices), 3)])
185 assert(len(model.vertices), len(mesh.vertices))
186 # set vertex attributes(normal, bone weights)
187 bl.mesh.useVertexUV(mesh)
188 for i, (v, mvert) in enumerate(zip(model.vertices, mesh.vertices)):
189 bl.vertex.setNormal(mvert, convert_coord(v.normal))
190 if isinstance(v.deform, pmx.Bdef1):
191 bl.object.assignVertexGroup(mesh_object,
192 model.bones[v.deform.index0].name, i, 1.0)
193 elif isinstance(v.deform, pmx.Bdef2):
194 bl.object.assignVertexGroup(mesh_object,
195 model.bones[v.deform.index0].name, i, v.deform.weight0)
196 bl.object.assignVertexGroup(mesh_object,
197 model.bones[v.deform.index1].name, i, 1.0-v.deform.weight0)
199 raise Exception("unknown deform: %s" % v.deform)
204 bl.modifier.addArmature(mesh_object, armature_object)