4 Name: 'Metasequoia(.mqo)...'
\r
7 Tooltip: 'Import from Metasequoia file format (.mqo)'
\r
9 __author__=['ousttrue']
\r
10 __url__ = ["http://gunload.web.fc2.com/blender/"]
\r
11 __version__= '0.6 2010/05/05'
\r
16 This script imports a mqo into Blender for editing.
\r
18 0.2 20080123: update.
\r
19 0.3 20091125: modify for linux.
\r
20 0.4 20100310: rewrite.
\r
21 0.5 20100311: create armature from mikoto bone.
\r
22 0.6 20100505: C extension.
\r
23 0.7 20100606: integrate 2.4 and 2.5.
\r
25 ###############################################################################
\r
27 ###############################################################################
\r
33 from meshio import mqo
\r
36 return sys.version_info[0]<3
\r
41 from Blender import Mathutils
\r
46 FS_ENCODING=sys.getfilesystemencoding()
\r
47 if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"):
\r
48 INTERNAL_ENCODING='utf-8'
\r
50 INTERNAL_ENCODING=FS_ENCODING
\r
54 from bpy.props import *
\r
57 ###############################################################################
\r
59 ###############################################################################
\r
61 def has_mikoto(mqo):
\r
65 def create_materials(scene, mqo, directory):
\r
67 create blender materials and renturn material list.
\r
71 for m in mqo.materials:
\r
72 material = Blender.Material.New(m.getName().encode(INTERNAL_ENCODING))
\r
73 materials.append(material)
\r
75 material.mode |= Blender.Material.Modes.SHADELESS
\r
76 material.rgbCol = [m.color.r, m.color.g, m.color.b]
\r
77 material.alpha = m.color.a
\r
78 material.amb = m.ambient
\r
79 material.spec = m.specular
\r
80 material.hard = int(255 * m.power)
\r
82 texture_path=m.getTexture()
\r
84 # load texture image
\r
85 if os.path.isabs(texture_path):
\r
90 path = os.path.join(directory, texture_path)
\r
92 # backslash to slash
\r
93 #path = path.replace('\\', '/')
\r
96 if os.path.exists(path):
\r
97 image = Blender.Image.Load(path.encode(INTERNAL_ENCODING))
\r
98 images.append(image)
\r
99 material.mode = material.mode | Blender.Material.Modes.TEXFACE
\r
100 tex = Blender.Texture.New(path.encode(INTERNAL_ENCODING))
\r
101 tex.type = Blender.Texture.Types.IMAGE
\r
103 material.setTexture(0, tex, Blender.Texture.TexCo.UV)
\r
105 print("%s not exits" % path)
\r
110 def create_objects(scene, root, mqo, materials):
\r
112 create blender mesh objects.
\r
118 for o in mqo.objects:
\r
119 #print "%s:v(%d),f(%d)" % (o.name, len(o.vertices), len(o.faces))
\r
121 mesh = Blender.Mesh.New()
\r
122 mesh_object=scene.objects.new(mesh, o.name.encode('utf-8'))
\r
125 stack_depth=len(stack)-1
\r
126 print(o.depth, stack_depth)
\r
127 if o.depth<stack_depth:
\r
128 for i in range(stack_depth-o.depth):
\r
130 stack[-1].makeParent([mesh_object])
\r
131 stack.append(mesh_object)
\r
133 if o.name.startswith('sdef'):
\r
135 objects.append(mesh_object)
\r
136 elif o.name.startswith('anchor'):
\r
137 #print("hide %s" % o.name)
\r
138 #mesh_object.restrictDisplay=False
\r
139 mesh_object.layers=[2]
\r
140 elif o.name.startswith('bone'):
\r
141 mesh_object.layers=[2]
\r
144 mesh.verts.extend(Mathutils.Vector(0, 0, 0)) # dummy
\r
145 mesh.verts.extend([(v.x, -v.z, v.y) for v in o.vertices])
\r
148 for face in o.faces:
\r
150 for i in xrange(face.index_count):
\r
151 face_indices.append(face.getIndex(i)+1)
\r
152 mesh_faces.append(face_indices)
\r
153 #new_faces=mesh.faces.extend([face.indices for face in o.faces],
\r
154 new_faces=mesh.faces.extend(mesh_faces,
\r
159 # gather used materials
\r
162 for i in new_faces:
\r
164 usedMaterials[o.faces[i].material_index]=True
\r
166 # blender limits 16 materials per mesh
\r
168 for i, material_index in enumerate(usedMaterials.keys()):
\r
170 print("over 16 materials!")
\r
172 mesh.materials+=[materials[material_index]]
\r
173 usedMaterials[material_index]=i
\r
176 for i, f in enumerate(o.faces):
\r
177 if not type(new_faces[i]) is int:
\r
180 face=mesh.faces[new_faces[i]]
\r
183 for i in xrange(f.index_count):
\r
184 uv_array.append(Blender.Mathutils.Vector(
\r
190 except Exception as msg:
\r
192 #print face.index, uv_array
\r
195 if f.material_index in usedMaterials:
\r
196 face.mat = usedMaterials[f.material_index]
\r
200 # rmeove dummy 0 vertex
\r
201 mesh.verts.delete(0)
\r
203 mesh.mode |= Blender.Mesh.Modes.AUTOSMOOTH
\r
204 mesh.maxSmoothAngle = int(o.smoothing)
\r
212 mod=mesh_object.modifiers.append(Blender.Modifier.Types.MIRROR)
\r
217 class MikotoBone(object):
\r
220 'iHead', 'iTail', 'iUp',
\r
221 'vHead', 'vTail', 'vUp',
\r
222 'parent', 'isFloating',
\r
225 def __init__(self, face=None, vertices=None, materials=None):
\r
227 self.isFloating=False
\r
233 self.name=materials[face.material_index].name.encode('utf-8')
\r
244 sqNorm0=e01.getSqNorm()
\r
245 sqNorm1=e12.getSqNorm()
\r
246 sqNorm2=e20.getSqNorm()
\r
247 if sqNorm0>sqNorm1:
\r
248 if sqNorm1>sqNorm2:
\r
254 if sqNorm0>sqNorm2:
\r
266 if sqNorm1<sqNorm2:
\r
272 if sqNorm0<sqNorm2:
\r
282 self.vHead=vertices[self.iHead]
\r
283 self.vTail=vertices[self.iTail]
\r
284 self.vUp=vertices[self.iUp]
\r
286 if self.name.endswith('[]'):
\r
287 basename=self.name[0:-2]
\r
290 self.name="%s_L" % basename
\r
292 self.name="%s_R" % basename
\r
295 def setParent(self, parent, floating=False):
\r
297 self.isFloating=True
\r
299 parent.children.append(self)
\r
301 def printTree(self, indent=''):
\r
302 print("%s%s" % (indent, self.name))
\r
303 for child in self.children:
\r
304 child.printTree(indent+' ')
\r
307 def build_armature(armature, mikotoBone, parent=None):
\r
309 create a armature bone.
\r
311 bone = Armature.Editbone()
\r
312 bone.name = mikotoBone.name.encode('utf-8')
\r
313 armature.bones[bone.name] = bone
\r
315 bone.head = Mathutils.Vector(*mikotoBone.vHead.to_a())
\r
316 bone.tail = Mathutils.Vector(*mikotoBone.vTail.to_a())
\r
319 if mikotoBone.isFloating:
\r
322 bone.options=[Armature.CONNECTED]
\r
324 for child in mikotoBone.children:
\r
325 build_armature(armature, child, bone)
\r
328 def create_armature(scene, mqo):
\r
333 for o in mqo.objects:
\r
334 if o.name.startswith('bone'):
\r
341 for f in boneObject.faces:
\r
342 if f.index_count!=3:
\r
343 print("invalid index_count: %d" % f.index_count)
\r
345 b=MikotoBone(f, boneObject.vertices, mqo.materials)
\r
348 ####################
\r
349 # build mikoto bone tree
\r
350 ####################
\r
351 mikotoRoot=MikotoBone()
\r
353 for b in tailMap.values():
\r
354 # each bone has unique parent or is root bone.
\r
355 if b.iHead in tailMap:
\r
356 b.setParent(tailMap[b.iHead])
\r
359 for e in boneObject.edges:
\r
360 if b.iHead==e.indices[0]:
\r
362 if e.indices[1] in tailMap:
\r
363 b.setParent(tailMap[e.indices[1]], True)
\r
366 elif b.iHead==e.indices[1]:
\r
368 if e.indices[0] in tailMap:
\r
369 b.setParent(tailMap[e.indices[0]], True)
\r
376 b.setParent(mikotoRoot, True)
\r
378 if len(mikotoRoot.children)==0:
\r
379 print("no root bone")
\r
382 if len(mikotoRoot.children)==1:
\r
384 mikotoRoot=mikotoRoot.children[0]
\r
385 mikotoRoot.parent=None
\r
387 mikotoRoot.vHead=Vector3(0, 10, 0)
\r
388 mikotoRoot.vTail=Vector3(0, 0, 0)
\r
390 ####################
\r
392 ####################
\r
393 armature = Armature.New()
\r
395 armature_object = scene.objects.new(armature)
\r
397 act = Armature.NLA.NewAction()
\r
398 act.setActive(armature_object)
\r
400 armature_object.drawMode |= Object.DrawModes.XRAY
\r
401 # armature settings
\r
402 armature.drawType = Armature.OCTAHEDRON
\r
403 armature.envelopes = False
\r
404 armature.vertexGroups = True
\r
405 armature.mirrorEdit = True
\r
406 armature.drawNames=True
\r
409 armature.makeEditable()
\r
410 build_armature(armature, mikotoRoot)
\r
413 return armature_object
\r
416 class TrianglePlane(object):
\r
418 mikoto方式ボーンのアンカーウェイト計算用。
\r
421 __slots__=['normal',
\r
424 def __init__(self, v0, v1, v2):
\r
429 def isInsideXY(self, p):
\r
430 v0=Vector2(self.v0.x, self.v0.y)
\r
431 v1=Vector2(self.v1.x, self.v1.y)
\r
432 v2=Vector2(self.v2.x, self.v2.y)
\r
436 c0=Vector2.cross(e01, p-v0)
\r
437 c1=Vector2.cross(e12, p-v1)
\r
438 c2=Vector2.cross(e20, p-v2)
\r
439 if c0>=0 and c1>=0 and c2>=0:
\r
441 if c0<=0 and c1<=0 and c2<=0:
\r
444 def isInsideYZ(self, p):
\r
445 v0=Vector2(self.v0.y, self.v0.z)
\r
446 v1=Vector2(self.v1.y, self.v1.z)
\r
447 v2=Vector2(self.v2.y, self.v2.z)
\r
451 c0=Vector2.cross(e01, p-v0)
\r
452 c1=Vector2.cross(e12, p-v1)
\r
453 c2=Vector2.cross(e20, p-v2)
\r
454 if c0>=0 and c1>=0 and c2>=0:
\r
456 if c0<=0 and c1<=0 and c2<=0:
\r
459 def isInsideZX(self, p):
\r
460 v0=Vector2(self.v0.z, self.v0.x)
\r
461 v1=Vector2(self.v1.z, self.v1.x)
\r
462 v2=Vector2(self.v2.z, self.v2.x)
\r
466 c0=Vector2.cross(e01, p-v0)
\r
467 c1=Vector2.cross(e12, p-v1)
\r
468 c2=Vector2.cross(e20, p-v2)
\r
469 if c0>=0 and c1>=0 and c2>=0:
\r
471 if c0<=0 and c1<=0 and c2<=0:
\r
475 class MikotoAnchor(object):
\r
477 mikoto方式スケルトンのアンカー。
\r
480 "triangles", "bbox",
\r
482 def __init__(self):
\r
486 def push(self, face, vertices):
\r
487 if face.index_count==3:
\r
488 self.triangles.append(TrianglePlane(
\r
489 vertices[face.indices[0]],
\r
490 vertices[face.indices[1]],
\r
491 vertices[face.indices[2]]
\r
493 elif face.index_count==4:
\r
494 self.triangles.append(TrianglePlane(
\r
495 vertices[face.indices[0]],
\r
496 vertices[face.indices[1]],
\r
497 vertices[face.indices[2]]
\r
499 self.triangles.append(TrianglePlane(
\r
500 vertices[face.indices[2]],
\r
501 vertices[face.indices[3]],
\r
502 vertices[face.indices[0]]
\r
506 self.bbox=BoundingBox(vertices[face.indices[0]])
\r
507 for i in face.indices:
\r
508 self.bbox.expand(vertices[i])
\r
511 def calcWeight(self, v):
\r
512 if not self.bbox.isInside(v):
\r
515 if self.anyXY(v.x, v.y) and self.anyYZ(v.y, v.z) and self.anyZX(v.z, v.x):
\r
520 def anyXY(self, x, y):
\r
521 for t in self.triangles:
\r
522 if t.isInsideXY(Vector2(x, y)):
\r
526 def anyYZ(self, y, z):
\r
527 for t in self.triangles:
\r
528 if t.isInsideYZ(Vector2(y, z)):
\r
532 def anyZX(self, z, x):
\r
533 for t in self.triangles:
\r
534 if t.isInsideZX(Vector2(z, x)):
\r
539 def create_bone_weight(scene, mqo, armature_object, objects):
\r
541 create mikoto bone weight.
\r
544 # setup mikoto anchors
\r
545 for o in mqo.objects:
\r
546 if o.name.startswith("anchor"):
\r
548 name=mqo.materials[f.material_index].name
\r
549 if name.endswith('[]'):
\r
550 basename=name[0:-2]
\r
551 v=o.vertices[f.indices[0]]
\r
554 name_L=basename+'_L'
\r
555 if not name_L in anchorMap:
\r
556 anchorMap[name_L]=MikotoAnchor()
\r
557 anchorMap[name_L].push(f, o.vertices)
\r
560 name_R=basename+'_R'
\r
561 if not name_R in anchorMap:
\r
562 anchorMap[name_R]=MikotoAnchor()
\r
563 anchorMap[name_R].push(f, o.vertices)
\r
565 print("no side", v)
\r
567 if not name in anchorMap:
\r
568 anchorMap[name]=MikotoAnchor()
\r
569 anchorMap[name].push(f, o.vertices)
\r
572 # add armature modifier
\r
573 mod=o.modifiers.append(Modifier.Types.ARMATURE)
\r
574 mod[Modifier.Settings.OBJECT] = armature_object
\r
575 mod[Modifier.Settings.ENVELOPES] = False
\r
576 o.makeDisplayList()
\r
577 # create vertex group
\r
578 mesh=o.getData(mesh=True)
\r
579 for name in anchorMap.keys():
\r
580 mesh.addVertGroup(name)
\r
583 # assing vertices to vertex group
\r
585 mesh=o.getData(mesh=True)
\r
586 for i, mvert in enumerate(mesh.verts):
\r
588 for name, anchor in anchorMap.items():
\r
589 weight=anchor.calcWeight(mvert.co)
\r
591 mesh.assignVertsToGroup(
\r
592 name, [i], weight, Mesh.AssignModes.ADD)
\r
595 # debug orphan vertex
\r
596 print('orphan', mvert)
\r
600 def execute_24(filename):
\r
604 filename=filename.decode(INTERNAL_ENCODING)
\r
605 print("##start mqo_import.py##")
\r
606 print(INTERNAL_ENCODING, FS_ENCODING)
\r
607 print("parse mqo file: %s" % (filename))
\r
609 Blender.Window.WaitCursor(1)
\r
610 t = Blender.sys.time()
\r
615 if not io.read(filename):
\r
619 scene = Blender.Scene.GetCurrent()
\r
622 materials=create_materials(scene, io, os.path.dirname(filename))
\r
625 root=scene.objects.new("Empty")
\r
626 root.setName(os.path.basename(filename))
\r
627 objects=create_objects(scene, root, io, materials)
\r
630 # create mikoto bone
\r
631 armature_object=create_armature(scene, io)
\r
632 if armature_object:
\r
633 root.makeParent([armature_object])
\r
635 # create bone weight
\r
636 create_bone_weight(scene, io, armature_object, objects)
\r
639 print('finished in %.2f seconds' % (Blender.sys.time()-t))
\r
642 Blender.Window.WaitCursor(0)
\r
646 def create_texture(directory, texture_name):
\r
647 texture=bpy.data.textures.new(texture_name)
\r
648 texture.type='IMAGE'
\r
649 texture=texture.recast_type()
\r
650 #texturePath="%s/%s" % (directory, texture_name)
\r
651 texturePath=os.path.join(directory, texture_name)
\r
652 print('create_texture', texturePath)
\r
653 image=bpy.data.images.load(texturePath)
\r
654 texture.image=image
\r
655 texture.mipmap = True
\r
656 texture.interpolation = True
\r
657 texture.use_alpha = True
\r
660 def create_materials(mqo, scene, directory):
\r
664 if len(mqo.materials)>0:
\r
665 for material_index, m in enumerate(mqo.materials):
\r
666 material = bpy.data.materials.new(m.getName())
\r
667 materials.append(material)
\r
669 material.diffuse_color=[m.color.r, m.color.g, m.color.b]
\r
670 material.alpha=m.color.a
\r
671 material.diffuse_intensity=m.diffuse
\r
672 texture_name=m.getTexture()
\r
673 if texture_name!='':
\r
674 if texture_name in textureMap:
\r
675 texture=textureMap[texture_name]
\r
677 texture=create_texture(directory, texture_name)
\r
678 textureMap[texture_name]=texture
\r
679 imageMap[material_index]=texture.image
\r
680 #material.add_texture(texture, "UV", {"COLOR", "ALPHA"})
\r
681 material.add_texture(texture, "UV", "COLOR")
\r
685 material = bpy.data.materials.new('Default')
\r
686 materials.append(material)
\r
687 return materials, imageMap
\r
689 def create_objects(mqo, scene, parent, materials, imageMap, scale):
\r
690 for o in mqo.objects:
\r
693 mesh=bpy.data.meshes.new("Mesh")
\r
694 meshObject= bpy.data.objects.new(o.getName(), mesh)
\r
695 scene.objects.link(meshObject)
\r
696 meshObject.parent=parent
\r
698 # count triangle and quadrangle
\r
701 if f.index_count==3 or f.index_count==4:
\r
703 mesh.add_geometry(len(o.vertices), 0, faceCount)
\r
706 unpackedVertices=[]
\r
707 for v in o.vertices:
\r
708 # convert right-handed y-up to right-handed z-up
\r
709 unpackedVertices.extend(
\r
710 (scale*v.x, scale*-v.z, scale*v.y))
\r
711 mesh.verts.foreach_set("co", unpackedVertices)
\r
719 for i in range(f.index_count):
\r
720 face.append(f.getIndex(i))
\r
725 if len(face) != 3 and len(face) != 4:
\r
726 print("{0} vertices in face.".format(len(face)))
\r
731 # rotate indices if the 4th is 0
\r
732 face = [face[3], face[0], face[1], face[2]]
\r
733 elif len(face) == 3:
\r
735 # rotate indices if the 3rd is 0
\r
736 face = [face[2], face[0], face[1], 0]
\r
740 unpackedFaces.extend(face)
\r
741 usedMaterial.add(f.material_index)
\r
743 mesh.faces.foreach_set("verts_raw", unpackedFaces)
\r
745 #print([getFace(f) for f in o.faces])
\r
746 print("fail to mesh.faces.foreach_set")
\r
752 for i in usedMaterial:
\r
753 mesh.add_material(materials[i])
\r
754 meshMaterialMap[i]=materialIndex
\r
758 mesh.add_uv_texture()
\r
759 for mqo_face, blender_face, uv_face in zip(
\r
760 o.faces, mesh.faces, mesh.uv_textures[0].data):
\r
761 if mqo_face.index_count<3:
\r
763 blender_face.material_index=meshMaterialMap[mqo_face.material_index]
\r
764 if mqo_face.index_count>=3:
\r
765 uv_face.uv1=[mqo_face.getUV(0).x, 1.0-mqo_face.getUV(0).y]
\r
766 uv_face.uv2=[mqo_face.getUV(1).x, 1.0-mqo_face.getUV(1).y]
\r
767 uv_face.uv3=[mqo_face.getUV(2).x, 1.0-mqo_face.getUV(2).y]
\r
768 if mqo_face.index_count==4:
\r
770 mqo_face.getUV(3).x, 1.0-mqo_face.getUV(3).y]
\r
771 if materials[mqo_face.material_index] in imageMap:
\r
772 uv_face.image=imageMap[mqo_face.material_index]
\r
777 def load(filename, context, scale):
\r
779 load mqo file to context.
\r
782 if not io.read(filename):
\r
783 print("fail to load",filename)
\r
786 scene=context.scene
\r
789 materials, imageMap=create_materials(
\r
790 io, scene, os.path.dirname(filename))
\r
793 empty=bpy.data.objects.new(os.path.basename(filename), None)
\r
794 scene.objects.link(empty)
\r
797 create_objects(io, scene, empty, materials, imageMap, scale)
\r
800 ###############################################################################
\r
802 ###############################################################################
\r
806 Blender.Window.FileSelector(execute_24, 'Import MQO', '*.mqo')
\r
810 class IMPORT_OT_mqo(bpy.types.Operator):
\r
811 '''Import from Metasequoia file format (.mqo)'''
\r
812 bl_idname = "import_scene.mqo"
\r
813 bl_label = 'Import MQO'
\r
815 # List of operator properties, the attributes will be assigned
\r
816 # to the class instance from the operator settings before calling.
\r
818 path = StringProperty(
\r
820 description="File path used for importing the MQO file",
\r
821 maxlen= 1024, default= "")
\r
822 filename = StringProperty(
\r
824 description="Name of the file.")
\r
825 directory = StringProperty(
\r
827 description="Directory of the file.")
\r
829 scale = FloatProperty(
\r
831 description="Scale the MQO by this value",
\r
832 min=0.0001, max=1000000.0,
\r
833 soft_min=0.001, soft_max=100.0, default=1.0)
\r
835 def execute(self, context):
\r
836 load(self.properties.path, context, self.properties.scale)
\r
839 def invoke(self, context, event):
\r
841 wm.add_fileselect(self)
\r
842 return 'RUNNING_MODAL'
\r
846 def menu_func(self, context):
\r
847 self.layout.operator(IMPORT_OT_mqo.bl_idname,
\r
848 text="Metasequoia (.mqo)")
\r
851 bpy.types.register(IMPORT_OT_mqo)
\r
852 bpy.types.INFO_MT_file_import.append(menu_func)
\r
855 bpy.types.unregister(IMPORT_OT_mqo)
\r
856 bpy.types.INFO_MT_file_import.remove(menu_func)
\r
858 if __name__=="__main__":
\r