OSDN Git Service

replace blender25-meshio to blender26-meshio
[meshio/pymeshio.git] / blender26-meshio / import_pmx.py
1 # coding: utf-8
2 """
3 PMXモデルをインポートする。
4
5 1マテリアル、1オブジェクトで作成する。
6 """
7 import os
8 from . import bl
9
10
11 def __create_a_material(m, name, textures_and_images):
12     material = bl.material.create(name)
13     # diffuse
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
17     # specular
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)
21     # ambient
22     material.mirror_color=[m.ambient_color.r, m.ambient_color.g, m.ambient_color.b]
23     # todo
24     # flag
25     # edge_color
26     # edge_size
27     # other
28     material.preview_render_type='FLAT'
29     material.use_transparency=True
30     # texture
31     texture_index=bl.material.addTexture(material, textures_and_images[m.texture_index][0])
32     return material
33
34 def _execute(filepath):
35     bl.progress_set('load %s' % filepath, 0.0)
36     print(filepath)
37
38     from .pymeshio.pmx import reader
39     model=reader.read_from_file(filepath)
40     if not model:
41         print("fail to load %s" % filepath)
42         return
43     print(model)
44     bl.progress_set('loaded', 0.1)
45
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
54
55     # テクスチャを作る
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)
60
61     def get_name(name, fmt, *args):
62         if len(name.encode("utf-8"))<16:
63             return name
64         else:
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):
71         # マテリアル毎にメッシュを作成する
72         print(m.name)
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)
77         # activate object
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)])
86
87     return {'FINISHED'}
88