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
24 0.8 20100619: fix multibyte object name.
\r
28 ###############################################################################
\r
30 ###############################################################################
\r
35 from meshio import mqo
\r
38 return sys.version_info[0]<3
\r
43 from Blender import Mathutils
\r
51 from bpy.props import *
\r
57 def has_mikoto(mqo):
\r
58 #for o in mqo.objects:
\r
59 # if o.getName().startswith('bone'):
\r
61 # if o.getName().startswith('sdef'):
\r
63 # if o.getName().startswith('anchor'):
\r
68 def __createMaterials(scene, mqo, directory):
\r
70 create blender materials and renturn material list.
\r
75 if len(mqo.materials)>0:
\r
76 for material_index, m in enumerate(mqo.materials):
\r
78 material=bl.createMqoMaterial(m)
\r
79 materials.append(material)
\r
81 texture_name=m.getTexture()
\r
82 if texture_name!='':
\r
83 if texture_name in textureMap:
\r
84 texture=textureMap[texture_name]
\r
86 # load texture image
\r
87 if os.path.isabs(texture_name):
\r
92 path = os.path.join(directory, texture_name)
\r
94 if os.path.exists(path):
\r
95 print("create texture:", path)
\r
96 texture, image=bl.createTexture(path)
\r
97 textureMap[texture_name]=texture
\r
98 imageMap[material_index]=image
\r
100 print("%s not exits" % path)
\r
102 bl.materialAddTexture(material, texture)
\r
106 return materials, imageMap
\r
109 def __createObjects(scene, mqo, root, materials, imageMap, scale):
\r
111 create blender mesh objects.
\r
116 for o in mqo.objects:
\r
117 mesh, mesh_object=bl.createMesh(scene, o.getName())
\r
120 stack_depth=len(stack)-1
\r
121 print(o.depth, stack_depth)
\r
122 if o.depth<stack_depth:
\r
123 for i in range(stack_depth-o.depth):
\r
125 bl.objectMakeParent(stack[-1], mesh_object)
\r
126 stack.append(mesh_object)
\r
128 if o.getName().startswith('sdef'):
\r
129 objects.append(mesh_object)
\r
130 elif o.getName().startswith('anchor'):
\r
131 bl.objectLayerMask(mesh_object, [0, 1])
\r
132 elif o.getName().startswith('bone'):
\r
133 bl.objectLayerMask(mesh_object, [0, 1])
\r
135 bl.meshAddMqoGeometry(mesh_object, o, materials, imageMap, scale)
\r
140 ###############################################################################
\r
141 # for mqo mikoto bone.
\r
142 ###############################################################################
\r
143 class MikotoBone(object):
\r
146 'iHead', 'iTail', 'iUp',
\r
147 'vHead', 'vTail', 'vUp',
\r
148 'parent', 'isFloating',
\r
151 def __init__(self, face=None, vertices=None, materials=None):
\r
153 self.isFloating=False
\r
159 self.name=materials[face.material_index].name.encode('utf-8')
\r
161 i0=face.getIndex(0)
\r
162 i1=face.getIndex(1)
\r
163 i2=face.getIndex(2)
\r
170 sqNorm0=e01.getSqNorm()
\r
171 sqNorm1=e12.getSqNorm()
\r
172 sqNorm2=e20.getSqNorm()
\r
173 if sqNorm0>sqNorm1:
\r
174 if sqNorm1>sqNorm2:
\r
180 if sqNorm0>sqNorm2:
\r
192 if sqNorm1<sqNorm2:
\r
198 if sqNorm0<sqNorm2:
\r
208 self.vHead=vertices[self.iHead]
\r
209 self.vTail=vertices[self.iTail]
\r
210 self.vUp=vertices[self.iUp]
\r
212 if self.name.endswith('[]'):
\r
213 basename=self.name[0:-2]
\r
216 self.name="%s_L" % basename
\r
218 self.name="%s_R" % basename
\r
221 def setParent(self, parent, floating=False):
\r
223 self.isFloating=True
\r
225 parent.children.append(self)
\r
227 def printTree(self, indent=''):
\r
228 print("%s%s" % (indent, self.name))
\r
229 for child in self.children:
\r
230 child.printTree(indent+' ')
\r
233 def build_armature(armature, mikotoBone, parent=None):
\r
235 create a armature bone.
\r
237 bone = Armature.Editbone()
\r
238 bone.name = mikotoBone.name.encode('utf-8')
\r
239 armature.bones[bone.name] = bone
\r
241 bone.head = Mathutils.Vector(*mikotoBone.vHead.to_a())
\r
242 bone.tail = Mathutils.Vector(*mikotoBone.vTail.to_a())
\r
245 if mikotoBone.isFloating:
\r
248 bone.options=[Armature.CONNECTED]
\r
250 for child in mikotoBone.children:
\r
251 build_armature(armature, child, bone)
\r
254 def create_armature(scene, mqo):
\r
259 for o in mqo.objects:
\r
260 if o.name.startswith('bone'):
\r
267 for f in boneObject.faces:
\r
268 if f.index_count!=3:
\r
269 print("invalid index_count: %d" % f.index_count)
\r
271 b=MikotoBone(f, boneObject.vertices, mqo.materials)
\r
274 ####################
\r
275 # build mikoto bone tree
\r
276 ####################
\r
277 mikotoRoot=MikotoBone()
\r
279 for b in tailMap.values():
\r
280 # each bone has unique parent or is root bone.
\r
281 if b.iHead in tailMap:
\r
282 b.setParent(tailMap[b.iHead])
\r
285 for e in boneObject.edges:
\r
286 if b.iHead==e.indices[0]:
\r
288 if e.indices[1] in tailMap:
\r
289 b.setParent(tailMap[e.indices[1]], True)
\r
292 elif b.iHead==e.indices[1]:
\r
294 if e.indices[0] in tailMap:
\r
295 b.setParent(tailMap[e.indices[0]], True)
\r
302 b.setParent(mikotoRoot, True)
\r
304 if len(mikotoRoot.children)==0:
\r
305 print("no root bone")
\r
308 if len(mikotoRoot.children)==1:
\r
310 mikotoRoot=mikotoRoot.children[0]
\r
311 mikotoRoot.parent=None
\r
313 mikotoRoot.vHead=Vector3(0, 10, 0)
\r
314 mikotoRoot.vTail=Vector3(0, 0, 0)
\r
316 ####################
\r
318 ####################
\r
319 armature = Armature.New()
\r
321 armature_object = scene.objects.new(armature)
\r
323 act = Armature.NLA.NewAction()
\r
324 act.setActive(armature_object)
\r
326 armature_object.drawMode |= Object.DrawModes.XRAY
\r
327 # armature settings
\r
328 armature.drawType = Armature.OCTAHEDRON
\r
329 armature.envelopes = False
\r
330 armature.vertexGroups = True
\r
331 armature.mirrorEdit = True
\r
332 armature.drawNames=True
\r
335 armature.makeEditable()
\r
336 build_armature(armature, mikotoRoot)
\r
339 return armature_object
\r
342 class TrianglePlane(object):
\r
344 mikoto
\e$BJ}<0%\!<%s$N%"%s%+!<%&%'%$%H7W;;MQ!#
\e(B
\r
347 __slots__=['normal',
\r
350 def __init__(self, v0, v1, v2):
\r
355 def isInsideXY(self, p):
\r
356 v0=Vector2(self.v0.x, self.v0.y)
\r
357 v1=Vector2(self.v1.x, self.v1.y)
\r
358 v2=Vector2(self.v2.x, self.v2.y)
\r
362 c0=Vector2.cross(e01, p-v0)
\r
363 c1=Vector2.cross(e12, p-v1)
\r
364 c2=Vector2.cross(e20, p-v2)
\r
365 if c0>=0 and c1>=0 and c2>=0:
\r
367 if c0<=0 and c1<=0 and c2<=0:
\r
370 def isInsideYZ(self, p):
\r
371 v0=Vector2(self.v0.y, self.v0.z)
\r
372 v1=Vector2(self.v1.y, self.v1.z)
\r
373 v2=Vector2(self.v2.y, self.v2.z)
\r
377 c0=Vector2.cross(e01, p-v0)
\r
378 c1=Vector2.cross(e12, p-v1)
\r
379 c2=Vector2.cross(e20, p-v2)
\r
380 if c0>=0 and c1>=0 and c2>=0:
\r
382 if c0<=0 and c1<=0 and c2<=0:
\r
385 def isInsideZX(self, p):
\r
386 v0=Vector2(self.v0.z, self.v0.x)
\r
387 v1=Vector2(self.v1.z, self.v1.x)
\r
388 v2=Vector2(self.v2.z, self.v2.x)
\r
392 c0=Vector2.cross(e01, p-v0)
\r
393 c1=Vector2.cross(e12, p-v1)
\r
394 c2=Vector2.cross(e20, p-v2)
\r
395 if c0>=0 and c1>=0 and c2>=0:
\r
397 if c0<=0 and c1<=0 and c2<=0:
\r
401 class MikotoAnchor(object):
\r
403 mikoto
\e$BJ}<0%9%1%k%H%s$N%"%s%+!<!#
\e(B
\r
406 "triangles", "bbox",
\r
408 def __init__(self):
\r
412 def push(self, face, vertices):
\r
413 if face.index_count==3:
\r
414 self.triangles.append(TrianglePlane(
\r
415 vertices[face.indices[0]],
\r
416 vertices[face.indices[1]],
\r
417 vertices[face.indices[2]]
\r
419 elif face.index_count==4:
\r
420 self.triangles.append(TrianglePlane(
\r
421 vertices[face.indices[0]],
\r
422 vertices[face.indices[1]],
\r
423 vertices[face.indices[2]]
\r
425 self.triangles.append(TrianglePlane(
\r
426 vertices[face.indices[2]],
\r
427 vertices[face.indices[3]],
\r
428 vertices[face.indices[0]]
\r
432 self.bbox=BoundingBox(vertices[face.indices[0]])
\r
433 for i in face.indices:
\r
434 self.bbox.expand(vertices[i])
\r
437 def calcWeight(self, v):
\r
438 if not self.bbox.isInside(v):
\r
441 if self.anyXY(v.x, v.y) and self.anyYZ(v.y, v.z) and self.anyZX(v.z, v.x):
\r
446 def anyXY(self, x, y):
\r
447 for t in self.triangles:
\r
448 if t.isInsideXY(Vector2(x, y)):
\r
452 def anyYZ(self, y, z):
\r
453 for t in self.triangles:
\r
454 if t.isInsideYZ(Vector2(y, z)):
\r
458 def anyZX(self, z, x):
\r
459 for t in self.triangles:
\r
460 if t.isInsideZX(Vector2(z, x)):
\r
465 def create_bone_weight(scene, mqo, armature_object, objects):
\r
467 create mikoto bone weight.
\r
470 # setup mikoto anchors
\r
471 for o in mqo.objects:
\r
472 if o.name.startswith("anchor"):
\r
474 name=mqo.materials[f.material_index].name
\r
475 if name.endswith('[]'):
\r
476 basename=name[0:-2]
\r
477 v=o.vertices[f.indices[0]]
\r
480 name_L=basename+'_L'
\r
481 if not name_L in anchorMap:
\r
482 anchorMap[name_L]=MikotoAnchor()
\r
483 anchorMap[name_L].push(f, o.vertices)
\r
486 name_R=basename+'_R'
\r
487 if not name_R in anchorMap:
\r
488 anchorMap[name_R]=MikotoAnchor()
\r
489 anchorMap[name_R].push(f, o.vertices)
\r
491 print("no side", v)
\r
493 if not name in anchorMap:
\r
494 anchorMap[name]=MikotoAnchor()
\r
495 anchorMap[name].push(f, o.vertices)
\r
498 # add armature modifier
\r
499 mod=o.modifiers.append(Modifier.Types.ARMATURE)
\r
500 mod[Modifier.Settings.OBJECT] = armature_object
\r
501 mod[Modifier.Settings.ENVELOPES] = False
\r
502 o.makeDisplayList()
\r
503 # create vertex group
\r
504 mesh=o.getData(mesh=True)
\r
505 for name in anchorMap.keys():
\r
506 mesh.addVertGroup(name)
\r
509 # assing vertices to vertex group
\r
511 mesh=o.getData(mesh=True)
\r
512 for i, mvert in enumerate(mesh.verts):
\r
514 for name, anchor in anchorMap.items():
\r
515 weight=anchor.calcWeight(mvert.co)
\r
517 mesh.assignVertsToGroup(
\r
518 name, [i], weight, Mesh.AssignModes.ADD)
\r
521 # debug orphan vertex
\r
522 print('orphan', mvert)
\r
526 def __execute(filename, scene, scale=1.0):
\r
529 if not io.read(filename):
\r
530 print("fail to load",filename)
\r
534 materials, imageMap=__createMaterials(scene, io, os.path.dirname(filename))
\r
537 root=bl.createEmptyObject(scene, os.path.basename(filename))
\r
538 objects=__createObjects(scene, io, root, materials, imageMap, scale)
\r
541 # create mikoto bone
\r
542 armature_object=create_armature(scene, io)
\r
543 if armature_object:
\r
544 root.makeParent([armature_object])
\r
546 # create bone weight
\r
547 create_bone_weight(scene, io, armature_object, objects)
\r
550 ###############################################################################
\r
552 ###############################################################################
\r
555 def execute_24(filename):
\r
559 filename=filename.decode(bl.INTERNAL_ENCODING)
\r
560 print("##start mqo_import.py##")
\r
561 print(bl.INTERNAL_ENCODING, bl.FS_ENCODING)
\r
562 print("parse mqo file: %s" % (filename))
\r
564 Blender.Window.WaitCursor(1)
\r
565 t = Blender.sys.time()
\r
568 scene = Blender.Scene.GetCurrent()
\r
569 __execute(filename, scene)
\r
572 print('finished in %.2f seconds' % (Blender.sys.time()-t))
\r
575 Blender.Window.WaitCursor(0)
\r
578 Blender.Window.FileSelector(execute_24, 'Import MQO', '*.mqo')
\r
581 def execute_25(*args):
\r
585 class IMPORT_OT_mqo(bpy.types.Operator):
\r
586 '''Import from Metasequoia file format (.mqo)'''
\r
587 bl_idname = "import_scene.mqo"
\r
588 bl_label = 'Import MQO'
\r
590 # List of operator properties, the attributes will be assigned
\r
591 # to the class instance from the operator settings before calling.
\r
593 path = StringProperty(
\r
595 description="File path used for importing the MQO file",
\r
596 maxlen= 1024, default= "")
\r
597 filename = StringProperty(
\r
599 description="Name of the file.")
\r
600 directory = StringProperty(
\r
602 description="Directory of the file.")
\r
604 scale = FloatProperty(
\r
606 description="Scale the MQO by this value",
\r
607 min=0.0001, max=1000000.0,
\r
608 soft_min=0.001, soft_max=100.0, default=0.01)
\r
610 def execute(self, context):
\r
612 self.properties.path,
\r
614 self.properties.scale)
\r
617 def invoke(self, context, event):
\r
619 wm.add_fileselect(self)
\r
620 return 'RUNNING_MODAL'
\r
624 def menu_func(self, context):
\r
625 self.layout.operator(
\r
626 IMPORT_OT_mqo.bl_idname,
\r
627 text="Metasequoia (.mqo)")
\r
630 bpy.types.register(IMPORT_OT_mqo)
\r
631 bpy.types.INFO_MT_file_import.append(menu_func)
\r
634 bpy.types.unregister(IMPORT_OT_mqo)
\r
635 bpy.types.INFO_MT_file_import.remove(menu_func)
\r
637 if __name__=="__main__":
\r