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
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
25 0.9 20100626: refactoring.
\r
26 2.0 20100724: update for Blender2.53.
\r
27 2.1 20100731: add full python module.
\r
28 2.2 20101005: update for Blender2.54.
\r
32 'category': 'Import/Export',
\r
33 'name': 'Import: Metasequioa Model Format (.mqo)',
\r
34 'author': 'ousttrue',
\r
36 'blender': (2, 5, 3),
\r
37 'location': 'File > Import',
\r
38 'description': 'Import from the Metasequioa Model Format (.mqo)',
\r
39 'warning': '', # used for warning icon and text in addons panel
\r
40 'wiki_url': 'http://sourceforge.jp/projects/meshio/wiki/FrontPage',
\r
41 'tracker_url': 'http://sourceforge.jp/ticket/newticket.php?group_id=5081',
\r
49 from meshio import mqo
\r
50 print('use meshio C module')
\r
53 from pymeshio import mqo
\r
56 return sys.version_info[0]<3
\r
61 from Blender import Mathutils
\r
67 def createMqoMaterial(m):
\r
68 material = Blender.Material.New(
\r
69 m.getName().encode(bl.INTERNAL_ENCODING))
\r
70 #material.mode |= Blender.Material.Modes.SHADELESS
\r
72 material.rgbCol = [m.color.r, m.color.g, m.color.b]
\r
73 material.alpha = m.color.a
\r
75 material.amb=m.ambient
\r
76 material.spec=m.specular
\r
77 material.hard=int(255 * m.power)
\r
78 material.emit=m.emit
\r
88 def createMqoMaterial(m):
\r
89 material = bpy.data.materials.new(m.getName())
\r
92 material.diffuse_shader='FRESNEL'
\r
94 material.diffuse_shader='LAMBERT'
\r
96 material.diffuse_color=[m.color.r, m.color.g, m.color.b]
\r
97 material.diffuse_intensity=m.diffuse
\r
98 material.alpha=m.color.a
\r
100 material.ambient = m.ambient
\r
101 #material.specular = m.specular
\r
102 material.emit=m.emit
\r
106 def has_mikoto(mqo):
\r
107 #for o in mqo.objects:
\r
108 # if o.getName().startswith('bone'):
\r
110 # if o.getName().startswith('sdef'):
\r
112 # if o.getName().startswith('anchor'):
\r
117 def __createMaterials(mqo, directory):
\r
119 create blender materials and renturn material list.
\r
124 if len(mqo.materials)>0:
\r
125 for material_index, m in enumerate(mqo.materials):
\r
127 material=createMqoMaterial(m)
\r
128 materials.append(material)
\r
130 texture_name=m.getTexture()
\r
131 if texture_name!='':
\r
132 if texture_name in textureMap:
\r
133 texture=textureMap[texture_name]
\r
135 # load texture image
\r
136 if os.path.isabs(texture_name):
\r
138 path = texture_name
\r
141 path = os.path.join(directory, texture_name)
\r
143 path=path.replace("\\", "/")
\r
144 if os.path.exists(path):
\r
145 print("create texture:", path)
\r
146 texture, image=bl.texture.create(path)
\r
147 textureMap[texture_name]=texture
\r
148 imageMap[material_index]=image
\r
150 print("%s not exits" % path)
\r
152 bl.material.addTexture(material, texture)
\r
156 return materials, imageMap
\r
159 def __createObjects(mqo, root, materials, imageMap, scale):
\r
161 create blender mesh objects.
\r
166 for o in mqo.objects:
\r
167 mesh, mesh_object=bl.mesh.create(o.getName())
\r
170 stack_depth=len(stack)-1
\r
171 #print(o.depth, stack_depth)
\r
172 if o.depth<stack_depth:
\r
173 for i in range(stack_depth-o.depth):
\r
175 bl.object.makeParent(stack[-1], mesh_object)
\r
176 stack.append(mesh_object)
\r
178 if o.getName().startswith('sdef'):
\r
179 objects.append(mesh_object)
\r
180 elif o.getName().startswith('anchor'):
\r
181 bl.object.setLayerMask(mesh_object, [0, 1])
\r
182 elif o.getName().startswith('bone'):
\r
183 bl.object.setLayerMask(mesh_object, [0, 1])
\r
186 vertices=[(v.x * scale, -v.z * scale, v.y * scale) for v in o.vertices]
\r
192 for i in reversed(range(f.index_count)):
\r
193 face_indices.append(f.getIndex(i))
\r
194 faces.append(face_indices)
\r
195 materialMap[f.material_index]=True
\r
196 bl.mesh.addGeometry(mesh, vertices, faces)
\r
198 # blender limits 16 materials per mesh
\r
199 for i, material_index in enumerate(materialMap.keys()):
\r
202 print("over 16 materials!")
\r
204 bl.mesh.addMaterial(mesh, materials[material_index])
\r
205 materialMap[material_index]=i
\r
208 assert(len(o.faces)==len(mesh.faces))
\r
209 bl.mesh.addUV(mesh)
\r
210 for i, (f, face) in enumerate(zip(o.faces, mesh.faces)):
\r
214 for j in reversed(range(f.index_count)):
\r
215 uv_array.append((f.getUV(j).x, 1.0-f.getUV(j).y))
\r
216 bl.mesh.setFaceUV(mesh, i, face, uv_array,
\r
217 imageMap.get(f.material_index, None))
\r
218 if f.material_index in materialMap:
\r
219 bl.face.setMaterial(face, materialMap[f.material_index])
\r
220 bl.face.setSmooth(face, True)
\r
224 bl.modifier.addMirror(mesh_object)
\r
227 bl.mesh.setSmooth(mesh, o.smoothing)
\r
230 bl.mesh.recalcNormals(mesh_object)
\r
235 ###############################################################################
\r
236 # for mqo mikoto bone.
\r
237 ###############################################################################
\r
238 class MikotoBone(object):
\r
241 'iHead', 'iTail', 'iUp',
\r
242 'vHead', 'vTail', 'vUp',
\r
243 'parent', 'isFloating',
\r
246 def __init__(self, face=None, vertices=None, materials=None):
\r
248 self.isFloating=False
\r
254 self.name=materials[face.material_index].name.encode('utf-8')
\r
256 i0=face.getIndex(0)
\r
257 i1=face.getIndex(1)
\r
258 i2=face.getIndex(2)
\r
265 sqNorm0=e01.getSqNorm()
\r
266 sqNorm1=e12.getSqNorm()
\r
267 sqNorm2=e20.getSqNorm()
\r
268 if sqNorm0>sqNorm1:
\r
269 if sqNorm1>sqNorm2:
\r
275 if sqNorm0>sqNorm2:
\r
287 if sqNorm1<sqNorm2:
\r
293 if sqNorm0<sqNorm2:
\r
303 self.vHead=vertices[self.iHead]
\r
304 self.vTail=vertices[self.iTail]
\r
305 self.vUp=vertices[self.iUp]
\r
307 if self.name.endswith('[]'):
\r
308 basename=self.name[0:-2]
\r
311 self.name="%s_L" % basename
\r
313 self.name="%s_R" % basename
\r
316 def setParent(self, parent, floating=False):
\r
318 self.isFloating=True
\r
320 parent.children.append(self)
\r
322 def printTree(self, indent=''):
\r
323 print("%s%s" % (indent, self.name))
\r
324 for child in self.children:
\r
325 child.printTree(indent+' ')
\r
328 def build_armature(armature, mikotoBone, parent=None):
\r
330 create a armature bone.
\r
332 bone = Armature.Editbone()
\r
333 bone.name = mikotoBone.name.encode('utf-8')
\r
334 armature.bones[bone.name] = bone
\r
336 bone.head = Mathutils.Vector(*mikotoBone.vHead.to_a())
\r
337 bone.tail = Mathutils.Vector(*mikotoBone.vTail.to_a())
\r
340 if mikotoBone.isFloating:
\r
343 bone.options=[Armature.CONNECTED]
\r
345 for child in mikotoBone.children:
\r
346 build_armature(armature, child, bone)
\r
349 def create_armature(mqo):
\r
354 for o in mqo.objects:
\r
355 if o.name.startswith('bone'):
\r
362 for f in boneObject.faces:
\r
363 if f.index_count!=3:
\r
364 print("invalid index_count: %d" % f.index_count)
\r
366 b=MikotoBone(f, boneObject.vertices, mqo.materials)
\r
369 ####################
\r
370 # build mikoto bone tree
\r
371 ####################
\r
372 mikotoRoot=MikotoBone()
\r
374 for b in tailMap.values():
\r
375 # each bone has unique parent or is root bone.
\r
376 if b.iHead in tailMap:
\r
377 b.setParent(tailMap[b.iHead])
\r
380 for e in boneObject.edges:
\r
381 if b.iHead==e.indices[0]:
\r
383 if e.indices[1] in tailMap:
\r
384 b.setParent(tailMap[e.indices[1]], True)
\r
387 elif b.iHead==e.indices[1]:
\r
389 if e.indices[0] in tailMap:
\r
390 b.setParent(tailMap[e.indices[0]], True)
\r
397 b.setParent(mikotoRoot, True)
\r
399 if len(mikotoRoot.children)==0:
\r
400 print("no root bone")
\r
403 if len(mikotoRoot.children)==1:
\r
405 mikotoRoot=mikotoRoot.children[0]
\r
406 mikotoRoot.parent=None
\r
408 mikotoRoot.vHead=Vector3(0, 10, 0)
\r
409 mikotoRoot.vTail=Vector3(0, 0, 0)
\r
411 ####################
\r
413 ####################
\r
414 armature = Armature.New()
\r
416 armature_object = scene.objects.new(armature)
\r
418 act = Armature.NLA.NewAction()
\r
419 act.setActive(armature_object)
\r
421 armature_object.drawMode |= Object.DrawModes.XRAY
\r
422 # armature settings
\r
423 armature.drawType = Armature.OCTAHEDRON
\r
424 armature.envelopes = False
\r
425 armature.vertexGroups = True
\r
426 armature.mirrorEdit = True
\r
427 armature.drawNames=True
\r
430 armature.makeEditable()
\r
431 build_armature(armature, mikotoRoot)
\r
434 return armature_object
\r
437 class TrianglePlane(object):
\r
439 mikoto
\e$BJ}<0%\!<%s$N%"%s%+!<%&%'%$%H7W;;MQ!#
\e(B
\r
442 __slots__=['normal',
\r
445 def __init__(self, v0, v1, v2):
\r
450 def isInsideXY(self, p):
\r
451 v0=Vector2(self.v0.x, self.v0.y)
\r
452 v1=Vector2(self.v1.x, self.v1.y)
\r
453 v2=Vector2(self.v2.x, self.v2.y)
\r
457 c0=Vector2.cross(e01, p-v0)
\r
458 c1=Vector2.cross(e12, p-v1)
\r
459 c2=Vector2.cross(e20, p-v2)
\r
460 if c0>=0 and c1>=0 and c2>=0:
\r
462 if c0<=0 and c1<=0 and c2<=0:
\r
465 def isInsideYZ(self, p):
\r
466 v0=Vector2(self.v0.y, self.v0.z)
\r
467 v1=Vector2(self.v1.y, self.v1.z)
\r
468 v2=Vector2(self.v2.y, self.v2.z)
\r
472 c0=Vector2.cross(e01, p-v0)
\r
473 c1=Vector2.cross(e12, p-v1)
\r
474 c2=Vector2.cross(e20, p-v2)
\r
475 if c0>=0 and c1>=0 and c2>=0:
\r
477 if c0<=0 and c1<=0 and c2<=0:
\r
480 def isInsideZX(self, p):
\r
481 v0=Vector2(self.v0.z, self.v0.x)
\r
482 v1=Vector2(self.v1.z, self.v1.x)
\r
483 v2=Vector2(self.v2.z, self.v2.x)
\r
487 c0=Vector2.cross(e01, p-v0)
\r
488 c1=Vector2.cross(e12, p-v1)
\r
489 c2=Vector2.cross(e20, p-v2)
\r
490 if c0>=0 and c1>=0 and c2>=0:
\r
492 if c0<=0 and c1<=0 and c2<=0:
\r
496 class MikotoAnchor(object):
\r
498 mikoto
\e$BJ}<0%9%1%k%H%s$N%"%s%+!<!#
\e(B
\r
501 "triangles", "bbox",
\r
503 def __init__(self):
\r
507 def push(self, face, vertices):
\r
508 if face.index_count==3:
\r
509 self.triangles.append(TrianglePlane(
\r
510 vertices[face.indices[0]],
\r
511 vertices[face.indices[1]],
\r
512 vertices[face.indices[2]]
\r
514 elif face.index_count==4:
\r
515 self.triangles.append(TrianglePlane(
\r
516 vertices[face.indices[0]],
\r
517 vertices[face.indices[1]],
\r
518 vertices[face.indices[2]]
\r
520 self.triangles.append(TrianglePlane(
\r
521 vertices[face.indices[2]],
\r
522 vertices[face.indices[3]],
\r
523 vertices[face.indices[0]]
\r
527 self.bbox=BoundingBox(vertices[face.indices[0]])
\r
528 for i in face.indices:
\r
529 self.bbox.expand(vertices[i])
\r
532 def calcWeight(self, v):
\r
533 if not self.bbox.isInside(v):
\r
536 if self.anyXY(v.x, v.y) and self.anyYZ(v.y, v.z) and self.anyZX(v.z, v.x):
\r
541 def anyXY(self, x, y):
\r
542 for t in self.triangles:
\r
543 if t.isInsideXY(Vector2(x, y)):
\r
547 def anyYZ(self, y, z):
\r
548 for t in self.triangles:
\r
549 if t.isInsideYZ(Vector2(y, z)):
\r
553 def anyZX(self, z, x):
\r
554 for t in self.triangles:
\r
555 if t.isInsideZX(Vector2(z, x)):
\r
560 def create_bone_weight(scene, mqo, armature_object, objects):
\r
562 create mikoto bone weight.
\r
565 # setup mikoto anchors
\r
566 for o in mqo.objects:
\r
567 if o.name.startswith("anchor"):
\r
569 name=mqo.materials[f.material_index].name
\r
570 if name.endswith('[]'):
\r
571 basename=name[0:-2]
\r
572 v=o.vertices[f.indices[0]]
\r
575 name_L=basename+'_L'
\r
576 if not name_L in anchorMap:
\r
577 anchorMap[name_L]=MikotoAnchor()
\r
578 anchorMap[name_L].push(f, o.vertices)
\r
581 name_R=basename+'_R'
\r
582 if not name_R in anchorMap:
\r
583 anchorMap[name_R]=MikotoAnchor()
\r
584 anchorMap[name_R].push(f, o.vertices)
\r
586 print("no side", v)
\r
588 if not name in anchorMap:
\r
589 anchorMap[name]=MikotoAnchor()
\r
590 anchorMap[name].push(f, o.vertices)
\r
593 # add armature modifier
\r
594 mod=o.modifiers.append(Modifier.Types.ARMATURE)
\r
595 mod[Modifier.Settings.OBJECT] = armature_object
\r
596 mod[Modifier.Settings.ENVELOPES] = False
\r
597 o.makeDisplayList()
\r
598 # create vertex group
\r
599 mesh=o.getData(mesh=True)
\r
600 for name in anchorMap.keys():
\r
601 mesh.addVertGroup(name)
\r
604 # assing vertices to vertex group
\r
606 mesh=o.getData(mesh=True)
\r
607 for i, mvert in enumerate(mesh.verts):
\r
609 for name, anchor in anchorMap.items():
\r
610 weight=anchor.calcWeight(mvert.co)
\r
612 mesh.assignVertsToGroup(
\r
613 name, [i], weight, Mesh.AssignModes.ADD)
\r
616 # debug orphan vertex
\r
617 print('orphan', mvert)
\r
621 def __execute(filename, scene, scale=0.1):
\r
624 if not io.read(filename):
\r
625 bl.message("fail to load %s" % filename)
\r
629 materials, imageMap=__createMaterials(io, os.path.dirname(filename))
\r
630 if len(materials)==0:
\r
631 materials.append(bl.material.create('default'))
\r
634 root=bl.object.createEmpty(os.path.basename(filename))
\r
635 objects=__createObjects(io, root, materials, imageMap, scale)
\r
638 # create mikoto bone
\r
639 armature_object=create_armature(io)
\r
640 if armature_object:
\r
641 root.makeParent([armature_object])
\r
643 # create bone weight
\r
644 create_bone_weight(io, armature_object, objects)
\r
647 ###############################################################################
\r
649 ###############################################################################
\r
652 def execute_24(filename):
\r
653 scene=Blender.Scene.GetCurrent()
\r
654 bl.initialize('mqo_import', scene)
\r
656 filename.decode(bl.INTERNAL_ENCODING),
\r
661 Blender.Window.FileSelector(execute_24, 'Import MQO', '*.mqo')
\r
665 def execute_25(filename, scene, scale):
\r
666 bl.initialize('mqo_import', scene)
\r
667 __execute(filename, scene, scale)
\r
671 class IMPORT_OT_mqo(bpy.types.Operator):
\r
672 '''Import from Metasequoia file format (.mqo)'''
\r
673 bl_idname = "import_scene.mqo"
\r
674 bl_label = 'Import MQO'
\r
676 # List of operator properties, the attributes will be assigned
\r
677 # to the class instance from the operator settings before calling.
\r
678 filepath = bpy.props.StringProperty()
\r
679 filename = bpy.props.StringProperty()
\r
680 directory = bpy.props.StringProperty()
\r
682 scale = bpy.props.FloatProperty(
\r
684 description="Scale the MQO by this value",
\r
685 min=0.0001, max=1000000.0,
\r
686 soft_min=0.001, soft_max=100.0, default=0.1)
\r
688 def execute(self, context):
\r
690 self.properties.filepath,
\r
692 self.properties.scale)
\r
695 def invoke(self, context, event):
\r
696 wm=context.window_manager
\r
697 wm.add_fileselect(self)
\r
698 return 'RUNNING_MODAL'
\r
702 def menu_func(self, context):
\r
703 self.layout.operator(
\r
704 IMPORT_OT_mqo.bl_idname,
\r
705 text="Metasequoia (.mqo)",
\r
710 bpy.types.INFO_MT_file_import.append(menu_func)
\r
713 bpy.types.INFO_MT_file_import.remove(menu_func)
\r
715 if __name__=="__main__":
\r