10 #
\e$B%U%!%$%k%7%9%F%`$NJ8;z%3!<%I
\e(B
11 #
\e$B2~B$HG$H$N6&MQ$N$?$a
\e(B
12 FS_ENCODING=sys.getfilesystemencoding()
13 if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"):
14 INTERNAL_ENCODING='utf-8'
16 INTERNAL_ENCODING=FS_ENCODING
19 ###############################################################################
21 ###############################################################################
22 class ProgressBar(object):
23 def __init__(self, base):
24 print("#### %s ####" % base)
26 self.start=time.time()
27 self.set('<start>', 0)
29 def advance(self, message, progress):
30 self.progress+=float(progress)
33 def set(self, message, progress):
34 self.progress=float(progress)
37 def _print(self, message):
39 message="%s: %s" % (self.base, message)
40 #Blender.Window.DrawProgressBar(self.progress, message)
44 message='finished in %.2f sec' % (time.time()-self.start)
45 self.set(message, 1.0)
47 def progress_start(base):
49 progressBar=ProgressBar(base)
51 def progress_finish():
55 def progress_print(message, progress=0.05):
57 progressBar.advance(message, progress)
59 def progress_set(message, progress):
61 progressBar.set(message, progress)
64 ###############################################################################
66 def __init__(self, path, encoding):
67 self.io=open(path, "wb")
68 self.encoding=encoding
71 self.io.write(s.encode(self.encoding))
79 ###############################################################################
80 def createEmptyObject(scene, name):
81 empty=bpy.data.objects.new(name, None)
82 scene.objects.link(empty)
85 def createMaterial(name):
86 material = bpy.data.materials.new(name)
89 def createMqoMaterial(m):
90 material = bpy.data.materials.new(m.getName())
91 material.diffuse_color=[m.color.r, m.color.g, m.color.b]
92 material.alpha=m.color.a
93 material.diffuse_intensity=m.diffuse
96 def createPmdMaterial(m):
97 material = bpy.data.materials.new("Material")
98 material.diffuse_shader='FRESNEL'
99 material.specular_shader='TOON'
100 material.diffuse_color=([m.diffuse.r, m.diffuse.g, m.diffuse.b])
101 material.alpha=m.diffuse.a
102 material.specular_hardness=int(m.shinness)
103 material.specular_color=([m.specular.r, m.specular.g, m.specular.b])
104 material.mirror_color=([m.ambient.r, m.ambient.g, m.ambient.b])
105 material.subsurface_scattering.enabled=True if m.flag==1 else False
108 def createTexture(path):
109 texture=bpy.data.textures.new(os.path.basename(path))
111 texture=texture.recast_type()
112 image=bpy.data.images.load(path)
114 texture.mipmap = True
115 texture.interpolation = True
116 texture.use_alpha = True
117 return texture, image
120 def materialAddTexture(material, texture):
121 #material.add_texture(texture, "UV", {"COLOR", "ALPHA"})
122 material.add_texture(texture, "UV", "COLOR")
125 def meshAddMaterial(mesh, material):
126 mesh.add_material(material)
129 def createMesh(scene, name):
130 mesh=bpy.data.meshes.new("Mesh")
131 mesh_object= bpy.data.objects.new(name, mesh)
132 scene.objects.link(mesh_object)
133 return mesh, mesh_object
136 def objectMakeParent(parent, child):
139 def objectAddMirrorModifier(mesh_object):
140 return mesh_object.modifiers.new("Modifier", "MIRROR")
142 def meshAddGeometry(mesh, vertices, faces):
143 mesh.from_pydata(vertices, [], faces)
145 mesh.add_geometry(len(vertices), 0, len(faces))
149 unpackedVertices.extend(v)
150 mesh.verts.foreach_set("co", unpackedVertices)
156 # rotate indices if the 4th is 0
157 face = [face[3], face[0], face[1], face[2]]
160 # rotate indices if the 3rd is 0
161 face = [face[2], face[0], face[1], 0]
164 unpackedFaces.extend(face)
165 mesh.faces.foreach_set("verts_raw", unpackedFaces)
167 assert(len(vertices)==len(mesh.verts))
168 assert(len(faces)==len(mesh.faces))
170 def meshSetFaceUv(mesh, i, face, uv_array, image):
171 uv_face=mesh.uv_textures[0].data[i]
177 def faceSetMaterial(face, material_index):
178 face.material_index=material_index
180 def faceSetSmooth(face, isSmooth):
181 face.smooth=True if isSmooth else False
183 def getTexture(m, dirname):
187 for slot in m.texture_slots:
188 if slot and slot.texture:
190 if texture.type=="IMAGE":
194 imagePath=image.filename
195 if len(dirname)>0 and imagePath.startswith(dirname):
196 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
197 imagePath=imagePath[len(dirname)+1:len(imagePath)]
198 #imagePath=Blender.sys.expandpath(
199 # imagePath).replace("\\", '/')
200 if slot.map_colordiff:
201 tex=" tex(\"%s\")" % imagePath
203 aplane=" aplane(\"%s\")" % imagePath
206 def objectDuplicate(scene, obj):
207 bpy.ops.object.select_all(action='DESELECT')
209 scene.objects.active=obj
210 bpy.ops.object.duplicate()
211 dumy=scene.objects.active
212 bpy.ops.object.rotation_apply()
213 bpy.ops.object.scale_apply()
214 bpy.ops.object.location_apply()
215 return dumy.data, dumy
217 def objectDelete(scene, obj):
218 scene.objects.unlink(obj)
220 def faceVertexCount(face):
221 return len(face.verts)
223 def faceVertices(face):
227 return mesh.active_uv_texture
229 def faceHasUV(mesh, i, face):
230 return mesh.active_uv_texture.data[i]
232 def faceGetUV(mesh, i, faces, count):
233 uvFace=mesh.active_uv_texture.data[i]
235 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
237 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
242 def materialToMqo(m):
243 return "\"%s\" shader(3) col(%f %f %f %f)" % (
245 m.diffuse_color[0], m.diffuse_color[1], m.diffuse_color[2],
248 def faceMaterialIndex(face):
249 return face.material_index
251 def objectGetData(o):
254 def objectAddArmatureModifier(o, armature_object):
255 mod=o.modifiers.new("Modifier", "ARMATURE")
256 mod.object = armature_object
257 mod.use_bone_envelopes=False
262 def objectGetPose(o):
265 def poseBoneLimit(n, b):
268 if n.startswith("knee_"):
275 elif n.startswith("ankle_"):
280 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
283 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
285 def objectDeselectAll():
286 bpy.ops.object.select_all(action='DESELECT')
288 def objectActivate(scene, o):
290 scene.objects.active=o
292 def objectGetActive(scene):
293 return scene.objects.active
295 def meshAddVertexGroup(meshObject, name):
296 meshObject.add_vertex_group(name)
298 def vertexSetNormal(mvert, normal):
299 mvert.normal=mathutils.Vector(normal)
301 def faceSetNormal(face, normal):
304 def meshUseVertexUv(mesh):
307 def vertexSetUv(mvert, uv):
310 def meshAssignVertexGroup(meshObject, name, index, weight):
311 meshObject.add_vertex_to_group(index,
312 meshObject.vertex_groups[name], weight, 'ADD')
315 mesh.add_uv_texture()
317 def meshVertsDelete(mesh, remove_vertices):
319 bpy.ops.mesh.select_all(action='DESELECT')
322 for i in remove_vertices:
323 mesh.verts[i].selected=True
326 bpy.ops.mesh.delete(type='VERT')
329 def createArmature(scene):
330 armature = bpy.data.armatures.new('Armature')
331 armature_object=bpy.data.objects.new('Armature', armature)
332 scene.objects.link(armature_object)
334 armature_object.x_ray=True
335 armature.draw_names=True
336 armature.drawtype='OCTAHEDRAL'
337 armature.deform_envelope=False
338 armature.deform_vertexgroups=True
339 armature.x_axis_mirror=True
341 return armature, armature_object
343 def armatureMakeEditable(scene, armature_object):
344 # select only armature object and set edit mode
345 scene.objects.active=armature_object
346 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
347 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
349 def createIkConstraint(armature_object, p_bone, effector_name, ik):
350 constraint = p_bone.constraints.new('IK')
351 constraint.chain_length=len(ik.children)
352 constraint.target=armature_object
353 constraint.subtarget=effector_name
354 constraint.use_tail=False
355 # not used. place folder when export.
356 constraint.weight=ik.weight
357 constraint.iterations=ik.iterations * 10
360 def createArmatureBone(armature, name):
361 return armature.edit_bones.new(name)
363 def boneSetConnected(bone):
366 def createVector(x, y, z):
367 return mathutils.Vector([x, y, z])
369 def armatureUpdate(armature):
372 def boneLayerMask(bone, layers):
376 layer.append(True if layers[i]!=0 else False)
381 def objectLayerMask(object, layers):
385 layer.append(True if layers[i]!=0 else False)
390 def objectPinShape(o):
391 o.shape_key_lock=True
393 def objectAddShapeKey(o, name):
394 return o.add_shape_key(name)
396 def objectActivateShapeKey(o, index):
397 o.active_shape_key_index=index
399 def shapeKeyAssign(shapeKey, index, pos):
400 shapeKey.data[index].co=pos
402 def objectIsVisible(obj):
403 return obj.restrict_view
405 def meshVertexGroupNames(meshObject):
406 for g in meshObject.vertex_groups:
409 def faceNormal(face):
412 def meshFaceUv(mesh, i, face):
413 return mesh.uv_textures[0].data[i].uv
415 def armatureModifierGetObject(m):
418 def objectHasShapeKey(o):
419 return o.data.shape_keys
421 def objectShapeKeys(o):
422 return o.data.shape_keys.keys
424 def meshVertexGroup(meshObject, name):
426 for i, v in enumerate(meshObject.data.verts):
428 if meshObject.vertex_groups[g.group].name==name:
432 def materialGet(scene, material_name):
433 return bpy.data.materials[material_name]
435 def modifierIsArmature(m):
436 return m.type=="ARMATURE"
438 def boneHeadLocal(b):
439 return b.head_local[0:3]
441 def boneTailLocal(b):
442 return b.tail_local[0:3]
444 def boneIsConnected(b):
447 def constraintIsIKSolver(c):
451 return c.chain_length
459 def ikRotationWeight(c):
462 def shapeKeyGet(b, index):
463 return b.data[index].co
470 return mathutils.Vector([v.x, v.y, v.z])
472 def meshSetSmooth(mesh, smoothing):
473 mesh.autosmooth_angle=int(smoothing)
476 def meshRecalcNormals(mesh_object):
477 bpy.ops.object.select_all(action='DESELECT')
478 objectActivate(bpy.context.scene, mesh_object)
480 bpy.ops.mesh.normals_make_consistent()
483 def meshFlipNormals(mesh):
486 def materialHasTexture(material):
487 return material.texture_slots[0]
489 def faceGetIndices(face):
490 return [face.verts[0], face.verts[1], face.verts[2]]
492 def internalToCp932(s):
493 return s.encode('cp932')