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)
277 texture.mipmap = True
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", "ALPHA"})
295 material.add_texture(texture, "UV", "COLOR")
298 def hasTexture(material):
299 return material.texture_slots[0]
302 def getTexturePath(m, dirname):
306 for slot in m.texture_slots:
307 if slot and slot.texture:
309 if texture.type=="IMAGE":
313 imagePath=image.filename
314 if len(dirname)>0 and imagePath.startswith(dirname):
315 #
\e$BAjBP%Q%9$KJQ49$9$k
\e(B
316 imagePath=imagePath[len(dirname)+1:len(imagePath)]
317 #imagePath=Blender.sys.expandpath(
318 # imagePath).replace("\\", '/')
319 if slot.map_colordiff:
320 tex=" tex(\"%s\")" % imagePath
322 aplane=" aplane(\"%s\")" % imagePath
330 mesh=bpy.data.meshes.new("Mesh")
331 mesh_object= bpy.data.objects.new(name, mesh)
332 SCENE.objects.link(mesh_object)
333 return mesh, mesh_object
336 def addGeometry(mesh, vertices, faces):
337 mesh.from_pydata(vertices, [], faces)
339 mesh.add_geometry(len(vertices), 0, len(faces))
343 unpackedVertices.extend(v)
344 mesh.verts.foreach_set("co", unpackedVertices)
350 # rotate indices if the 4th is 0
351 face = [face[3], face[0], face[1], face[2]]
354 # rotate indices if the 3rd is 0
355 face = [face[2], face[0], face[1], 0]
358 unpackedFaces.extend(face)
359 mesh.faces.foreach_set("verts_raw", unpackedFaces)
361 assert(len(vertices)==len(mesh.verts))
362 assert(len(faces)==len(mesh.faces))
366 return mesh.active_uv_texture
369 def useVertexUV(mesh):
374 mesh.add_uv_texture()
377 def hasFaceUV(mesh, i, face):
378 return mesh.active_uv_texture.data[i]
381 def getFaceUV(mesh, i, faces, count=3):
382 uvFace=mesh.active_uv_texture.data[i]
384 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
386 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
392 def setFaceUV(mesh, i, face, uv_array, image):
393 uv_face=mesh.uv_textures[0].data[i]
400 def vertsDelete(mesh, remove_vertices):
402 bpy.ops.mesh.select_all(action='DESELECT')
405 for i in remove_vertices:
406 mesh.verts[i].selected=True
409 bpy.ops.mesh.delete(type='VERT')
413 def setSmooth(mesh, smoothing):
414 mesh.autosmooth_angle=int(smoothing)
418 def recalcNormals(mesh_object):
419 bpy.ops.object.select_all(action='DESELECT')
420 object.activate(mesh_object)
422 bpy.ops.mesh.normals_make_consistent()
426 def flipNormals(mesh):
430 def addMaterial(mesh, material):
431 mesh.add_material(material)
436 def setNormal(mvert, normal):
437 mvert.normal=mathutils.Vector(normal)
440 def setUv(mvert, uv):
446 def getVertexCount(face):
447 return len(face.verts)
450 def getVertices(face):
454 def getIndices(face):
455 return [face.verts[0], face.verts[1], face.verts[2]]
458 def setMaterial(face, material_index):
459 face.material_index=material_index
462 def getMaterialIndex(face):
463 return face.material_index
466 def setNormal(face, normal):
474 def setSmooth(face, isSmooth):
475 face.smooth=True if isSmooth else False
482 armature = bpy.data.armatures.new('Armature')
483 armature_object=bpy.data.objects.new('Armature', armature)
484 SCENE.objects.link(armature_object)
486 armature_object.x_ray=True
487 armature.draw_names=True
488 armature.drawtype='OCTAHEDRAL'
489 armature.deform_envelope=False
490 armature.deform_vertexgroups=True
491 armature.x_axis_mirror=True
493 return armature, armature_object
496 def makeEditable(armature_object):
498 # select only armature object and set edit mode
499 SCENE.objects.active=armature_object
500 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
501 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
504 def createIkConstraint(armature_object, p_bone, effector_name, ik):
505 constraint = p_bone.constraints.new('IK')
506 constraint.chain_length=len(ik.children)
507 constraint.target=armature_object
508 constraint.subtarget=effector_name
509 constraint.use_tail=False
510 # not used. place folder when export.
511 constraint.weight=ik.weight
512 constraint.iterations=ik.iterations * 10
516 def createBone(armature, name):
517 return armature.edit_bones.new(name)
520 def update(armature):
526 def setConnected(bone):
534 def setLayerMask(bone, layers):
538 layer.append(True if layers[i]!=0 else False)
545 return b.head_local[0:3]
549 return b.tail_local[0:3]
555 return c.chain_length
566 def ikRotationWeight(c):