8 from .common import unicode as u
12 class ConvertException(Exception):
21 return pymeshio.pmx.Model.
29 dst.name=src.name.decode("cp932")
30 dst.english_name=src.english_name.decode("cp932")
31 dst.comment=src.comment.replace(
32 b"\n", b"\r\n").decode("cp932")
33 dst.english_comment=src.english_comment.replace(
34 b"\n", b"\r\n").decode("cp932")
36 def createDeform(bone0, bone1, weight0):
38 return pmx.Bdef1(bone1)
40 return pmx.Bdef1(bone0)
42 return pmx.Bdef2(bone0, bone1, weight0*0.01)
48 createDeform(v.bone0, v.bone1, v.weight0),
49 1.0 if v.edge_flag==0 else 0.0
51 for v in src.vertices]
53 dst.indices=src.indices[:]
59 (2 if (m.edge_flag & 1!=0) else 0)+
62 (16 if (m.edge_flag & 1!=0) else 0)
64 def get_texture_file(path):
67 elif path.find(b'*')==-1:
70 return b'*'.split(path)[0]
71 def get_sphere_texture_file(path):
74 elif path.find(b'*')==-1:
77 return b'*'.split(path)[1]
78 def get_texture_index(path):
80 return texture_map[get_texture_file(path)]
83 def get_sphere_texture_index(path):
85 return texture_map[get_sphere_texture_file(path)]
88 def get_sphere_texture_flag(path):
89 sphere_texture=get_sphere_texture_file(path)
91 if sphere_texture.endswith('.sph'):
93 elif sphere_texture.endswith('.spa'):
96 raise ConvertException(
97 "invalid sphere texture: {0}".format(sphere_texture))
99 def get_toon_shared_flag(m):
101 def get_toon_index(m):
103 for m in src.materials:
104 texture=get_texture_file(m.texture_file)
105 if texture and not texture in texture_map:
106 texture_map[texture]=len(texture_map)
107 dst.textures.append(texture.decode("cp932"))
108 sphere_texture=get_sphere_texture_file(m.texture_file)
109 if sphere_texture and not sphere_texture in texture_map:
110 texture_map[sphere_texture]=len(texture_map)
111 dst.textures.append(sphere_texture.decode("cp932"))
114 name=common.unicode(""),
115 english_name=common.unicode(""),
116 diffuse_color=m.diffuse_color,
118 specular_factor=m.specular_factor,
119 specular_color=m.specular_color,
120 ambient_color=m.ambient_color,
122 edge_color=common.RGBA(0.0, 0.0, 0.0, 1.0),
124 texture_index=get_texture_index(m.texture_file),
125 sphere_texture_index=get_sphere_texture_index(m.texture_file),
126 sphere_mode=get_sphere_texture_flag(m.texture_file),
127 toon_sharing_flag=get_toon_shared_flag(m),
128 toon_texture_index=get_toon_index(m),
129 comment=common.unicode(""),
130 vertex_count=m.vertex_count
132 for i, m in enumerate(src.materials)]
135 for ik in src.ik_list:
138 if isinstance(b, pmd.Bone_Rolling):
140 if isinstance(b, pmd.Bone_Tweak):
144 if isinstance(b, pmd.Bone_Rotate):
146 if isinstance(b, pmd.Bone_RotateMove):
148 if isinstance(b, pmd.Bone_RotateInfl):
150 if isinstance(b, pmd.Bone_IKRotateInfl):
152 if isinstance(b, pmd.Bone_Rolling):
154 if isinstance(b, pmd.Bone_IKTarget):
156 if isinstance(b, pmd.Bone_IK):
158 if isinstance(b, pmd.Bone_Unvisible):
160 if isinstance(b, pmd.Bone_Tweak):
163 if isinstance(b, pmd.Bone_RotateMove):
165 if isinstance(b, pmd.Bone_IK):
168 if isinstance(b, pmd.Bone_Unvisible):
170 if isinstance(b, pmd.Bone_IKTarget):
172 if isinstance(b, pmd.Bone_Tweak):
175 def is_manupilatable(b):
178 if isinstance(b, pmd.Bone_IK):
180 def is_external_rotation(b):
181 if isinstance(b, pmd.Bone_RotateInfl):
183 if isinstance(b, pmd.Bone_Tweak):
185 def is_fixed_axis(b):
186 if isinstance(b, pmd.Bone_Rolling):
188 def is_local_axis(b):
190 def after_physics(b):
192 def external_parent(b):
194 def get_bone_flag(b):
196 (1 if is_connected(b) else 0)+
197 (2 if is_rotatable(b) else 0)+
198 (4 if is_movable(b) else 0)+
199 (8 if is_visible(b) else 0)+
201 (16 if is_manupilatable(b) else 0)+
202 (32 if has_ik(b) else 0)+
206 (256 if is_external_rotation(b) else 0)+
208 (1024 if is_fixed_axis(b) else 0)+
209 (2048 if is_local_axis(b) else 0)+
211 (4096 if after_physics(b) else 0)+
212 (8192 if external_parent(b) else 0)
214 def get_tail_position(b):
215 return common.Vector3()
216 def get_tail_index(b):
217 if isinstance(b, pmd.Bone_Rolling):
219 if isinstance(b, pmd.Bone_IKTarget):
221 if isinstance(b, pmd.Bone_Unvisible):
223 if isinstance(b, pmd.Bone_Tweak):
226 def get_ik_link(bone_index):
227 b=src.bones[bone_index]
228 if b.english_name.find(b'knee')==-1:
236 common.Vector3(-3.1415927410125732, 0.0, 0.0),
237 common.Vector3(-0.00872664619237184524536132812500, 0.0, 0.0))
239 if isinstance(b, pmd.Bone_IK):
242 ik.target, ik.iterations, ik.weight * 4, [
243 get_ik_link(child) for child in ik.children ])
249 name=b.name.decode('cp932'),
250 english_name=b.english_name.decode('cp932'),
252 parent_index=b.parent_index if b.parent_index!=65535 else -1,
254 flag=get_bone_flag(b),
255 tail_position=get_tail_position(b),
256 tail_index=get_tail_index(b),
259 fixed_axis=common.Vector3(),
260 local_x_vector=common.Vector3(),
261 local_z_vector=common.Vector3(),
265 for i, b in enumerate(src.bones)]
269 if len(src.morphs)>0:
271 assert(base.name==b"base")
274 name=m.name.decode('cp932'),
275 english_name=m.english_name.decode('cp932'),
278 offsets=[pmx.VerexMorphOffset(base.indices[i], pos)
279 for i, pos in zip(m.indices, m.pos_list)]
281 for i, m in enumerate(src.morphs) if m.name!=b"base"]
284 pmx.DisplaySlot(u('Root'), u('Root'), 1),
285 pmx.DisplaySlot(u('表情'), u('Exp'), 1)]+[
287 name=g.name.strip().decode('cp932'),
288 english_name=g.english_name.strip().decode('cp932'),
290 for i, g in enumerate(src.bone_group_list)]
294 name=r.name.decode("cp932"),
296 bone_index=r.bone_index,
297 collision_group=r.collision_group,
298 no_collision_group=r.no_collision_group,
299 shape_type=r.shape_type,
300 shape_size=r.shape_size,
301 shape_position=r.shape_position,
302 shape_rotation=r.shape_rotation,
304 linear_damping=r.linear_damping,
305 angular_damping=r.angular_damping,
306 restitution=r.restitution,
310 for i, r in enumerate(src.rigidbodies)]
314 j.name.decode('cp932'),
321 j.translation_limit_min,
322 j.translation_limit_max,
323 j.rotation_limit_min,
324 j.rotation_limit_max,
325 j.spring_constant_translation,
326 j.spring_constant_rotation
328 for i, j in enumerate(src.joints)]