13 FS_ENCODING=sys.getfilesystemencoding()
14 if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"):
15 INTERNAL_ENCODING='utf-8'
17 INTERNAL_ENCODING=FS_ENCODING
26 def initialize(name, scene):
39 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
41 def enterObjectMode():
42 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
45 bpy.ops.object.mode_set(mode='POSE', toggle=False)
47 def createVector(x, y, z):
48 return mathutils.Vector([x, y, z])
55 def __init__(self, path, encoding):
56 self.io=open(path, "wb")
57 self.encoding=encoding
60 self.io.write(s.encode(self.encoding))
69 class ProgressBar(object):
73 def __init__(self, base):
74 print("#### %s ####" % base)
76 self.start=time.time()
77 self.set('<start>', 0)
79 def advance(self, message, progress):
80 self.progress+=float(progress)
83 def set(self, message, progress):
84 self.progress=float(progress)
87 def _print(self, message):
89 message="%s: %s" % (self.base, message)
90 #Blender.Window.DrawProgressBar(self.progress, message)
94 message='finished in %.2f sec' % (time.time()-self.start)
95 self.set(message, 1.0)
97 def progress_start(base):
99 progressBar=ProgressBar(base)
101 def progress_finish():
105 def progress_print(message, progress=0.05):
107 progressBar.advance(message, progress)
109 def progress_set(message, progress):
111 progressBar.set(message, progress)
122 def createEmpty(name):
124 empty=bpy.data.objects.new(name, None)
125 SCENE.objects.link(empty)
130 for o in SCENE.objects:
134 def makeParent(parent, child):
140 bpy.ops.object.select_all(action='DESELECT')
142 SCENE.objects.active=o
143 bpy.ops.object.duplicate()
144 dumy=SCENE.objects.active
145 #bpy.ops.object.rotation_apply()
146 #bpy.ops.object.scale_apply()
147 #bpy.ops.object.location_apply()
148 return dumy.data, dumy
153 SCENE.objects.unlink(o)
167 SCENE.objects.active=o
172 return SCENE.objects.active
176 bpy.ops.object.select_all(action='DESELECT')
179 def setLayerMask(object, layers):
183 layer.append(True if layers[i]!=0 else False)
190 return False if o.hide else True
194 return o.data.shape_keys.keys
197 def addShapeKey(o, name):
199 return o.shape_key_add(name)
201 return o.add_shape_key(name)
205 return o.data.shape_keys
208 def pinShape(o, enable):
209 o.show_only_shape_key=enable
212 def setActivateShapeKey(o, index):
213 o.active_shape_key_index=index
220 def getVertexGroup(o, name):
222 for i, v in enumerate(o.data.vertices):
224 if o.vertex_groups[g.group].name==name:
229 def getVertexGroupNames(o):
230 for g in o.vertex_groups:
234 def addVertexGroup(o, name):
235 o.vertex_groups.new(name)
238 def assignVertexGroup(o, name, index, weight):
239 o.vertex_groups[name].add([index], weight, 'ADD')
242 def createBoneGroup(o, name, color_set='DEFAULT'):
246 bpy.ops.pose.group_add()
248 pose=object.getPose(o)
249 g=pose.bone_groups.active
251 g.color_set=color_set
255 return object.getPose(o).bone_groups
260 def addMirror(mesh_object):
261 return mesh_object.modifiers.new("Modifier", "MIRROR")
264 def addArmature(mesh_object, armature_object):
265 mod=mesh_object.modifiers.new("Modifier", "ARMATURE")
266 mod.object = armature_object
267 mod.use_bone_envelopes=False
270 def hasType(mesh_object, type_name):
271 for mod in mesh_object.modifiers:
272 if mod.type==type_name.upper():
276 def isType(m, type_name):
277 return m.type==type_name.upper()
280 def getArmatureObject(m):
286 def assign(shapeKey, index, pos):
287 shapeKey.data[index].co=pos
290 def getByIndex(b, index):
291 return b.data[index].co
302 texture=bpy.data.textures.new(os.path.basename(path), 'IMAGE')
303 texture.use_mipmap=True
304 texture.use_interpolation=True
305 texture.use_alpha=True
307 image=bpy.data.images.load(path)
309 image=bpy.data.images.new('Image', width=16, height=16)
311 return texture, image
318 return image.filepath
324 return bpy.data.materials.new(name)
327 def get(material_name):
328 return bpy.data.materials[material_name]
331 def addTexture(material, texture, enable=True):
334 for i, slot in enumerate(material.texture_slots):
341 #material.add_texture(texture, "UV", "COLOR")
342 #slot=material.texture_slots.add()
343 slot=material.texture_slots.create(index)
345 slot.texture_coords='UV'
346 slot.blend_type='MULTIPLY'
347 slot.use_map_alpha=True
352 def getTexture(m, index):
353 return m.texture_slots[index].texture
357 return m.texture_slots[0]
360 def setUseTexture(m, index, enable):
361 m.use_textures[index]=enable
364 def eachTexturePath(m):
365 for slot in m.texture_slots:
366 if slot and slot.texture:
368 if texture.type=="IMAGE":
375 def eachEnalbeTexturePath(m):
376 for i, slot in enumerate(m.texture_slots):
377 if m.use_textures[i] and slot and slot.texture:
379 if texture.type=="IMAGE":
390 mesh=bpy.data.meshes.new("Mesh")
391 mesh_object= bpy.data.objects.new(name, mesh)
392 SCENE.objects.link(mesh_object)
393 return mesh, mesh_object
396 def addGeometry(mesh, vertices, faces):
397 mesh.from_pydata(vertices, [], faces)
399 mesh.add_geometry(len(vertices), 0, len(faces))
403 unpackedVertices.extend(v)
404 mesh.vertices.foreach_set("co", unpackedVertices)
410 # rotate indices if the 4th is 0
411 face = [face[3], face[0], face[1], face[2]]
414 # rotate indices if the 3rd is 0
415 face = [face[2], face[0], face[1], 0]
418 unpackedFaces.extend(face)
419 mesh.faces.foreach_set("verts_raw", unpackedFaces)
421 assert(len(vertices)==len(mesh.vertices))
422 assert(len(faces)==len(mesh.faces))
426 return mesh.active_uv_texture
429 def useVertexUV(mesh):
434 mesh.uv_textures.new()
437 def hasFaceUV(mesh, i, face):
438 return mesh.active_uv_texture and mesh.active_uv_texture.data[i]
441 def getFaceUV(mesh, i, faces, count=3):
442 if mesh.active_uv_texture and mesh.active_uv_texture.data[i]:
443 uvFace=mesh.active_uv_texture.data[i]
445 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
447 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
452 return ((0, 0), (0, 0), (0, 0), (0, 0))
455 def setFaceUV(m, i, face, uv_array, image):
456 uv_face=m.uv_textures[0].data[i]
460 uv_face.use_image=True
463 def vertsDelete(m, remove_vertices):
465 bpy.ops.mesh.select_all(action='DESELECT')
468 for i in remove_vertices:
469 m.vertices[i].select=True
472 bpy.ops.mesh.delete(type='VERT')
476 def setSmooth(m, smoothing):
477 m.auto_smooth_angle=int(smoothing)
478 m.use_auto_smooth=True
481 def recalcNormals(mesh_object):
482 bpy.ops.object.select_all(action='DESELECT')
483 object.activate(mesh_object)
485 bpy.ops.mesh.normals_make_consistent()
493 def addMaterial(m, material):
494 m.materials.append(material)
497 def getMaterial(m, index):
498 return m.materials[index]
503 def setNormal(v, normal):
504 v.normal=mathutils.Vector(normal)
518 def getVertexCount(face):
519 return len(face.vertices)
522 def getVertices(face):
523 return face.vertices[:]
526 def getIndices(face, count=3):
528 return [face.vertices[0], face.vertices[1], face.vertices[2]]
530 return [face.vertices[0], face.vertices[1], face.vertices[2], face.vertices[3]]
535 def setMaterial(face, material_index):
536 face.material_index=material_index
539 def getMaterialIndex(face):
540 return face.material_index
543 def setNormal(face, normal):
551 def setSmooth(face, isSmooth):
552 face.use_smooth=True if isSmooth else False
559 armature = bpy.data.armatures.new('Armature')
560 armature_object=bpy.data.objects.new('Armature', armature)
561 SCENE.objects.link(armature_object)
563 armature_object.show_x_ray=True
564 armature.show_names=True
565 #armature.draw_type='OCTAHEDRAL'
566 armature.draw_type='STICK'
567 armature.use_deform_envelopes=False
568 armature.use_deform_vertex_groups=True
569 armature.use_mirror_x=True
571 return armature, armature_object
574 def makeEditable(armature_object):
576 # select only armature object and set edit mode
577 SCENE.objects.active=armature_object
578 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
579 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
582 def createIkConstraint(armature_object, p_bone, effector_name, ik):
583 constraint = p_bone.constraints.new('IK')
584 constraint.chain_count=len(ik.children)
585 constraint.target=armature_object
586 constraint.subtarget=effector_name
587 constraint.use_tail=False
588 # not used. place folder when export.
589 constraint.weight=ik.weight
590 constraint.iterations=ik.iterations * 10
594 def createBone(armature, name):
595 return armature.edit_bones.new(name)
598 def update(armature):
604 def setConnected(bone):
605 bone.use_connect=True
612 def setLayerMask(bone, layers):
616 layer.append(True if layers[i]!=0 else False)
623 return b.head_local[0:3]
627 return b.tail_local[0:3]
633 return c.chain_length
644 def ikRotationWeight(c):