X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=swig%2Fblender%2Fbl25.py;h=f842242591790f405983437c42bc2c34c4fccda8;hb=af0f7ea2da9ace619ff51edef564fe4669582074;hp=27ff97701801bbeffbabf6b244ea498f83b22e0f;hpb=5d19c719e669ec5245bc167eac5b571a15fd3b8d;p=meshio%2Fmeshio.git diff --git a/swig/blender/bl25.py b/swig/blender/bl25.py index 27ff977..f842242 100644 --- a/swig/blender/bl25.py +++ b/swig/blender/bl25.py @@ -1,7 +1,51 @@ +# coding: utf-8 import bpy -import os - +import mathutils +import os +import sys +import time +import functools + +# ファイルシステムの文字コード +# 改造版との共用のため +FS_ENCODING=sys.getfilesystemencoding() +if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"): + INTERNAL_ENCODING='utf-8' +else: + INTERNAL_ENCODING=FS_ENCODING + + +############################################################################### +# ProgressBar +############################################################################### +class ProgressBar(object): + def __init__(self, base): + print("#### %s ####" % base) + self.base=base + self.start=time.time() + self.set('', 0) + + def advance(self, message, progress): + self.progress+=float(progress) + self._print(message) + + def set(self, message, progress): + self.progress=float(progress) + self._print(message) + + def _print(self, message): + print(message) + message="%s: %s" % (self.base, message) + #Blender.Window.DrawProgressBar(self.progress, message) + + def finish(self): + self.progress=1.0 + message='finished in %.2f sec' % (time.time()-self.start) + self.set(message, 1.0) + + +############################################################################### class Writer(object): def __init__(self, path, encoding): self.io=open(path, "wb") @@ -16,12 +60,15 @@ class Writer(object): def close(self): self.io.close() - +############################################################################### def createEmptyObject(scene, name): empty=bpy.data.objects.new(name, None) scene.objects.link(empty) return empty +def createMaterial(name): + material = bpy.data.materials.new(name) + return material def createMqoMaterial(m): material = bpy.data.materials.new(m.getName()) @@ -32,6 +79,19 @@ def createMqoMaterial(m): material.emit=1.0 return material +def createPmdMaterial(m): + material = bpy.data.materials.new("Material") + material.diffuse_shader='TOON' + material.specular_shader='TOON' + material.diffuse_color=([m.diffuse.r, m.diffuse.g, m.diffuse.b]) + material.alpha=m.diffuse.a + material.specular_hardness=int(m.shinness) + material.specular_color=([m.specular.r, m.specular.g, m.specular.b]) + material.mirror_color=([m.ambient.r, m.ambient.g, m.ambient.b]) + material.subsurface_scattering.enabled=True if m.flag==1 else False + # temporary + material.emit=1.0 + return material def createTexture(path): texture=bpy.data.textures.new(os.path.basename(path)) @@ -50,6 +110,10 @@ def materialAddTexture(material, texture): material.add_texture(texture, "UV", "COLOR") +def meshAddMaterial(mesh, material): + mesh.add_material(material) + + def createMesh(scene, name): mesh=bpy.data.meshes.new("Mesh") mesh_object= bpy.data.objects.new(name, mesh) @@ -175,6 +239,9 @@ def objectDuplicate(scene, obj): bpy.ops.object.location_apply() return dumy.data, dumy +def objectDelete(scene, obj): + scene.objects.unlink(obj) + def faceVertexCount(face): return len(face.verts) @@ -206,3 +273,214 @@ def materialToMqo(m): def faceMaterialIndex(face): return face.material_index +def objectGetData(o): + return o.data + +def objectAddArmatureModifier(o, armature_object): + mod=o.modifiers.new("Modifier", "ARMATURE") + mod.object = armature_object + mod.use_bone_envelopes=False + +def objectSelect(o): + o.selected=True + +def objectGetPose(o): + return o.pose + +def poseBoneLimit(n, b): + if n.endswith("_t"): + return + if n.startswith("knee_"): + b.ik_dof_y=False + b.ik_dof_z=False + b.ik_dof_x=True + b.ik_limit_x=True + b.ik_min_x=0 + b.ik_max_x=180 + elif n.startswith("ankle_"): + #b.ik_dof_y=False + pass + +def enterEditMode(): + bpy.ops.object.mode_set(mode='EDIT', toggle=False) + +def exitEditMode(): + bpy.ops.object.mode_set(mode='OBJECT', toggle=False) + +def objectDeselectAll(): + bpy.ops.object.select_all(action='DESELECT') + +def objectActivate(scene, o): + o.selected=True + scene.objects.active=o + +def meshAddVertexGroup(meshObject, name): + meshObject.add_vertex_group(name) + +def vertexSetNormal(mvert, normal): + mvert.normal=mathutils.Vector(normal) + +def meshUseVertexUv(mesh): + pass + +def vertexSetUv(mvert, uv): + pass + +def meshAssignVertexGroup(meshObject, name, index, weight): + meshObject.add_vertex_to_group(index, + meshObject.vertex_groups[name], weight, 'ADD') + +def meshCreateVerteicesAndFaces(mesh, vertices, faces): + vertexCount=int(len(vertices)/3) + faceCount=int(len(faces)/4) + mesh.add_geometry(vertexCount, 0, faceCount) + mesh.verts.foreach_set("co", vertices) + mesh.faces.foreach_set("verts_raw", faces) + assert(len(mesh.verts)==vertexCount) + +def meshAddUV(mesh): + mesh.add_uv_texture() + +def meshVertsDelete(mesh, remove_vertices): + enterEditMode() + bpy.ops.mesh.select_all(action='DESELECT') + exitEditMode() + + for i in remove_vertices: + mesh.verts[i].selected=True + + enterEditMode() + bpy.ops.mesh.delete(type='VERT') + exitEditMode() + +def createArmature(scene): + armature = bpy.data.armatures.new('Armature') + armature_object=bpy.data.objects.new('Armature', armature) + scene.objects.link(armature_object) + + armature_object.x_ray=True + armature.draw_names=True + armature.drawtype='OCTAHEDRAL' + armature.deform_envelope=False + armature.deform_vertexgroups=True + armature.x_axis_mirror=True + + return armature, armature_object + +def armatureMakeEditable(scene, armature_object): + # select only armature object and set edit mode + scene.objects.active=armature_object + bpy.ops.object.mode_set(mode='OBJECT', toggle=False) + bpy.ops.object.mode_set(mode='EDIT', toggle=False) + +def createIkConstraint(armature_object, p_bone, effector_name, ik): + constraint = p_bone.constraints.new('IK') + constraint.chain_length=len(ik.children) + constraint.target=armature_object + constraint.subtarget=effector_name + constraint.use_tail=False + # not used. place folder when export. + constraint.weight=ik.weight + constraint.iterations=ik.iterations * 10 + return constraint + +def createArmatureBone(armature, name): + return armature.edit_bones.new(name) + +def boneSetConnected(bone): + bone.connected=True + +def createVector(x, y, z): + return mathutils.Vector([x, y, z]) + +def armatureUpdate(armature): + pass + +def boneLayerMask(bone, layers): + layer=[] + for i in range(32): + try: + layer.append(True if layers[i]!=0 else False) + except IndexError: + layer.append(False) + bone.layer=layer + +def objectPinShape(o): + o.shape_key_lock=True + +def objectAddShapeKey(o, name): + return o.add_shape_key(name) + +def objectActivateShapeKey(o, index): + o.active_shape_key_index=index + +def shapeKeyAssign(shapeKey, index, pos): + shapeKey.data[index].co=pos + +def objectIsVisible(obj): + return obj.restrict_view + +def meshVertexGroupNames(meshObject): + for g in meshObject.vertex_groups: + yield g.name + +def faceNormal(face): + return face.normal + +def meshFaceUv(mesh, i, face): + return mesh.uv_textures[0].data[i].uv + +def armatureModifierGetObject(m): + return m.object + +def objectHasShapeKey(o): + return o.data.shape_keys + +def objectShapeKeys(o): + return o.data.shape_keys.keys + +def meshVertexGroup(meshObject, name): + indices=[] + for i, v in enumerate(meshObject.data.verts): + for g in v.groups: + if meshObject.vertex_groups[g.group].name==name: + indices.append(i) + return indices + +def materialGet(scene, material_name): + return bpy.data.materials[material_name] + +def modifierIsArmature(m): + return m.type=="ARMATURE" + +def boneHeadLocal(b): + return b.head_local[0:3] + +def boneTailLocal(b): + return b.tail_local[0:3] + +def boneIsConnected(b): + return b.connected + +def constraintIsIKSolver(c): + return c.type=='IK' + +def ikChainLen(c): + return c.chain_length + +def ikTarget(c): + return c.subtarget + +def ikItration(c): + return c.iterations + +def ikRotationWeight(c): + return c.weight + +def shapeKeyGet(b, index): + return b.data[index].co + +def shapeKeys(b): + for k in b.data: + yield k.co +