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
262 return object.getPose(o).bone_groups
267 def addMirror(mesh_object):
268 return mesh_object.modifiers.new("Modifier", "MIRROR")
271 def addArmature(mesh_object, armature_object):
272 mod=mesh_object.modifiers.new("Modifier", "ARMATURE")
273 mod.object = armature_object
274 mod.use_bone_envelopes=False
277 def hasType(mesh_object, type_name):
278 for mod in mesh_object.modifiers:
279 if mod.type==type_name.upper():
283 def isType(m, type_name):
284 return m.type==type_name.upper()
287 def getArmatureObject(m):
293 def assign(shapeKey, index, pos):
294 shapeKey.data[index].co=pos
297 def getByIndex(b, index):
298 return b.data[index].co
309 texture=bpy.data.textures.new(os.path.basename(path), 'IMAGE')
310 texture.use_mipmap=True
311 texture.use_interpolation=True
312 texture.use_alpha=True
314 image=bpy.data.images.load(path)
316 image=bpy.data.images.new('Image', width=16, height=16)
318 return texture, image
325 return image.filepath
331 return bpy.data.materials.new(name)
334 def get(material_name):
335 return bpy.data.materials[material_name]
338 def addTexture(material, texture, enable=True):
341 for i, slot in enumerate(material.texture_slots):
348 #material.add_texture(texture, "UV", "COLOR")
349 #slot=material.texture_slots.add()
350 slot=material.texture_slots.create(index)
352 slot.texture_coords='UV'
353 slot.blend_type='MULTIPLY'
354 slot.use_map_alpha=True
359 def getTexture(m, index):
360 return m.texture_slots[index].texture
364 return m.texture_slots[0]
367 def setUseTexture(m, index, enable):
368 m.use_textures[index]=enable
371 def eachTexturePath(m):
372 for slot in m.texture_slots:
373 if slot and slot.texture:
375 if texture.type=="IMAGE":
382 def eachEnalbeTexturePath(m):
383 for i, slot in enumerate(m.texture_slots):
384 if m.use_textures[i] and slot and slot.texture:
386 if texture.type=="IMAGE":
397 mesh=bpy.data.meshes.new("Mesh")
398 mesh_object= bpy.data.objects.new(name, mesh)
399 SCENE.objects.link(mesh_object)
400 return mesh, mesh_object
403 def addGeometry(mesh, vertices, faces):
404 mesh.from_pydata(vertices, [], faces)
406 mesh.add_geometry(len(vertices), 0, len(faces))
410 unpackedVertices.extend(v)
411 mesh.vertices.foreach_set("co", unpackedVertices)
417 # rotate indices if the 4th is 0
418 face = [face[3], face[0], face[1], face[2]]
421 # rotate indices if the 3rd is 0
422 face = [face[2], face[0], face[1], 0]
425 unpackedFaces.extend(face)
426 mesh.faces.foreach_set("verts_raw", unpackedFaces)
428 assert(len(vertices)==len(mesh.vertices))
429 assert(len(faces)==len(mesh.faces))
433 return len(mesh.uv_textures)>0
436 def useVertexUV(mesh):
441 mesh.uv_textures.new()
444 def hasFaceUV(mesh, i, face):
445 active_uv_texture=None
446 for t in mesh.uv_textures:
450 return active_uv_texture and active_uv_texture.data[i]
453 def getFaceUV(mesh, i, faces, count=3):
454 active_uv_texture=None
455 for t in mesh.uv_textures:
459 if active_uv_texture and active_uv_texture.data[i]:
460 uvFace=active_uv_texture.data[i]
462 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
464 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
469 return ((0, 0), (0, 0), (0, 0), (0, 0))
472 def setFaceUV(m, i, face, uv_array, image):
473 uv_face=m.uv_textures[0].data[i]
477 #uv_face.use_image=True
480 def vertsDelete(m, remove_vertices):
482 bpy.ops.mesh.select_all(action='DESELECT')
485 for i in remove_vertices:
486 m.vertices[i].select=True
489 bpy.ops.mesh.delete(type='VERT')
493 def setSmooth(m, smoothing):
494 m.auto_smooth_angle=int(smoothing)
495 m.use_auto_smooth=True
498 def recalcNormals(mesh_object):
499 bpy.ops.object.select_all(action='DESELECT')
500 object.activate(mesh_object)
502 bpy.ops.mesh.normals_make_consistent()
510 def addMaterial(m, material):
511 m.materials.append(material)
514 def getMaterial(m, index):
515 return m.materials[index]
520 def setNormal(v, normal):
521 v.normal=mathutils.Vector(normal)
535 def getVertexCount(face):
536 return len(face.vertices)
539 def getVertices(face):
540 return face.vertices[:]
543 def getIndices(face, count=3):
545 return [face.vertices[0], face.vertices[1], face.vertices[2]]
547 return [face.vertices[0], face.vertices[1], face.vertices[2], face.vertices[3]]
552 def setMaterial(face, material_index):
553 face.material_index=material_index
556 def getMaterialIndex(face):
557 return face.material_index
560 def setNormal(face, normal):
568 def setSmooth(face, isSmooth):
569 face.use_smooth=True if isSmooth else False
576 armature = bpy.data.armatures.new('Armature')
577 armature_object=bpy.data.objects.new('Armature', armature)
578 SCENE.objects.link(armature_object)
580 armature_object.show_x_ray=True
581 armature.show_names=True
582 #armature.draw_type='OCTAHEDRAL'
583 armature.draw_type='STICK'
584 armature.use_deform_envelopes=False
585 armature.use_deform_vertex_groups=True
586 armature.use_mirror_x=True
588 return armature, armature_object
591 def makeEditable(armature_object):
593 # select only armature object and set edit mode
594 SCENE.objects.active=armature_object
595 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
596 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
599 def createIkConstraint(armature_object, p_bone, effector_name,
600 chain, weight, iterations):
601 constraint = p_bone.constraints.new('IK')
602 constraint.chain_count=len(chain)
603 constraint.target=armature_object
604 constraint.subtarget=effector_name
605 constraint.use_tail=False
607 # not used. place folder when export.
608 #constraint.weight=weight
609 #constraint.iterations=iterations * 10
613 def createBone(armature, name):
614 return armature.edit_bones.new(name)
617 def update(armature):
623 def setConnected(bone):
624 bone.use_connect=True
627 def isConnected(bone):
628 return bone.use_connect
631 def setLayerMask(bone, layers):
635 layer.append(True if layers[i]!=0 else False)
642 return b.head_local[0:3]
646 return b.tail_local[0:3]
663 def ikRotationWeight(c):
670 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
671 MMD_MB_NAME='mb_name'
672 MMD_MB_COMMENT='mb_comment'
673 MMD_COMMENT='comment'
674 BASE_SHAPE_NAME='Basis'
675 RIGID_NAME='rigid_name'
676 RIGID_SHAPE_TYPE='rigid_shape_type'
677 RIGID_PROCESS_TYPE='rigid_process_type'
678 RIGID_BONE_NAME='rigid_bone_name'
679 RIGID_GROUP='ribid_group'
680 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
681 RIGID_WEIGHT='rigid_weight'
682 RIGID_LINEAR_DAMPING='rigid_linear_damping'
683 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
684 RIGID_RESTITUTION='rigid_restitution'
685 RIGID_FRICTION='rigid_friction'
686 CONSTRAINT_NAME='const_name'
687 CONSTRAINT_A='const_a'
688 CONSTRAINT_B='const_b'
689 CONSTRAINT_POS_MIN='const_pos_min'
690 CONSTRAINT_POS_MAX='const_pos_max'
691 CONSTRAINT_ROT_MIN='const_rot_min'
692 CONSTRAINT_ROT_MAX='const_rot_max'
693 CONSTRAINT_SPRING_POS='const_spring_pos'
694 CONSTRAINT_SPRING_ROT='const_spring_rot'
695 TOON_TEXTURE_OBJECT='ToonTextures'