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='TOON'
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 meshAddMqoGeometry(mesh_object, o, materials, imageMap, scale):
143 mesh=mesh_object.data
144 # count triangle and quadrangle
147 if f.index_count==3 or f.index_count==4:
149 mesh.add_geometry(len(o.vertices), 0, faceCount)
154 # convert right-handed y-up to right-handed z-up
155 unpackedVertices.extend(
156 (scale*v.x, scale*-v.z, scale*v.y))
157 mesh.verts.foreach_set("co", unpackedVertices)
165 for i in range(f.index_count):
166 face.append(f.getIndex(i))
171 if len(face) != 3 and len(face) != 4:
172 print("{0} vertices in face.".format(len(face)))
177 # rotate indices if the 4th is 0
178 face = [face[3], face[0], face[1], face[2]]
181 # rotate indices if the 3rd is 0
182 face = [face[2], face[0], face[1], 0]
186 unpackedFaces.extend(face)
187 usedMaterial.add(f.material_index)
189 mesh.faces.foreach_set("verts_raw", unpackedFaces)
191 #print([getFace(f) for f in o.faces])
192 print("fail to mesh.faces.foreach_set")
198 for i in usedMaterial:
199 mesh.add_material(materials[i])
200 meshMaterialMap[i]=materialIndex
204 mesh.add_uv_texture()
205 for mqo_face, blender_face, uv_face in zip(
206 o.faces, mesh.faces, mesh.uv_textures[0].data):
207 if mqo_face.index_count<3:
209 blender_face.material_index=meshMaterialMap[mqo_face.material_index]
210 if mqo_face.index_count>=3:
211 uv_face.uv1=[mqo_face.getUV(0).x, 1.0-mqo_face.getUV(0).y]
212 uv_face.uv2=[mqo_face.getUV(1).x, 1.0-mqo_face.getUV(1).y]
213 uv_face.uv3=[mqo_face.getUV(2).x, 1.0-mqo_face.getUV(2).y]
214 if mqo_face.index_count==4:
216 mqo_face.getUV(3).x, 1.0-mqo_face.getUV(3).y]
217 if materials[mqo_face.material_index] in imageMap:
218 uv_face.image=imageMap[mqo_face.material_index]
223 def getTexture(m, dirname):
227 for slot in m.texture_slots:
228 if slot and slot.texture:
230 if texture.type=="IMAGE":
234 imagePath=image.filename
235 if len(dirname)>0 and imagePath.startswith(dirname):
236 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
237 imagePath=imagePath[len(dirname)+1:len(imagePath)]
238 #imagePath=Blender.sys.expandpath(
239 # imagePath).replace("\\", '/')
240 if slot.map_colordiff:
241 tex=" tex(\"%s\")" % imagePath
243 aplane=" aplane(\"%s\")" % imagePath
246 def objectDuplicate(scene, obj):
247 bpy.ops.object.select_all(action='DESELECT')
249 scene.objects.active=obj
250 bpy.ops.object.duplicate()
251 dumy=scene.objects.active
252 bpy.ops.object.rotation_apply()
253 bpy.ops.object.scale_apply()
254 bpy.ops.object.location_apply()
255 return dumy.data, dumy
257 def objectDelete(scene, obj):
258 scene.objects.unlink(obj)
260 def faceVertexCount(face):
261 return len(face.verts)
263 def faceVertices(face):
267 return mesh.active_uv_texture
269 def faceHasUV(mesh, i, face):
270 return mesh.active_uv_texture.data[i]
272 def faceGetUV(mesh, i, faces, count):
273 uvFace=mesh.active_uv_texture.data[i]
275 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
277 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
282 def materialToMqo(m):
283 return "\"%s\" shader(3) col(%f %f %f %f)" % (
285 m.diffuse_color[0], m.diffuse_color[1], m.diffuse_color[2],
288 def faceMaterialIndex(face):
289 return face.material_index
291 def objectGetData(o):
294 def objectAddArmatureModifier(o, armature_object):
295 mod=o.modifiers.new("Modifier", "ARMATURE")
296 mod.object = armature_object
297 mod.use_bone_envelopes=False
302 def objectGetPose(o):
305 def poseBoneLimit(n, b):
308 if n.startswith("knee_"):
315 elif n.startswith("ankle_"):
320 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
323 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
325 def objectDeselectAll():
326 bpy.ops.object.select_all(action='DESELECT')
328 def objectActivate(scene, o):
330 scene.objects.active=o
332 def objectGetActive(scene):
333 return scene.objects.active
335 def meshAddVertexGroup(meshObject, name):
336 meshObject.add_vertex_group(name)
338 def vertexSetNormal(mvert, normal):
339 mvert.normal=mathutils.Vector(normal)
341 def meshUseVertexUv(mesh):
344 def vertexSetUv(mvert, uv):
347 def meshAssignVertexGroup(meshObject, name, index, weight):
348 meshObject.add_vertex_to_group(index,
349 meshObject.vertex_groups[name], weight, 'ADD')
351 def meshCreateVerteicesAndFaces(mesh, vertices, faces):
352 vertexCount=int(len(vertices)/3)
353 faceCount=int(len(faces)/4)
354 mesh.add_geometry(vertexCount, 0, faceCount)
355 mesh.verts.foreach_set("co", vertices)
356 mesh.faces.foreach_set("verts_raw", faces)
357 assert(len(mesh.verts)==vertexCount)
360 mesh.add_uv_texture()
362 def meshVertsDelete(mesh, remove_vertices):
364 bpy.ops.mesh.select_all(action='DESELECT')
367 for i in remove_vertices:
368 mesh.verts[i].selected=True
371 bpy.ops.mesh.delete(type='VERT')
374 def createArmature(scene):
375 armature = bpy.data.armatures.new('Armature')
376 armature_object=bpy.data.objects.new('Armature', armature)
377 scene.objects.link(armature_object)
379 armature_object.x_ray=True
380 armature.draw_names=True
381 armature.drawtype='OCTAHEDRAL'
382 armature.deform_envelope=False
383 armature.deform_vertexgroups=True
384 armature.x_axis_mirror=True
386 return armature, armature_object
388 def armatureMakeEditable(scene, armature_object):
389 # select only armature object and set edit mode
390 scene.objects.active=armature_object
391 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
392 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
394 def createIkConstraint(armature_object, p_bone, effector_name, ik):
395 constraint = p_bone.constraints.new('IK')
396 constraint.chain_length=len(ik.children)
397 constraint.target=armature_object
398 constraint.subtarget=effector_name
399 constraint.use_tail=False
400 # not used. place folder when export.
401 constraint.weight=ik.weight
402 constraint.iterations=ik.iterations * 10
405 def createArmatureBone(armature, name):
406 return armature.edit_bones.new(name)
408 def boneSetConnected(bone):
411 def createVector(x, y, z):
412 return mathutils.Vector([x, y, z])
414 def armatureUpdate(armature):
417 def boneLayerMask(bone, layers):
421 layer.append(True if layers[i]!=0 else False)
426 def objectLayerMask(object, layers):
430 layer.append(True if layers[i]!=0 else False)
435 def objectPinShape(o):
436 o.shape_key_lock=True
438 def objectAddShapeKey(o, name):
439 return o.add_shape_key(name)
441 def objectActivateShapeKey(o, index):
442 o.active_shape_key_index=index
444 def shapeKeyAssign(shapeKey, index, pos):
445 shapeKey.data[index].co=pos
447 def objectIsVisible(obj):
448 return obj.restrict_view
450 def meshVertexGroupNames(meshObject):
451 for g in meshObject.vertex_groups:
454 def faceNormal(face):
457 def meshFaceUv(mesh, i, face):
458 return mesh.uv_textures[0].data[i].uv
460 def armatureModifierGetObject(m):
463 def objectHasShapeKey(o):
464 return o.data.shape_keys
466 def objectShapeKeys(o):
467 return o.data.shape_keys.keys
469 def meshVertexGroup(meshObject, name):
471 for i, v in enumerate(meshObject.data.verts):
473 if meshObject.vertex_groups[g.group].name==name:
477 def materialGet(scene, material_name):
478 return bpy.data.materials[material_name]
480 def modifierIsArmature(m):
481 return m.type=="ARMATURE"
483 def boneHeadLocal(b):
484 return b.head_local[0:3]
486 def boneTailLocal(b):
487 return b.tail_local[0:3]
489 def boneIsConnected(b):
492 def constraintIsIKSolver(c):
496 return c.chain_length
504 def ikRotationWeight(c):
507 def shapeKeyGet(b, index):
508 return b.data[index].co
515 return mathutils.Vector([v.x, v.y, v.z])
517 def meshSetSmooth(mesh, smoothing):
518 mesh.autosmooth_angle=int(smoothing)