#!BPY """ Name: 'Metasequoia Object (.mqo)...' Blender: 237 Group: 'Import' Tooltip: 'Import from MQO file format. (.mqo)' """ __author__ = ["Ry"] __url__ = ("http://jyoken.net") __version__ = "0.93" __bpydoc__ = """\ mqo Importer This script imports a mqo file and the materials into Blender for editing. Changes: """ # Importing modules import Blender, meshtools from Blender import NMesh, Scene, Object, Material, Image, Texture import sys, struct, string, math import os import re RE_FLOAT = "[-+]?(\d+(\.\d*)?|\d*\.\d+)([eE][-+]?\d+)?" RESIZE = 0.01 nmaterials=0 materials=[] uniqueInt=0 texturedict={} ###################################################### # Helper functions ###################################################### def read_split(file): return file.readline().split(" ") def skip_to_end(file, skip_chunk): buffer_size=skip_chunk.length-skip_chunk.bytes_read binary_format=str(buffer_size)+"c" temp_data=file.read(struct.calcsize(binary_format)) skip_chunk.bytes_read+=buffer_size def getUniqueName(name): newName = name uniqueInt=0 while 1: try: ob = Object.Get(newName) # Okay, this is working, so lets make a new name newName = '%s.%d' % (name, uniqueInt) uniqueInt +=1 except AttributeError: if newName not in NMesh.GetNames(): print "uniqueName:",newName return newName else: newName = '%s.%d' % (name, uniqueInt) uniqueInt +=1 except ValueError: if newName not in NMesh.GetNames(): print "uniqueName:",newName return newName else: newName = '%s.%d' % (name, uniqueInt) uniqueInt +=1 def add_texture_to_material(image, texture, material, mapto): if mapto=="DIFFUSE": map=Texture.MapTo.COL elif mapto=="SPECULAR": map=Texture.MapTo.SPEC elif mapto=="OPACITY": map=Texture.MapTo.ALPHA flg=material.getMode()|Material.Modes['RAYTRANSP'] material.setMode(flg) elif mapto=="BUMP": map=Texture.MapTo.NOR else: print "\tError: Cannot map to ", mapto return texture.setImage(image) texture_list=material.getTextures() index=0 for tex in texture_list: if tex==None: material.setTexture(index,texture,Texture.TexCo.UV,map) material.getTextures()[index].blendmode=Texture.BlendModes.MULTIPLY return else: index+=1 if index>10: print "\tError: Cannot add diffuse map. Too many textures" ## ## MQOローダ(仮) ## ## def load_header(file): header=file.readline() format=file.readline() header=header.strip("\r\n") format=format.strip("\r\n") if(header!="Metasequoia Document"): print "\tError: It is not mqo file." print "header is :"+header+"*" return -1 if(format!="Format Text Ver 1.0"): print "\tError: This format is not supported." return -1 print "support format" return 0 # マテリアルを解析 def lex_material(content,name): global materials new_material=Blender.Material.New() new_material.setName(name) for i in range(0,len(content)): k=content[i].split("(") print k[0]+"*" if(k[0]=="shader"): shader=int(re.split( " ",content[i].rstrip("\)")[7:] )[0]) #0 classic #1 constant #2 lambert #3 phong #4 blinn if shader==3 : new_material.setSpecShader(Material.Shaders.SPEC_PHONG) elif shader==4: new_material.setSpecShader(Material.Shaders.SPEC_BLINN) print "shader: ", shader elif(k[0]=="col"): col=[float(c) for c in re.split( " ",content[i].rstrip("\)")[4:] )] new_material.setRGBCol(col[:3]) new_material.setAlpha(col[3]) if col[3]<1: flg=new_material.getMode()|Material.Modes['RAYTRANSP'] new_material.setMode(flg) print "col: ", col elif(k[0]=="dif"): dif=float(re.split( " ",content[i].rstrip("\)")[4:] )[0]) new_material.setRef(dif) print "dif: ", dif elif(k[0]=="amb"): amb=float(re.split( " ",content[i].rstrip("\)")[4:] )[0]) #new_material.mirCol=[amb,amb,amb] new_material.setAmb(amb) print "amb: ", amb elif(k[0]=="emi"): emi=float(re.split( " ",content[i].rstrip("\)")[4:] )[0]) new_material.setEmit(emi) print "emi: ", emi elif(k[0]=="spc"): spc=float(re.split( " ",content[i].rstrip("\)")[4:] )[0]) #new_material.specCol=[spc,spc,spc] new_material.setSpec(spc) print "spc: ", spc elif(k[0]=="power"): pw=float(re.split( " ",content[i].rstrip("\)")[6:] )[0]) new_material.setHardness( int(pw*2.5+1) ) print "power: ", pw elif(k[0]=="tex"): tex=re.split( " ",content[i].rstrip("\)")[4:] )[0].strip('"') print "tex: ",tex new_texture=Blender.Texture.New('Diffuse') new_texture.setType('Image') try: img = Image.Load(tex) texturedict[new_material.name]=img except IOError: fname = os.path.join( os.path.dirname(FILENAME), tex) try: img = Image.Load(fname) texturedict[new_material.name]=img except IOError: print "\tERROR: failed to load image ",tex texturedict[new_material.name] = None # Dummy img=Blender.Image.New(fname,1,1,24) #blank image #add the map to the material in the right channel add_texture_to_material(img, new_texture, new_material, "DIFFUSE") elif(k[0]=="aplane"): tex=re.split( " ",content[i].rstrip("\)")[7:] )[0].strip('"') print "aplane: ",tex new_texture=Blender.Texture.New('Opacity') new_texture.setType('Image') try: img = Image.Load(tex) texturedict[new_material.name]=img except IOError: fname = os.path.join( os.path.dirname(FILENAME), tex) try: img = Image.Load(fname) texturedict[new_material.name]=img except IOError: print "\tERROR: failed to load image ",tex texturedict[new_material.name] = None # Dummy img=Blender.Image.New(fname,1,1,24) #blank image #add the map to the material in the right channel add_texture_to_material(img, new_texture, new_material, "OPACITY") materials.append(new_material) # 頂点を解析 def lex_vertex(file,nvertex,mesh): global nmaterials for i in range(nvertex): r=file.readline() vv=[float(x)*RESIZE for x in r.split(" ")] v=NMesh.Vert(vv[0],-vv[2],vv[1]) for j in range(nmaterials): mesh[j].verts.append(v) ## 三角形の面 def lex_face_sub3(content,mesh): m=0 uv=[] for i in range(len(content)): k=content[i].split("(") if(k[0]=="V"): vt=[int(x) for x in re.split( " ",content[i].rstrip("\)")[2:] )] vt.reverse() elif(k[0]=="UV"): uvtmp=[float(x) for x in re.split( " ",content[i].rstrip("\)")[3:] )] subzero=0. for i in range(len(uvtmp)): if(math.floor(uvtmp[i])