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)
38 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
40 def createVector(x, y, z):
41 return mathutils.Vector([x, y, z])
48 def __init__(self, path, encoding):
49 self.io=open(path, "wb")
50 self.encoding=encoding
53 self.io.write(s.encode(self.encoding))
62 class ProgressBar(object):
66 def __init__(self, base):
67 print("#### %s ####" % base)
69 self.start=time.time()
70 self.set('<start>', 0)
72 def advance(self, message, progress):
73 self.progress+=float(progress)
76 def set(self, message, progress):
77 self.progress=float(progress)
80 def _print(self, message):
82 message="%s: %s" % (self.base, message)
83 #Blender.Window.DrawProgressBar(self.progress, message)
87 message='finished in %.2f sec' % (time.time()-self.start)
88 self.set(message, 1.0)
90 def progress_start(base):
92 progressBar=ProgressBar(base)
94 def progress_finish():
98 def progress_print(message, progress=0.05):
100 progressBar.advance(message, progress)
102 def progress_set(message, progress):
104 progressBar.set(message, progress)
115 def createEmpty(name):
117 empty=bpy.data.objects.new(name, None)
118 SCENE.objects.link(empty)
122 def makeParent(parent, child):
128 bpy.ops.object.select_all(action='DESELECT')
130 SCENE.objects.active=o
131 bpy.ops.object.duplicate()
132 dumy=SCENE.objects.active
133 #bpy.ops.object.rotation_apply()
134 #bpy.ops.object.scale_apply()
135 #bpy.ops.object.location_apply()
136 return dumy.data, dumy
141 SCENE.objects.unlink(o)
155 SCENE.objects.active=o
160 return SCENE.objects.active
164 bpy.ops.object.select_all(action='DESELECT')
167 def setLayerMask(object, layers):
171 layer.append(True if layers[i]!=0 else False)
178 return o.restrict_view
182 return o.data.shape_keys.keys
185 def addShapeKey(o, name):
186 return o.add_shape_key(name)
190 return o.data.shape_keys
193 def pinShape(o, enable):
194 o.shape_key_lock=enable
197 def setActivateShapeKey(o, index):
198 o.active_shape_key_index=index
205 def getVertexGroup(o, name):
207 for i, v in enumerate(o.data.verts):
209 if o.vertex_groups[g.group].name==name:
214 def getVertexGroupNames(o):
215 for g in o.vertex_groups:
219 def addVertexGroup(o, name):
220 o.add_vertex_group(name)
223 def assignVertexGroup(o, name, index, weight):
224 o.add_vertex_to_group(index,
225 o.vertex_groups[name], weight, 'ADD')
230 def addMirror(mesh_object):
231 return mesh_object.modifiers.new("Modifier", "MIRROR")
234 def addArmature(mesh_object, armature_object):
235 mod=mesh_object.modifiers.new("Modifier", "ARMATURE")
236 mod.object = armature_object
237 mod.use_bone_envelopes=False
240 def hasType(mesh_object, type_name):
241 for mod in mesh_object.modifiers:
242 if mod.type==type_name.upper():
246 def isType(m, type_name):
247 return m.type==type_name.upper()
250 def getArmatureObject(m):
256 def assign(shapeKey, index, pos):
257 shapeKey.data[index].co=pos
260 def getByIndex(b, index):
261 return b.data[index].co
272 texture=bpy.data.textures.new(os.path.basename(path))
274 texture=texture.recast_type()
275 image=bpy.data.images.load(path)
278 texture.interpolation=True
279 texture.use_alpha=True
280 return texture, image
286 return bpy.data.materials.new(name)
289 def get(material_name):
290 return bpy.data.materials[material_name]
293 def addTexture(material, texture):
294 material.add_texture(texture, "UV", "COLOR")
295 slot=material.texture_slots[material.active_texture_index]
296 slot.blend_type='MULTIPLY'
300 def hasTexture(material):
301 return material.texture_slots[0]
304 def getTexturePath(m, dirname):
308 for slot in m.texture_slots:
309 if slot and slot.texture:
311 if texture.type=="IMAGE":
315 imagePath=image.filename
316 if len(dirname)>0 and imagePath.startswith(dirname):
317 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
318 imagePath=imagePath[len(dirname)+1:len(imagePath)]
319 #imagePath=Blender.sys.expandpath(
320 # imagePath).replace("\\", '/')
321 if slot.map_colordiff:
322 tex=" tex(\"%s\")" % imagePath
324 aplane=" aplane(\"%s\")" % imagePath
332 mesh=bpy.data.meshes.new("Mesh")
333 mesh_object= bpy.data.objects.new(name, mesh)
334 SCENE.objects.link(mesh_object)
335 return mesh, mesh_object
338 def addGeometry(mesh, vertices, faces):
339 mesh.from_pydata(vertices, [], faces)
341 mesh.add_geometry(len(vertices), 0, len(faces))
345 unpackedVertices.extend(v)
346 mesh.verts.foreach_set("co", unpackedVertices)
352 # rotate indices if the 4th is 0
353 face = [face[3], face[0], face[1], face[2]]
356 # rotate indices if the 3rd is 0
357 face = [face[2], face[0], face[1], 0]
360 unpackedFaces.extend(face)
361 mesh.faces.foreach_set("verts_raw", unpackedFaces)
363 assert(len(vertices)==len(mesh.verts))
364 assert(len(faces)==len(mesh.faces))
368 return mesh.active_uv_texture
371 def useVertexUV(mesh):
376 mesh.add_uv_texture()
379 def hasFaceUV(mesh, i, face):
380 return mesh.active_uv_texture.data[i]
383 def getFaceUV(mesh, i, faces, count=3):
384 uvFace=mesh.active_uv_texture.data[i]
386 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
388 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
394 def setFaceUV(mesh, i, face, uv_array, image):
395 uv_face=mesh.uv_textures[0].data[i]
402 def vertsDelete(mesh, remove_vertices):
404 bpy.ops.mesh.select_all(action='DESELECT')
407 for i in remove_vertices:
408 mesh.verts[i].selected=True
411 bpy.ops.mesh.delete(type='VERT')
415 def setSmooth(mesh, smoothing):
416 mesh.autosmooth_angle=int(smoothing)
420 def recalcNormals(mesh_object):
421 bpy.ops.object.select_all(action='DESELECT')
422 object.activate(mesh_object)
424 bpy.ops.mesh.normals_make_consistent()
428 def flipNormals(mesh):
432 def addMaterial(mesh, material):
433 mesh.add_material(material)
438 def setNormal(mvert, normal):
439 mvert.normal=mathutils.Vector(normal)
442 def setUv(mvert, uv):
448 def getVertexCount(face):
449 return len(face.verts)
452 def getVertices(face):
456 def getIndices(face, count=3):
458 return [face.verts[0], face.verts[1], face.verts[2]]
460 return [face.verts[0], face.verts[1], face.verts[2], face.verts[3]]
465 def setMaterial(face, material_index):
466 face.material_index=material_index
469 def getMaterialIndex(face):
470 return face.material_index
473 def setNormal(face, normal):
481 def setSmooth(face, isSmooth):
482 face.smooth=True if isSmooth else False
489 armature = bpy.data.armatures.new('Armature')
490 armature_object=bpy.data.objects.new('Armature', armature)
491 SCENE.objects.link(armature_object)
493 armature_object.x_ray=True
494 armature.draw_names=True
495 armature.drawtype='OCTAHEDRAL'
496 armature.deform_envelope=False
497 armature.deform_vertexgroups=True
498 armature.x_axis_mirror=True
500 return armature, armature_object
503 def makeEditable(armature_object):
505 # select only armature object and set edit mode
506 SCENE.objects.active=armature_object
507 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
508 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
511 def createIkConstraint(armature_object, p_bone, effector_name, ik):
512 constraint = p_bone.constraints.new('IK')
513 constraint.chain_length=len(ik.children)
514 constraint.target=armature_object
515 constraint.subtarget=effector_name
516 constraint.use_tail=False
517 # not used. place folder when export.
518 constraint.weight=ik.weight
519 constraint.iterations=ik.iterations * 10
523 def createBone(armature, name):
524 return armature.edit_bones.new(name)
527 def update(armature):
533 def setConnected(bone):
541 def setLayerMask(bone, layers):
545 layer.append(True if layers[i]!=0 else False)
552 return b.head_local[0:3]
556 return b.tail_local[0:3]
562 return c.chain_length
573 def ikRotationWeight(c):