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 if name not in o.vertex_groups:
246 o.vertex_groups.new(name)
247 o.vertex_groups[name].add([index], weight, 'ADD')
250 def createBoneGroup(o, name, color_set='DEFAULT'):
254 bpy.ops.pose.group_add()
256 pose=object.getPose(o)
257 g=pose.bone_groups.active
259 g.color_set=color_set
264 return object.getPose(o).bone_groups
269 def addMirror(mesh_object):
270 return mesh_object.modifiers.new("Modifier", "MIRROR")
273 def addArmature(mesh_object, armature_object):
274 mod=mesh_object.modifiers.new("Modifier", "ARMATURE")
275 mod.object = armature_object
276 mod.use_bone_envelopes=False
279 def hasType(mesh_object, type_name):
280 for mod in mesh_object.modifiers:
281 if mod.type==type_name.upper():
285 def isType(m, type_name):
286 return m.type==type_name.upper()
289 def getArmatureObject(m):
295 def assign(shapeKey, index, pos):
296 shapeKey.data[index].co=pos
299 def getByIndex(b, index):
300 return b.data[index].co
311 texture=bpy.data.textures.new(os.path.basename(path), 'IMAGE')
312 texture.use_mipmap=True
313 texture.use_interpolation=True
314 texture.use_alpha=True
316 image=bpy.data.images.load(path)
318 image=bpy.data.images.new('Image', width=16, height=16)
320 return texture, image
327 return image.filepath
333 return bpy.data.materials.new(name)
336 def get(material_name):
337 return bpy.data.materials[material_name]
340 def addTexture(material, texture, enable=True):
343 for i, slot in enumerate(material.texture_slots):
350 #material.add_texture(texture, "UV", "COLOR")
351 #slot=material.texture_slots.add()
352 slot=material.texture_slots.create(index)
354 slot.texture_coords='UV'
355 slot.blend_type='MULTIPLY'
356 slot.use_map_alpha=True
361 def getTexture(m, index):
362 return m.texture_slots[index].texture
366 return m.texture_slots[0]
369 def setUseTexture(m, index, enable):
370 m.use_textures[index]=enable
373 def eachTexturePath(m):
374 for slot in m.texture_slots:
375 if slot and slot.texture:
377 if texture.type=="IMAGE":
384 def eachEnalbeTexturePath(m):
385 for i, slot in enumerate(m.texture_slots):
386 if m.use_textures[i] and slot and slot.texture:
388 if texture.type=="IMAGE":
399 mesh=bpy.data.meshes.new("Mesh")
400 mesh_object= bpy.data.objects.new(name, mesh)
401 SCENE.objects.link(mesh_object)
402 return mesh, mesh_object
405 def addGeometry(mesh, vertices, faces):
406 mesh.from_pydata(vertices, [], faces)
408 mesh.add_geometry(len(vertices), 0, len(faces))
412 unpackedVertices.extend(v)
413 mesh.vertices.foreach_set("co", unpackedVertices)
419 # rotate indices if the 4th is 0
420 face = [face[3], face[0], face[1], face[2]]
423 # rotate indices if the 3rd is 0
424 face = [face[2], face[0], face[1], 0]
427 unpackedFaces.extend(face)
428 mesh.faces.foreach_set("verts_raw", unpackedFaces)
430 assert(len(vertices)==len(mesh.vertices))
431 assert(len(faces)==len(mesh.faces))
435 return len(mesh.uv_textures)>0
438 def useVertexUV(mesh):
443 mesh.uv_textures.new()
446 def hasFaceUV(mesh, i, face):
447 active_uv_texture=None
448 for t in mesh.uv_textures:
452 return active_uv_texture and active_uv_texture.data[i]
455 def getFaceUV(mesh, i, faces, count=3):
456 active_uv_texture=None
457 for t in mesh.uv_textures:
461 if active_uv_texture and active_uv_texture.data[i]:
462 uvFace=active_uv_texture.data[i]
464 return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
466 return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
471 return ((0, 0), (0, 0), (0, 0), (0, 0))
474 def setFaceUV(m, i, face, uv_array, image):
475 uv_face=m.uv_textures[0].data[i]
479 #uv_face.use_image=True
482 def vertsDelete(m, remove_vertices):
484 bpy.ops.mesh.select_all(action='DESELECT')
487 for i in remove_vertices:
488 m.vertices[i].select=True
491 bpy.ops.mesh.delete(type='VERT')
495 def setSmooth(m, smoothing):
496 m.auto_smooth_angle=int(smoothing)
497 m.use_auto_smooth=True
500 def recalcNormals(mesh_object):
501 bpy.ops.object.select_all(action='DESELECT')
502 object.activate(mesh_object)
504 bpy.ops.mesh.normals_make_consistent()
512 def addMaterial(m, material):
513 m.materials.append(material)
516 def getMaterial(m, index):
517 return m.materials[index]
522 def setNormal(v, normal):
523 v.normal=mathutils.Vector(normal)
537 def getVertexCount(face):
538 return len(face.vertices)
541 def getVertices(face):
542 return face.vertices[:]
545 def getIndices(face, count=3):
547 return [face.vertices[0], face.vertices[1], face.vertices[2]]
549 return [face.vertices[0], face.vertices[1], face.vertices[2], face.vertices[3]]
554 def setMaterial(face, material_index):
555 face.material_index=material_index
558 def getMaterialIndex(face):
559 return face.material_index
562 def setNormal(face, normal):
570 def setSmooth(face, isSmooth):
571 face.use_smooth=True if isSmooth else False
578 armature = bpy.data.armatures.new('Armature')
579 armature_object=bpy.data.objects.new('Armature', armature)
580 SCENE.objects.link(armature_object)
582 armature_object.show_x_ray=True
583 armature.show_names=True
584 #armature.draw_type='OCTAHEDRAL'
585 armature.draw_type='STICK'
586 armature.use_deform_envelopes=False
587 armature.use_deform_vertex_groups=True
588 armature.use_mirror_x=True
590 return armature, armature_object
593 def makeEditable(armature_object):
595 # select only armature object and set edit mode
596 SCENE.objects.active=armature_object
597 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
598 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
601 def createIkConstraint(armature_object, p_bone, effector_name,
602 chain, weight, iterations):
603 constraint = p_bone.constraints.new('IK')
604 constraint.chain_count=len(chain)
605 constraint.target=armature_object
606 constraint.subtarget=effector_name
607 constraint.use_tail=False
609 # not used. place folder when export.
610 #constraint.weight=weight
611 #constraint.iterations=iterations * 10
615 def createBone(armature, name):
616 return armature.edit_bones.new(name)
619 def update(armature):
625 def setConnected(bone):
626 bone.use_connect=True
629 def isConnected(bone):
630 return bone.use_connect
633 def setLayerMask(bone, layers):
637 layer.append(True if layers[i]!=0 else False)
644 return b.head_local[0:3]
648 return b.tail_local[0:3]
665 def ikRotationWeight(c):
672 MMD_SHAPE_GROUP_NAME='_MMD_SHAPE'
673 MMD_MB_NAME='mb_name'
674 MMD_MB_COMMENT='mb_comment'
675 MMD_COMMENT='comment'
676 BASE_SHAPE_NAME='Basis'
677 RIGID_NAME='rigid_name'
678 RIGID_SHAPE_TYPE='rigid_shape_type'
679 RIGID_PROCESS_TYPE='rigid_process_type'
680 RIGID_BONE_NAME='rigid_bone_name'
681 RIGID_GROUP='ribid_group'
682 RIGID_INTERSECTION_GROUP='rigid_intersection_group'
683 RIGID_WEIGHT='rigid_weight'
684 RIGID_LINEAR_DAMPING='rigid_linear_damping'
685 RIGID_ANGULAR_DAMPING='rigid_angular_damping'
686 RIGID_RESTITUTION='rigid_restitution'
687 RIGID_FRICTION='rigid_friction'
688 CONSTRAINT_NAME='const_name'
689 CONSTRAINT_A='const_a'
690 CONSTRAINT_B='const_b'
691 CONSTRAINT_POS_MIN='const_pos_min'
692 CONSTRAINT_POS_MAX='const_pos_max'
693 CONSTRAINT_ROT_MIN='const_rot_min'
694 CONSTRAINT_ROT_MAX='const_rot_max'
695 CONSTRAINT_SPRING_POS='const_spring_pos'
696 CONSTRAINT_SPRING_ROT='const_spring_rot'
697 TOON_TEXTURE_OBJECT='ToonTextures'