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)
126 def createEmpty(name):
128 empty=bpy.data.objects.new(name, None)
129 SCENE.objects.link(empty)
134 for o in SCENE.objects:
138 def makeParent(parent, child):
144 bpy.ops.object.select_all(action='DESELECT')
146 SCENE.objects.active=o
147 bpy.ops.object.duplicate()
148 dumy=SCENE.objects.active
149 #bpy.ops.object.rotation_apply()
150 #bpy.ops.object.scale_apply()
151 #bpy.ops.object.location_apply()
152 return dumy.data, dumy
157 SCENE.objects.unlink(o)
171 SCENE.objects.active=o
176 return SCENE.objects.active
180 bpy.ops.object.select_all(action='DESELECT')
183 def setLayerMask(object, layers):
187 layer.append(True if layers[i]!=0 else False)
194 return False if o.hide else True
198 return o.data.shape_keys.key_blocks
201 def addShapeKey(o, name):
203 return o.shape_key_add(name)
205 return o.add_shape_key(name)
209 return o.data.shape_keys
212 def pinShape(o, enable):
213 o.show_only_shape_key=enable
216 def setActivateShapeKey(o, index):
217 o.active_shape_key_index=index
224 def getVertexGroup(o, name):
226 for i, v in enumerate(o.data.vertices):
228 if o.vertex_groups[g.group].name==name:
233 def getVertexGroupNames(o):
234 for g in o.vertex_groups:
238 def addVertexGroup(o, name):
239 o.vertex_groups.new(name)
242 def assignVertexGroup(o, name, index, weight):
243 o.vertex_groups[name].add([index], weight, 'ADD')
246 def createBoneGroup(o, name, color_set='DEFAULT'):
250 bpy.ops.pose.group_add()
252 pose=object.getPose(o)
253 g=pose.bone_groups.active
255 g.color_set=color_set
259 return object.getPose(o).bone_groups
264 def addMirror(mesh_object):
265 return mesh_object.modifiers.new("Modifier", "MIRROR")
268 def addArmature(mesh_object, armature_object):
269 mod=mesh_object.modifiers.new("Modifier", "ARMATURE")
270 mod.object = armature_object
271 mod.use_bone_envelopes=False
274 def hasType(mesh_object, type_name):
275 for mod in mesh_object.modifiers:
276 if mod.type==type_name.upper():
280 def isType(m, type_name):
281 return m.type==type_name.upper()
284 def getArmatureObject(m):
290 def assign(shapeKey, index, pos):
291 shapeKey.data[index].co=pos
294 def getByIndex(b, index):
295 return b.data[index].co
306 texture=bpy.data.textures.new(os.path.basename(path), 'IMAGE')
307 texture.use_mipmap=True
308 texture.use_interpolation=True
309 texture.use_alpha=True
311 image=bpy.data.images.load(path)
313 image=bpy.data.images.new('Image', width=16, height=16)
315 return texture, image
322 return image.filepath
328 return bpy.data.materials.new(name)
331 def get(material_name):
332 return bpy.data.materials[material_name]
335 def addTexture(material, texture, enable=True):
338 for i, slot in enumerate(material.texture_slots):
345 #material.add_texture(texture, "UV", "COLOR")
346 #slot=material.texture_slots.add()
347 slot=material.texture_slots.create(index)
349 slot.texture_coords='UV'
350 slot.blend_type='MULTIPLY'
351 slot.use_map_alpha=True
356 def getTexture(m, index):
357 return m.texture_slots[index].texture
361 return m.texture_slots[0]
364 def setUseTexture(m, index, enable):
365 m.use_textures[index]=enable
368 def eachTexturePath(m):
369 for slot in m.texture_slots:
370 if slot and slot.texture:
372 if texture.type=="IMAGE":
379 def eachEnalbeTexturePath(m):
380 for i, slot in enumerate(m.texture_slots):
381 if m.use_textures[i] and slot and slot.texture:
383 if texture.type=="IMAGE":
394 mesh=bpy.data.meshes.new("Mesh")
395 mesh_object= bpy.data.objects.new(name, mesh)
396 SCENE.objects.link(mesh_object)
397 return mesh, mesh_object
400 def addGeometry(mesh, vertices, faces):
401 mesh.from_pydata(vertices, [], faces)
403 mesh.add_geometry(len(vertices), 0, len(faces))
407 unpackedVertices.extend(v)
408 mesh.vertices.foreach_set("co", unpackedVertices)
414 # rotate indices if the 4th is 0
415 face = [face[3], face[0], face[1], face[2]]
418 # rotate indices if the 3rd is 0
419 face = [face[2], face[0], face[1], 0]
422 unpackedFaces.extend(face)
423 mesh.faces.foreach_set("verts_raw", unpackedFaces)
425 assert(len(vertices)==len(mesh.vertices))
426 assert(len(faces)==len(mesh.faces))
430 return len(mesh.uv_textures)>0
433 def useVertexUV(mesh):
438 mesh.uv_textures.new()
441 def hasFaceUV(mesh, i, face):
442 active_uv_texture=None
443 for t in mesh.uv_textures:
447 return active_uv_texture and active_uv_texture.data[i]
450 def getFaceUV(mesh, i, faces, count=3):
451 active_uv_texture=None
452 for t in mesh.uv_textures:
456 if active_uv_texture and active_uv_texture.data[i]:
457 uvFace=active_uv_texture.data[i]
459 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
461 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
466 return ((0, 0), (0, 0), (0, 0), (0, 0))
469 def setFaceUV(m, i, face, uv_array, image):
470 uv_face=m.uv_textures[0].data[i]
474 uv_face.use_image=True
477 def vertsDelete(m, remove_vertices):
479 bpy.ops.mesh.select_all(action='DESELECT')
482 for i in remove_vertices:
483 m.vertices[i].select=True
486 bpy.ops.mesh.delete(type='VERT')
490 def setSmooth(m, smoothing):
491 m.auto_smooth_angle=int(smoothing)
492 m.use_auto_smooth=True
495 def recalcNormals(mesh_object):
496 bpy.ops.object.select_all(action='DESELECT')
497 object.activate(mesh_object)
499 bpy.ops.mesh.normals_make_consistent()
507 def addMaterial(m, material):
508 m.materials.append(material)
511 def getMaterial(m, index):
512 return m.materials[index]
517 def setNormal(v, normal):
518 v.normal=mathutils.Vector(normal)
532 def getVertexCount(face):
533 return len(face.vertices)
536 def getVertices(face):
537 return face.vertices[:]
540 def getIndices(face, count=3):
542 return [face.vertices[0], face.vertices[1], face.vertices[2]]
544 return [face.vertices[0], face.vertices[1], face.vertices[2], face.vertices[3]]
549 def setMaterial(face, material_index):
550 face.material_index=material_index
553 def getMaterialIndex(face):
554 return face.material_index
557 def setNormal(face, normal):
565 def setSmooth(face, isSmooth):
566 face.use_smooth=True if isSmooth else False
573 armature = bpy.data.armatures.new('Armature')
574 armature_object=bpy.data.objects.new('Armature', armature)
575 SCENE.objects.link(armature_object)
577 armature_object.show_x_ray=True
578 armature.show_names=True
579 #armature.draw_type='OCTAHEDRAL'
580 armature.draw_type='STICK'
581 armature.use_deform_envelopes=False
582 armature.use_deform_vertex_groups=True
583 armature.use_mirror_x=True
585 return armature, armature_object
588 def makeEditable(armature_object):
590 # select only armature object and set edit mode
591 SCENE.objects.active=armature_object
592 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
593 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
596 def createIkConstraint(armature_object, p_bone, effector_name, ik):
597 constraint = p_bone.constraints.new('IK')
598 constraint.chain_count=len(ik.children)
599 constraint.target=armature_object
600 constraint.subtarget=effector_name
601 constraint.use_tail=False
602 # not used. place folder when export.
603 constraint.weight=ik.weight
604 constraint.iterations=ik.iterations * 10
608 def createBone(armature, name):
609 return armature.edit_bones.new(name)
612 def update(armature):
618 def setConnected(bone):
619 bone.use_connect=True
622 def isConnected(bone):
623 return bone.use_connect
626 def setLayerMask(bone, layers):
630 layer.append(True if layers[i]!=0 else False)
637 return b.head_local[0:3]
641 return b.tail_local[0:3]
658 def ikRotationWeight(c):
665 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
666 MMD_MB_NAME='mb_name'
667 MMD_MB_COMMENT='mb_comment'
668 MMD_COMMENT='comment'
669 BASE_SHAPE_NAME='Basis'
670 RIGID_NAME='rigid_name'
671 RIGID_SHAPE_TYPE='rigid_shape_type'
672 RIGID_PROCESS_TYPE='rigid_process_type'
673 RIGID_BONE_NAME='rigid_bone_name'
674 RIGID_GROUP='ribid_group'
675 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
676 RIGID_WEIGHT='rigid_weight'
677 RIGID_LINEAR_DAMPING='rigid_linear_damping'
678 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
679 RIGID_RESTITUTION='rigid_restitution'
680 RIGID_FRICTION='rigid_friction'
681 CONSTRAINT_NAME='const_name'
682 CONSTRAINT_A='const_a'
683 CONSTRAINT_B='const_b'
684 CONSTRAINT_POS_MIN='const_pos_min'
685 CONSTRAINT_POS_MAX='const_pos_max'
686 CONSTRAINT_ROT_MIN='const_rot_min'
687 CONSTRAINT_ROT_MAX='const_rot_max'
688 CONSTRAINT_SPRING_POS='const_spring_pos'
689 CONSTRAINT_SPRING_ROT='const_spring_rot'
690 TOON_TEXTURE_OBJECT='ToonTextures'