13 #
\e$B%U%!%$%k%7%9%F%`$NJ8;z%3!<%I
\e(B
14 #
\e$B2~B$HG$H$N6&MQ$N$?$a
\e(B
15 FS_ENCODING=sys.getfilesystemencoding()
16 if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"):
17 INTERNAL_ENCODING='utf-8'
19 INTERNAL_ENCODING=FS_ENCODING
28 def initialize(name, scene):
41 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
43 def enterObjectMode():
44 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
47 bpy.ops.object.mode_set(mode='POSE', toggle=False)
49 def createVector(x, y, z):
50 return mathutils.Vector([x, y, z])
57 def __init__(self, path, encoding):
58 self.io=open(path, "wb")
59 self.encoding=encoding
62 self.io.write(s.encode(self.encoding))
71 class ProgressBar(object):
75 def __init__(self, base):
76 print("#### %s ####" % base)
78 self.start=time.time()
79 self.set('<start>', 0)
81 def advance(self, message, progress):
82 self.progress+=float(progress)
85 def set(self, message, progress):
86 self.progress=float(progress)
89 def _print(self, message):
91 message="%s: %s" % (self.base, message)
92 #Blender.Window.DrawProgressBar(self.progress, message)
96 message='finished in %.2f sec' % (time.time()-self.start)
97 self.set(message, 1.0)
99 def progress_start(base):
101 progressBar=ProgressBar(base)
103 def progress_finish():
107 def progress_print(message, progress=0.05):
109 progressBar.advance(message, progress)
111 def progress_set(message, progress):
113 progressBar.set(message, progress)
124 def createEmpty(name):
126 empty=bpy.data.objects.new(name, None)
127 SCENE.objects.link(empty)
132 for o in SCENE.objects:
136 def makeParent(parent, child):
142 bpy.ops.object.select_all(action='DESELECT')
144 SCENE.objects.active=o
145 bpy.ops.object.duplicate()
146 dumy=SCENE.objects.active
147 #bpy.ops.object.rotation_apply()
148 #bpy.ops.object.scale_apply()
149 #bpy.ops.object.location_apply()
150 return dumy.data, dumy
155 SCENE.objects.unlink(o)
169 SCENE.objects.active=o
174 return SCENE.objects.active
178 bpy.ops.object.select_all(action='DESELECT')
181 def setLayerMask(object, layers):
185 layer.append(True if layers[i]!=0 else False)
192 return False if o.hide else True
196 return o.data.shape_keys.keys
199 def addShapeKey(o, name):
200 return o.add_shape_key(name)
204 return o.data.shape_keys
207 def pinShape(o, enable):
208 o.shape_key_lock=enable
211 def setActivateShapeKey(o, index):
212 o.active_shape_key_index=index
219 def getVertexGroup(o, name):
221 for i, v in enumerate(o.data.verts):
223 if o.vertex_groups[g.group].name==name:
228 def getVertexGroupNames(o):
229 for g in o.vertex_groups:
233 def addVertexGroup(o, name):
234 o.add_vertex_group(name)
237 def assignVertexGroup(o, name, index, weight):
238 o.add_vertex_to_group(index,
239 o.vertex_groups[name], weight, 'ADD')
242 def createBoneGroup(o, name, color_set='DEFAULT'):
246 bpy.ops.pose.group_add()
248 pose=object.getPose(o)
249 g=pose.active_bone_group
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))
304 texture=texture.recast_type()
306 texture.interpolation=True
307 texture.use_alpha=True
309 image=bpy.data.images.load(path)
311 image=bpy.data.images.new('Image')
313 return texture, image
320 return image.filepath
326 return bpy.data.materials.new(name)
329 def get(material_name):
330 return bpy.data.materials[material_name]
333 def addTexture(material, texture, enable=True):
336 for i, slot in enumerate(material.texture_slots):
344 material.add_texture(texture, "UV", "COLOR")
345 slot=material.texture_slots[index]
346 slot.blend_type='MULTIPLY'
349 material.add_texture(texture)
350 material.use_textures[index]=False
354 def getTexture(m, index):
355 return m.texture_slots[index].texture
359 return m.texture_slots[0]
362 def setUseTexture(m, index, enable):
363 m.use_textures[index]=enable
366 def eachTexturePath(m):
367 for slot in m.texture_slots:
368 if slot and slot.texture:
370 if texture.type=="IMAGE":
377 def eachEnalbeTexturePath(m):
378 for i, slot in enumerate(m.texture_slots):
379 if m.use_textures[i] and slot and slot.texture:
381 if texture.type=="IMAGE":
392 mesh=bpy.data.meshes.new("Mesh")
393 mesh_object= bpy.data.objects.new(name, mesh)
394 SCENE.objects.link(mesh_object)
395 return mesh, mesh_object
398 def addGeometry(mesh, vertices, faces):
399 mesh.from_pydata(vertices, [], faces)
401 mesh.add_geometry(len(vertices), 0, len(faces))
405 unpackedVertices.extend(v)
406 mesh.verts.foreach_set("co", unpackedVertices)
412 # rotate indices if the 4th is 0
413 face = [face[3], face[0], face[1], face[2]]
416 # rotate indices if the 3rd is 0
417 face = [face[2], face[0], face[1], 0]
420 unpackedFaces.extend(face)
421 mesh.faces.foreach_set("verts_raw", unpackedFaces)
423 assert(len(vertices)==len(mesh.verts))
424 assert(len(faces)==len(mesh.faces))
428 return mesh.active_uv_texture
431 def useVertexUV(mesh):
436 mesh.add_uv_texture()
439 def hasFaceUV(mesh, i, face):
440 return mesh.active_uv_texture and mesh.active_uv_texture.data[i]
443 def getFaceUV(mesh, i, faces, count=3):
444 if mesh.active_uv_texture and mesh.active_uv_texture.data[i]:
445 uvFace=mesh.active_uv_texture.data[i]
447 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
449 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
454 return ((0, 0), (0, 0), (0, 0), (0, 0))
457 def setFaceUV(m, i, face, uv_array, image):
458 uv_face=m.uv_textures[0].data[i]
465 def vertsDelete(m, remove_vertices):
467 bpy.ops.mesh.select_all(action='DESELECT')
470 for i in remove_vertices:
471 m.verts[i].select=True
474 bpy.ops.mesh.delete(type='VERT')
478 def setSmooth(m, smoothing):
479 m.autosmooth_angle=int(smoothing)
483 def recalcNormals(mesh_object):
484 bpy.ops.object.select_all(action='DESELECT')
485 object.activate(mesh_object)
487 bpy.ops.mesh.normals_make_consistent()
495 def addMaterial(m, material):
496 m.add_material(material)
499 def getMaterial(m, index):
500 return m.materials[index]
505 def setNormal(v, normal):
506 v.normal=mathutils.Vector(normal)
520 def getVertexCount(face):
521 return len(face.verts)
524 def getVertices(face):
528 def getIndices(face, count=3):
530 return [face.verts[0], face.verts[1], face.verts[2]]
532 return [face.verts[0], face.verts[1], face.verts[2], face.verts[3]]
537 def setMaterial(face, material_index):
538 face.material_index=material_index
541 def getMaterialIndex(face):
542 return face.material_index
545 def setNormal(face, normal):
553 def setSmooth(face, isSmooth):
554 face.smooth=True if isSmooth else False
561 armature = bpy.data.armatures.new('Armature')
562 armature_object=bpy.data.objects.new('Armature', armature)
563 SCENE.objects.link(armature_object)
565 armature_object.x_ray=True
566 armature.draw_names=True
567 #armature.drawtype='OCTAHEDRAL'
568 armature.drawtype='STICK'
569 armature.deform_envelope=False
570 armature.deform_vertexgroups=True
571 armature.x_axis_mirror=True
573 return armature, armature_object
576 def makeEditable(armature_object):
578 # select only armature object and set edit mode
579 SCENE.objects.active=armature_object
580 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
581 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
584 def createIkConstraint(armature_object, p_bone, effector_name, ik):
585 constraint = p_bone.constraints.new('IK')
586 constraint.chain_length=len(ik.children)
587 constraint.target=armature_object
588 constraint.subtarget=effector_name
589 constraint.use_tail=False
590 # not used. place folder when export.
591 constraint.weight=ik.weight
592 constraint.iterations=ik.iterations * 10
596 def createBone(armature, name):
597 return armature.edit_bones.new(name)
600 def update(armature):
606 def setConnected(bone):
614 def setLayerMask(bone, layers):
618 layer.append(True if layers[i]!=0 else False)
625 return b.head_local[0:3]
629 return b.tail_local[0:3]
635 return c.chain_length
646 def ikRotationWeight(c):