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
22 def initialize(name, scene):
35 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
37 def enterObjectMode():
38 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
41 bpy.ops.object.mode_set(mode='POSE', toggle=False)
43 def createVector(x, y, z):
44 return mathutils.Vector([x, y, z])
51 def __init__(self, path, encoding):
52 self.io=open(path, "wb")
53 self.encoding=encoding
56 self.io.write(s.encode(self.encoding))
65 class ProgressBar(object):
69 def __init__(self, base):
70 print("#### %s ####" % base)
72 self.start=time.time()
73 self.set('<start>', 0)
75 def advance(self, message, progress):
76 self.progress+=float(progress)
79 def set(self, message, progress):
80 self.progress=float(progress)
83 def _print(self, message):
85 message="%s: %s" % (self.base, message)
86 #Blender.Window.DrawProgressBar(self.progress, message)
90 message='finished in %.2f sec' % (time.time()-self.start)
91 self.set(message, 1.0)
93 def progress_start(base):
95 progressBar=ProgressBar(base)
97 def progress_finish():
101 def progress_print(message, progress=0.05):
103 progressBar.advance(message, progress)
105 def progress_set(message, progress):
107 progressBar.set(message, progress)
118 def createEmpty(name):
120 empty=bpy.data.objects.new(name, None)
121 SCENE.objects.link(empty)
125 def makeParent(parent, child):
131 bpy.ops.object.select_all(action='DESELECT')
133 SCENE.objects.active=o
134 bpy.ops.object.duplicate()
135 dumy=SCENE.objects.active
136 #bpy.ops.object.rotation_apply()
137 #bpy.ops.object.scale_apply()
138 #bpy.ops.object.location_apply()
139 return dumy.data, dumy
144 SCENE.objects.unlink(o)
158 SCENE.objects.active=o
163 return SCENE.objects.active
167 bpy.ops.object.select_all(action='DESELECT')
170 def setLayerMask(object, layers):
174 layer.append(True if layers[i]!=0 else False)
181 return o.restrict_view
185 return o.data.shape_keys.keys
188 def addShapeKey(o, name):
189 return o.add_shape_key(name)
193 return o.data.shape_keys
196 def pinShape(o, enable):
197 o.shape_key_lock=enable
200 def setActivateShapeKey(o, index):
201 o.active_shape_key_index=index
208 def getVertexGroup(o, name):
210 for i, v in enumerate(o.data.verts):
212 if o.vertex_groups[g.group].name==name:
217 def getVertexGroupNames(o):
218 for g in o.vertex_groups:
222 def addVertexGroup(o, name):
223 o.add_vertex_group(name)
226 def assignVertexGroup(o, name, index, weight):
227 o.add_vertex_to_group(index,
228 o.vertex_groups[name], weight, 'ADD')
231 def createBoneGroup(o, name, color_set='DEFAULT'):
235 bpy.ops.pose.group_add()
237 pose=object.getPose(o)
238 g=pose.active_bone_group
240 g.color_set=color_set
244 return object.getPose(o).bone_groups
249 def addMirror(mesh_object):
250 return mesh_object.modifiers.new("Modifier", "MIRROR")
253 def addArmature(mesh_object, armature_object):
254 mod=mesh_object.modifiers.new("Modifier", "ARMATURE")
255 mod.object = armature_object
256 mod.use_bone_envelopes=False
259 def hasType(mesh_object, type_name):
260 for mod in mesh_object.modifiers:
261 if mod.type==type_name.upper():
265 def isType(m, type_name):
266 return m.type==type_name.upper()
269 def getArmatureObject(m):
275 def assign(shapeKey, index, pos):
276 shapeKey.data[index].co=pos
279 def getByIndex(b, index):
280 return b.data[index].co
291 texture=bpy.data.textures.new(os.path.basename(path))
293 texture=texture.recast_type()
294 image=bpy.data.images.load(path)
297 texture.interpolation=True
298 texture.use_alpha=True
299 return texture, image
306 return image.filename
312 return bpy.data.materials.new(name)
315 def get(material_name):
316 return bpy.data.materials[material_name]
319 def addTexture(material, texture, enable=True):
322 for i, slot in enumerate(material.texture_slots):
330 material.add_texture(texture, "UV", "COLOR")
331 slot=material.texture_slots[index]
332 slot.blend_type='MULTIPLY'
335 material.add_texture(texture)
336 material.use_textures[index]=False
340 def getTexture(material, index):
341 return material.texture_slots[index].texture
344 def hasTexture(material):
345 return material.texture_slots[0]
348 def setUseTexture(material, index, enable):
349 material.use_textures[index]=enable
352 def eachTexturePath(m):
353 for slot in m.texture_slots:
354 if slot and slot.texture:
356 if texture.type=="IMAGE":
367 mesh=bpy.data.meshes.new("Mesh")
368 mesh_object= bpy.data.objects.new(name, mesh)
369 SCENE.objects.link(mesh_object)
370 return mesh, mesh_object
373 def addGeometry(mesh, vertices, faces):
374 mesh.from_pydata(vertices, [], faces)
376 mesh.add_geometry(len(vertices), 0, len(faces))
380 unpackedVertices.extend(v)
381 mesh.verts.foreach_set("co", unpackedVertices)
387 # rotate indices if the 4th is 0
388 face = [face[3], face[0], face[1], face[2]]
391 # rotate indices if the 3rd is 0
392 face = [face[2], face[0], face[1], 0]
395 unpackedFaces.extend(face)
396 mesh.faces.foreach_set("verts_raw", unpackedFaces)
398 assert(len(vertices)==len(mesh.verts))
399 assert(len(faces)==len(mesh.faces))
403 return mesh.active_uv_texture
406 def useVertexUV(mesh):
411 mesh.add_uv_texture()
414 def hasFaceUV(mesh, i, face):
415 return mesh.active_uv_texture and mesh.active_uv_texture.data[i]
418 def getFaceUV(mesh, i, faces, count=3):
419 if mesh.active_uv_texture and mesh.active_uv_texture.data[i]:
420 uvFace=mesh.active_uv_texture.data[i]
422 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
424 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
429 return ((0, 0), (0, 0), (0, 0), (0, 0))
432 def setFaceUV(m, i, face, uv_array, image):
433 uv_face=m.uv_textures[0].data[i]
440 def vertsDelete(m, remove_vertices):
442 bpy.ops.mesh.select_all(action='DESELECT')
445 for i in remove_vertices:
446 m.verts[i].selected=True
449 bpy.ops.mesh.delete(type='VERT')
453 def setSmooth(m, smoothing):
454 m.autosmooth_angle=int(smoothing)
458 def recalcNormals(mesh_object):
459 bpy.ops.object.select_all(action='DESELECT')
460 object.activate(mesh_object)
462 bpy.ops.mesh.normals_make_consistent()
470 def addMaterial(m, material):
471 m.add_material(material)
474 def getMaterial(m, index):
475 return m.materials[index]
480 def setNormal(mvert, normal):
481 mvert.normal=mathutils.Vector(normal)
484 def setUv(mvert, uv):
490 def getVertexCount(face):
491 return len(face.verts)
494 def getVertices(face):
498 def getIndices(face, count=3):
500 return [face.verts[0], face.verts[1], face.verts[2]]
502 return [face.verts[0], face.verts[1], face.verts[2], face.verts[3]]
507 def setMaterial(face, material_index):
508 face.material_index=material_index
511 def getMaterialIndex(face):
512 return face.material_index
515 def setNormal(face, normal):
523 def setSmooth(face, isSmooth):
524 face.smooth=True if isSmooth else False
531 armature = bpy.data.armatures.new('Armature')
532 armature_object=bpy.data.objects.new('Armature', armature)
533 SCENE.objects.link(armature_object)
535 armature_object.x_ray=True
536 armature.draw_names=True
537 #armature.drawtype='OCTAHEDRAL'
538 armature.drawtype='STICK'
539 armature.deform_envelope=False
540 armature.deform_vertexgroups=True
541 armature.x_axis_mirror=True
543 return armature, armature_object
546 def makeEditable(armature_object):
548 # select only armature object and set edit mode
549 SCENE.objects.active=armature_object
550 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
551 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
554 def createIkConstraint(armature_object, p_bone, effector_name, ik):
555 constraint = p_bone.constraints.new('IK')
556 constraint.chain_length=len(ik.children)
557 constraint.target=armature_object
558 constraint.subtarget=effector_name
559 constraint.use_tail=False
560 # not used. place folder when export.
561 constraint.weight=ik.weight
562 constraint.iterations=ik.iterations * 10
566 def createBone(armature, name):
567 return armature.edit_bones.new(name)
570 def update(armature):
576 def setConnected(bone):
584 def setLayerMask(bone, layers):
588 layer.append(True if layers[i]!=0 else False)
595 return b.head_local[0:3]
599 return b.tail_local[0:3]
605 return c.chain_length
616 def ikRotationWeight(c):