X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=swig%2Fblender%2Fmqo_import.py;h=630501ec3a5ca415b8fb4e727635b7f36be1c8f9;hb=e4436302445448dfac7da7b9b9666786611d33ab;hp=cb05bc8243cd64bd2b33d0e466fafcd71655a646;hpb=8624b41a70d35ec83be65b050a82e5c24bc06ad7;p=meshio%2Fmeshio.git diff --git a/swig/blender/mqo_import.py b/swig/blender/mqo_import.py index cb05bc8..630501e 100644 --- a/swig/blender/mqo_import.py +++ b/swig/blender/mqo_import.py @@ -21,10 +21,12 @@ This script imports a mqo into Blender for editing. 0.5 20100311: create armature from mikoto bone. 0.6 20100505: C extension. 0.7 20100606: integrate 2.4 and 2.5. +0.8 20100619: fix multibyte object name. +0.9 20100626: refactoring. ''' + import os import sys -import math # C extension from meshio import mqo @@ -32,182 +34,186 @@ from meshio import mqo def isBlender24(): return sys.version_info[0]<3 - if isBlender24(): # for 2.4 import Blender from Blender import Mathutils import bpy - # ファイルシステムの文字コード - # 改造版との共用のため - FS_ENCODING=sys.getfilesystemencoding() - if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"): - INTERNAL_ENCODING='utf-8' - else: - INTERNAL_ENCODING=FS_ENCODING + # wrapper + import bl24 as bl + + def createMqoMaterial(m): + material = Blender.Material.New( + m.getName().encode(bl.INTERNAL_ENCODING)) + #material.mode |= Blender.Material.Modes.SHADELESS + # diffuse + material.rgbCol = [m.color.r, m.color.g, m.color.b] + material.alpha = m.color.a + # other + material.amb=m.ambient + material.spec=m.specular + material.hard=int(255 * m.power) + material.emit=m.emmit + return material + else: # for 2.5 import bpy from bpy.props import * + # wrapper + import bl25 as bl + + def createMqoMaterial(m): + material = bpy.data.materials.new(m.getName()) + # shader + if m.shader==1: + material.diffuse_shader='FRESNEL' + else: + material.diffuse_shader='LAMBERT' + # diffuse + material.diffuse_color=[m.color.r, m.color.g, m.color.b] + material.diffuse_intensity=m.diffuse + material.alpha=m.color.a + # other + material.ambient = m.ambient + #material.specular = m.specular + material.emit=m.emit + return material + def has_mikoto(mqo): + #for o in mqo.objects: + # if o.getName().startswith('bone'): + # return True + # if o.getName().startswith('sdef'): + # return True + # if o.getName().startswith('anchor'): + # return True return False -def create_materials(scene, mqo, directory): +def __createMaterials(mqo, directory): """ create blender materials and renturn material list. """ materials = [] - images = [] - for m in mqo.materials: - material = Blender.Material.New(m.getName().encode(INTERNAL_ENCODING)) - materials.append(material) - - material.mode |= Blender.Material.Modes.SHADELESS - material.rgbCol = [m.color.r, m.color.g, m.color.b] - material.alpha = m.color.a - material.amb = m.ambient - material.spec = m.specular - material.hard = int(255 * m.power) - if m.texture!="": - texture_path=m.getTexture() - - # load texture image - if os.path.isabs(texture_path): - # absolute - path = texture_path - else: - # relative - path = os.path.join(directory, texture_path) - - # backslash to slash - #path = path.replace('\\', '/') - + textureMap={} + imageMap={} + if len(mqo.materials)>0: + for material_index, m in enumerate(mqo.materials): + # material + material=createMqoMaterial(m) + materials.append(material) # texture - if os.path.exists(path): - image = Blender.Image.Load(path.encode(INTERNAL_ENCODING)) - images.append(image) - material.mode = material.mode | Blender.Material.Modes.TEXFACE - tex = Blender.Texture.New(path.encode(INTERNAL_ENCODING)) - tex.type = Blender.Texture.Types.IMAGE - tex.image = image - material.setTexture(0, tex, Blender.Texture.TexCo.UV) - else: - print("%s not exits" % path) - - return materials + texture_name=m.getTexture() + if texture_name!='': + if texture_name in textureMap: + texture=textureMap[texture_name] + else: + # load texture image + if os.path.isabs(texture_name): + # absolute + path = texture_name + else: + # relative + path = os.path.join(directory, texture_name) + # texture + if os.path.exists(path): + print("create texture:", path) + texture, image=bl.texture.create(path) + textureMap[texture_name]=texture + imageMap[material_index]=image + else: + print("%s not exits" % path) + continue + bl.material.addTexture(material, texture) + else: + # default material + pass + return materials, imageMap -def create_objects(scene, root, mqo, materials): +def __createObjects(mqo, root, materials, imageMap, scale): """ create blender mesh objects. """ - # store hierarchy + # tree stack stack=[root] - objects=[] for o in mqo.objects: - #print "%s:v(%d),f(%d)" % (o.name, len(o.vertices), len(o.faces)) - # create mesh - mesh = Blender.Mesh.New() - mesh_object=scene.objects.new(mesh, o.name.encode('utf-8')) + mesh, mesh_object=bl.mesh.create(o.getName()) # add hierarchy stack_depth=len(stack)-1 - print(o.depth, stack_depth) + #print(o.depth, stack_depth) if o.depth=16: + # split a mesh ? print("over 16 materials!") break - mesh.materials+=[materials[material_index]] - usedMaterials[material_index]=i - + bl.mesh.addMaterial(mesh, materials[material_index]) + materialMap[material_index]=i + # set face params - for i, f in enumerate(o.faces): - if not type(new_faces[i]) is int: - continue - - face=mesh.faces[new_faces[i]] - + assert(len(o.faces)==len(mesh.faces)) + bl.mesh.addUV(mesh) + for i, (f, face) in enumerate(zip(o.faces, mesh.faces)): uv_array=[] - for i in xrange(f.index_count): - uv_array.append(Blender.Mathutils.Vector( - f.getUV(i).x, - 1.0-f.getUV(i).y) - ) - try: - face.uv=uv_array - except Exception, msg: - #print msg - #print face.index, uv_array - pass - - if f.material_index in usedMaterials: - face.mat = usedMaterials[f.material_index] - - face.smooth = 1 - - # rmeove dummy 0 vertex - mesh.verts.delete(0) - - mesh.mode |= Blender.Mesh.Modes.AUTOSMOOTH - mesh.maxSmoothAngle = int(o.smoothing) - mesh.smooth() - mesh.calcNormals() - mesh.flipNormals() - mesh.update() + # ToDo FIX + # flip face + for j in reversed(range(f.index_count)): + uv_array.append((f.getUV(j).x, 1.0-f.getUV(j).y)) + bl.mesh.setFaceUV(mesh, i, face, uv_array, + imageMap.get(f.material_index, None)) + if f.material_index in materialMap: + bl.face.setMaterial(face, materialMap[f.material_index]) + bl.face.setSmooth(face, True) # mirror modifier if o.mirror: - mod=mesh_object.modifiers.append(Blender.Modifier.Types.MIRROR) + bl.modifier.addMirror(mesh_object) + + # set smoothing + bl.mesh.setSmooth(mesh, o.smoothing) + + # calc normal + bl.mesh.recalcNormals(mesh_object) return objects +############################################################################### +# for mqo mikoto bone. +############################################################################### class MikotoBone(object): __slots__=[ 'name', @@ -226,9 +232,9 @@ class MikotoBone(object): self.name=materials[face.material_index].name.encode('utf-8') - i0=face.indices[0] - i1=face.indices[1] - i2=face.indices[2] + i0=face.getIndex(0) + i1=face.getIndex(1) + i2=face.getIndex(2) v0=vertices[i0] v1=vertices[i1] v2=vertices[i2] @@ -319,7 +325,7 @@ def build_armature(armature, mikotoBone, parent=None): build_armature(armature, child, bone) -def create_armature(scene, mqo): +def create_armature(mqo): """ create armature """ @@ -409,8 +415,8 @@ def create_armature(scene, mqo): class TrianglePlane(object): """ - mikoto方式ボーンのアンカーウェイト計算用。 - (不完全) + mikoto$BJ}<0%\!<%s$N%"%s%+!<%&%'%$%H7W;;MQ!#(B + ($BIT40A4(B) """ __slots__=['normal', 'v0', 'v1', 'v2', @@ -468,7 +474,7 @@ class TrianglePlane(object): class MikotoAnchor(object): """ - mikoto方式スケルトンのアンカー。 + mikoto$BJ}<0%9%1%k%H%s$N%"%s%+!