11 def __create_a_material(m, name, textures_and_images):
12 material = bl.material.create(name)
14 material.diffuse_shader='FRESNEL'
15 material.diffuse_color=[m.diffuse_color.r, m.diffuse_color.g, m.diffuse_color.b]
16 material.alpha=m.alpha
18 material.specular_shader='TOON'
19 material.specular_color=[m.specular_color.r, m.specular_color.g, m.specular_color.b]
20 material.specular_toon_size=int(m.specular_factor)
22 material.mirror_color=[m.ambient_color.r, m.ambient_color.g, m.ambient_color.b]
28 material.preview_render_type='FLAT'
29 material.use_transparency=True
31 texture_index=bl.material.addTexture(material, textures_and_images[m.texture_index][0])
34 def _execute(filepath):
35 bl.progress_set('load %s' % filepath, 0.0)
38 from .pymeshio.pmx import reader
39 model=reader.read_from_file(filepath)
41 print("fail to load %s" % filepath)
44 bl.progress_set('loaded', 0.1)
46 # メッシュをまとめるエンプティオブジェクト
47 model_name=model.english_name
48 if len(model_name)==0:
49 model_name=os.path.basename(filepath)
50 root=bl.object.createEmpty(model_name)
51 root[bl.MMD_MB_NAME]=model.name
52 root[bl.MMD_MB_COMMENT]=model.comment
53 root[bl.MMD_COMMENT]=model.english_comment
56 texture_dir=os.path.dirname(filepath)
57 textures_and_images=[bl.texture.create(os.path.join(texture_dir, t))
58 for t in model.textures]
59 print(textures_and_images)
61 def get_name(name, fmt, *args):
62 if len(name.encode("utf-8"))<16:
65 return fmt.format(*args)
66 index_generator=(i for i in model.indices)
67 # 頂点配列。(Left handed y-up) to (Right handed z-up)
68 vertices=[(pos.x, pos.z, pos.y)
69 for pos in (v.position for v in model.vertices)]
70 for i, m in enumerate(model.materials):
73 #material=__create_a_material(m, get_name(m.name, "material:{0:02}", i), textures_and_images)
74 material=__create_a_material(m, m.name, textures_and_images)
75 mesh, mesh_object=bl.mesh.create("object:{0:02}".format(i))
76 bl.mesh.addMaterial(mesh, material)
78 bl.object.deselectAll()
79 bl.object.activate(mesh_object)
80 bl.object.makeParent(root, mesh_object)
81 indices=[next(index_generator)
82 for _ in range(m.vertex_count)]
83 bl.mesh.addGeometry(mesh, vertices,
84 [(indices[i], indices[i+1], indices[i+2])
85 for i in range(0, len(indices), 3)])