6 def __init__(self, path, encoding):
7 self.io=open(path, "wb")
11 self.io.write(s.encode(self.encoding))
20 def createEmptyObject(scene, name):
21 empty=bpy.data.objects.new(name, None)
22 scene.objects.link(empty)
26 def createMqoMaterial(m):
27 material = bpy.data.materials.new(m.getName())
28 material.diffuse_color=[m.color.r, m.color.g, m.color.b]
29 material.alpha=m.color.a
30 material.diffuse_intensity=m.diffuse
36 def createTexture(path):
37 texture=bpy.data.textures.new(os.path.basename(path))
39 texture=texture.recast_type()
40 image=bpy.data.images.load(path)
43 texture.interpolation = True
44 texture.use_alpha = True
48 def materialAddTexture(material, texture):
49 #material.add_texture(texture, "UV", {"COLOR", "ALPHA"})
50 material.add_texture(texture, "UV", "COLOR")
53 def createMesh(scene, name):
54 mesh=bpy.data.meshes.new("Mesh")
55 mesh_object= bpy.data.objects.new(name, mesh)
56 scene.objects.link(mesh_object)
57 return mesh, mesh_object
60 def objectMakeParent(parent, child):
64 def meshAddMqoGeometry(mesh, o, materials, imageMap, scale):
65 # count triangle and quadrangle
68 if f.index_count==3 or f.index_count==4:
70 mesh.add_geometry(len(o.vertices), 0, faceCount)
75 # convert right-handed y-up to right-handed z-up
76 unpackedVertices.extend(
77 (scale*v.x, scale*-v.z, scale*v.y))
78 mesh.verts.foreach_set("co", unpackedVertices)
86 for i in range(f.index_count):
87 face.append(f.getIndex(i))
92 if len(face) != 3 and len(face) != 4:
93 print("{0} vertices in face.".format(len(face)))
98 # rotate indices if the 4th is 0
99 face = [face[3], face[0], face[1], face[2]]
102 # rotate indices if the 3rd is 0
103 face = [face[2], face[0], face[1], 0]
107 unpackedFaces.extend(face)
108 usedMaterial.add(f.material_index)
110 mesh.faces.foreach_set("verts_raw", unpackedFaces)
112 #print([getFace(f) for f in o.faces])
113 print("fail to mesh.faces.foreach_set")
119 for i in usedMaterial:
120 mesh.add_material(materials[i])
121 meshMaterialMap[i]=materialIndex
125 mesh.add_uv_texture()
126 for mqo_face, blender_face, uv_face in zip(
127 o.faces, mesh.faces, mesh.uv_textures[0].data):
128 if mqo_face.index_count<3:
130 blender_face.material_index=meshMaterialMap[mqo_face.material_index]
131 if mqo_face.index_count>=3:
132 uv_face.uv1=[mqo_face.getUV(0).x, 1.0-mqo_face.getUV(0).y]
133 uv_face.uv2=[mqo_face.getUV(1).x, 1.0-mqo_face.getUV(1).y]
134 uv_face.uv3=[mqo_face.getUV(2).x, 1.0-mqo_face.getUV(2).y]
135 if mqo_face.index_count==4:
137 mqo_face.getUV(3).x, 1.0-mqo_face.getUV(3).y]
138 if materials[mqo_face.material_index] in imageMap:
139 uv_face.image=imageMap[mqo_face.material_index]
144 def getTexture(m, dirname):
148 for slot in m.texture_slots:
149 if slot and slot.texture:
151 if texture.type=="IMAGE":
155 imagePath=image.filename
156 if len(dirname)>0 and imagePath.startswith(dirname):
157 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
158 imagePath=imagePath[len(dirname)+1:len(imagePath)]
159 #imagePath=Blender.sys.expandpath(
160 # imagePath).replace("\\", '/')
161 if slot.map_colordiff:
162 tex=" tex(\"%s\")" % imagePath
164 aplane=" aplane(\"%s\")" % imagePath
167 def objectDuplicate(scene, obj):
168 bpy.ops.object.select_all(action='DESELECT')
170 scene.objects.active=obj
171 bpy.ops.object.duplicate()
172 dumy=scene.objects.active
173 bpy.ops.object.rotation_apply()
174 bpy.ops.object.scale_apply()
175 bpy.ops.object.location_apply()
176 return dumy.data, dumy
178 def faceVertexCount(face):
179 return len(face.verts)
181 def faceVertices(face):
185 return mesh.active_uv_texture
187 def faceHasUV(mesh, i, face):
188 return mesh.active_uv_texture.data[i]
190 def faceGetUV(mesh, i, faces, count):
191 uvFace=mesh.active_uv_texture.data[i]
193 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
195 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
200 def materialToMqo(m):
201 return "\"%s\" shader(3) col(%f %f %f %f)" % (
203 m.diffuse_color[0], m.diffuse_color[1], m.diffuse_color[2],
206 def faceMaterialIndex(face):
207 return face.material_index