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))
70 class ProgressBar(object):
74 def __init__(self, base):
75 print("#### %s ####" % base)
77 self.start=time.time()
78 self.set('<start>', 0)
80 def advance(self, message, progress):
81 self.progress+=float(progress)
84 def set(self, message, progress):
85 self.progress=float(progress)
88 def _print(self, message):
90 message="%s: %s" % (self.base, message)
91 #Blender.Window.DrawProgressBar(self.progress, message)
95 message='finished in %.2f sec' % (time.time()-self.start)
96 self.set(message, 1.0)
98 def progress_start(base):
100 print("#### progressBar ####")
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)
128 def createEmpty(name):
130 empty=bpy.data.objects.new(name, None)
131 SCENE.objects.link(empty)
136 for o in SCENE.objects:
140 def makeParent(parent, child):
146 bpy.ops.object.select_all(action='DESELECT')
148 SCENE.objects.active=o
149 bpy.ops.object.duplicate()
150 dumy=SCENE.objects.active
151 #bpy.ops.object.rotation_apply()
152 #bpy.ops.object.scale_apply()
153 #bpy.ops.object.location_apply()
154 return dumy.data, dumy
159 SCENE.objects.unlink(o)
173 SCENE.objects.active=o
178 return SCENE.objects.active
182 bpy.ops.object.select_all(action='DESELECT')
185 def setLayerMask(object, layers):
189 layer.append(True if layers[i]!=0 else False)
196 return False if o.hide else True
200 return o.data.shape_keys.key_blocks
203 def addShapeKey(o, name):
205 return o.shape_key_add(name)
207 return o.add_shape_key(name)
211 return o.data.shape_keys
214 def pinShape(o, enable):
215 o.show_only_shape_key=enable
218 def setActivateShapeKey(o, index):
219 o.active_shape_key_index=index
226 def getVertexGroup(o, name):
228 for i, v in enumerate(o.data.vertices):
230 if o.vertex_groups[g.group].name==name:
235 def getVertexGroupNames(o):
236 for g in o.vertex_groups:
240 def addVertexGroup(o, name):
241 o.vertex_groups.new(name)
244 def assignVertexGroup(o, name, index, weight):
245 o.vertex_groups[name].add([index], weight, 'ADD')
248 def createBoneGroup(o, name, color_set='DEFAULT'):
252 bpy.ops.pose.group_add()
254 pose=object.getPose(o)
255 g=pose.bone_groups.active
257 g.color_set=color_set
261 return object.getPose(o).bone_groups
266 def addMirror(mesh_object):
267 return mesh_object.modifiers.new("Modifier", "MIRROR")
270 def addArmature(mesh_object, armature_object):
271 mod=mesh_object.modifiers.new("Modifier", "ARMATURE")
272 mod.object = armature_object
273 mod.use_bone_envelopes=False
276 def hasType(mesh_object, type_name):
277 for mod in mesh_object.modifiers:
278 if mod.type==type_name.upper():
282 def isType(m, type_name):
283 return m.type==type_name.upper()
286 def getArmatureObject(m):
292 def assign(shapeKey, index, pos):
293 shapeKey.data[index].co=pos
296 def getByIndex(b, index):
297 return b.data[index].co
308 texture=bpy.data.textures.new(os.path.basename(path), 'IMAGE')
309 texture.use_mipmap=True
310 texture.use_interpolation=True
311 texture.use_alpha=True
313 image=bpy.data.images.load(path)
315 image=bpy.data.images.new('Image', width=16, height=16)
317 return texture, image
324 return image.filepath
330 return bpy.data.materials.new(name)
333 def get(material_name):
334 return bpy.data.materials[material_name]
337 def addTexture(material, texture, enable=True):
340 for i, slot in enumerate(material.texture_slots):
347 #material.add_texture(texture, "UV", "COLOR")
348 #slot=material.texture_slots.add()
349 slot=material.texture_slots.create(index)
351 slot.texture_coords='UV'
352 slot.blend_type='MULTIPLY'
353 slot.use_map_alpha=True
358 def getTexture(m, index):
359 return m.texture_slots[index].texture
363 return m.texture_slots[0]
366 def setUseTexture(m, index, enable):
367 m.use_textures[index]=enable
370 def eachTexturePath(m):
371 for slot in m.texture_slots:
372 if slot and slot.texture:
374 if texture.type=="IMAGE":
381 def eachEnalbeTexturePath(m):
382 for i, slot in enumerate(m.texture_slots):
383 if m.use_textures[i] and slot and slot.texture:
385 if texture.type=="IMAGE":
396 mesh=bpy.data.meshes.new("Mesh")
397 mesh_object= bpy.data.objects.new(name, mesh)
398 SCENE.objects.link(mesh_object)
399 return mesh, mesh_object
402 def addGeometry(mesh, vertices, faces):
403 mesh.from_pydata(vertices, [], faces)
405 mesh.add_geometry(len(vertices), 0, len(faces))
409 unpackedVertices.extend(v)
410 mesh.vertices.foreach_set("co", unpackedVertices)
416 # rotate indices if the 4th is 0
417 face = [face[3], face[0], face[1], face[2]]
420 # rotate indices if the 3rd is 0
421 face = [face[2], face[0], face[1], 0]
424 unpackedFaces.extend(face)
425 mesh.faces.foreach_set("verts_raw", unpackedFaces)
427 assert(len(vertices)==len(mesh.vertices))
428 assert(len(faces)==len(mesh.faces))
432 return len(mesh.uv_textures)>0
435 def useVertexUV(mesh):
440 mesh.uv_textures.new()
443 def hasFaceUV(mesh, i, face):
444 active_uv_texture=None
445 for t in mesh.uv_textures:
449 return active_uv_texture and active_uv_texture.data[i]
452 def getFaceUV(mesh, i, faces, count=3):
453 active_uv_texture=None
454 for t in mesh.uv_textures:
458 if active_uv_texture and active_uv_texture.data[i]:
459 uvFace=active_uv_texture.data[i]
461 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
463 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
468 return ((0, 0), (0, 0), (0, 0), (0, 0))
471 def setFaceUV(m, i, face, uv_array, image):
472 uv_face=m.uv_textures[0].data[i]
476 #uv_face.use_image=True
479 def vertsDelete(m, remove_vertices):
481 bpy.ops.mesh.select_all(action='DESELECT')
484 for i in remove_vertices:
485 m.vertices[i].select=True
488 bpy.ops.mesh.delete(type='VERT')
492 def setSmooth(m, smoothing):
493 m.auto_smooth_angle=int(smoothing)
494 m.use_auto_smooth=True
497 def recalcNormals(mesh_object):
498 bpy.ops.object.select_all(action='DESELECT')
499 object.activate(mesh_object)
501 bpy.ops.mesh.normals_make_consistent()
509 def addMaterial(m, material):
510 m.materials.append(material)
513 def getMaterial(m, index):
514 return m.materials[index]
519 def setNormal(v, normal):
520 v.normal=mathutils.Vector(normal)
534 def getVertexCount(face):
535 return len(face.vertices)
538 def getVertices(face):
539 return face.vertices[:]
542 def getIndices(face, count=3):
544 return [face.vertices[0], face.vertices[1], face.vertices[2]]
546 return [face.vertices[0], face.vertices[1], face.vertices[2], face.vertices[3]]
551 def setMaterial(face, material_index):
552 face.material_index=material_index
555 def getMaterialIndex(face):
556 return face.material_index
559 def setNormal(face, normal):
567 def setSmooth(face, isSmooth):
568 face.use_smooth=True if isSmooth else False
575 armature = bpy.data.armatures.new('Armature')
576 armature_object=bpy.data.objects.new('Armature', armature)
577 SCENE.objects.link(armature_object)
579 armature_object.show_x_ray=True
580 armature.show_names=True
581 #armature.draw_type='OCTAHEDRAL'
582 armature.draw_type='STICK'
583 armature.use_deform_envelopes=False
584 armature.use_deform_vertex_groups=True
585 armature.use_mirror_x=True
587 return armature, armature_object
590 def makeEditable(armature_object):
592 # select only armature object and set edit mode
593 SCENE.objects.active=armature_object
594 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
595 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
598 def createIkConstraint(armature_object, p_bone, effector_name, ik):
599 constraint = p_bone.constraints.new('IK')
600 constraint.chain_count=len(ik.children)
601 constraint.target=armature_object
602 constraint.subtarget=effector_name
603 constraint.use_tail=False
604 # not used. place folder when export.
605 constraint.weight=ik.weight
606 constraint.iterations=ik.iterations * 10
610 def createBone(armature, name):
611 return armature.edit_bones.new(name)
614 def update(armature):
620 def setConnected(bone):
621 bone.use_connect=True
624 def isConnected(bone):
625 return bone.use_connect
628 def setLayerMask(bone, layers):
632 layer.append(True if layers[i]!=0 else False)
639 return b.head_local[0:3]
643 return b.tail_local[0:3]
660 def ikRotationWeight(c):
667 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
668 MMD_MB_NAME='mb_name'
669 MMD_MB_COMMENT='mb_comment'
670 MMD_COMMENT='comment'
671 BASE_SHAPE_NAME='Basis'
672 RIGID_NAME='rigid_name'
673 RIGID_SHAPE_TYPE='rigid_shape_type'
674 RIGID_PROCESS_TYPE='rigid_process_type'
675 RIGID_BONE_NAME='rigid_bone_name'
676 RIGID_GROUP='ribid_group'
677 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
678 RIGID_WEIGHT='rigid_weight'
679 RIGID_LINEAR_DAMPING='rigid_linear_damping'
680 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
681 RIGID_RESTITUTION='rigid_restitution'
682 RIGID_FRICTION='rigid_friction'
683 CONSTRAINT_NAME='const_name'
684 CONSTRAINT_A='const_a'
685 CONSTRAINT_B='const_b'
686 CONSTRAINT_POS_MIN='const_pos_min'
687 CONSTRAINT_POS_MAX='const_pos_max'
688 CONSTRAINT_ROT_MIN='const_rot_min'
689 CONSTRAINT_ROT_MAX='const_rot_max'
690 CONSTRAINT_SPRING_POS='const_spring_pos'
691 CONSTRAINT_SPRING_ROT='const_spring_rot'
692 TOON_TEXTURE_OBJECT='ToonTextures'