Advertisement
Guest User

I need update Python version 2.6.2 to 3.x

a guest
Sep 10th, 2017
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.04 KB | None | 0 0
  1. import newGameLib
  2. from newGameLib import *
  3. import Blender 
  4.            
  5.    
  6. def xmlParser(filename):
  7.     matFile=filename+'_data'+os.sep+'materials.xml'
  8.     if os.path.exists(matFile)==True:
  9.         materials={}
  10.         xml=Xml()
  11.         xml.input=open(matFile,'r')
  12.         xml.parse()
  13.         materialNodeList=xml.find(xml.root,'Material')
  14.         for materialNode in materialNodeList:
  15.             chunks=materialNode.chunks
  16.             materials[chunks['name']]={}
  17.             materials[chunks['name']]['diffuse']=None
  18.             materials[chunks['name']]['normalmap']=None
  19.             materials[chunks['name']]['specularmap']=None
  20.             if 'diffuse' in chunks:
  21.                 materials[chunks['name']]['diffuse']=chunks['diffuse']
  22.             if 'normalmap' in chunks:  
  23.                 materials[chunks['name']]['normalmap']=chunks['normalmap']
  24.             if 'specularmap' in chunks:
  25.                 materials[chunks['name']]['specularmap']=chunks['specularmap']
  26.         return materials
  27.     else:
  28.         return None
  29.    
  30. def modelParser(filename,g):
  31.     matList=[] 
  32.     g.seek(8)
  33.     mesh=None
  34.     skeleton=None
  35.     skin=None
  36.     material=None
  37.     while(True):
  38.         chunk = g.i(1)[0]
  39.         if chunk==0:
  40.             name = g.word(4)
  41.             seek = g.i(1)[0]
  42.             back = g.tell()
  43.             if name=='SRTM':
  44.                 materials=xmlParser(filename)              
  45.                 g.tell()
  46.                 matCount=g.i(1)[0]
  47.                 for m in range(matCount):
  48.                     material=Mat()
  49.                     g.i(1)
  50.                     g.word(4)
  51.                     g.i(1)
  52.                     g.H(1)
  53.                     matName=g.word(g.i(1)[0])
  54.                     g.i(1)
  55.                     g.seek(26,1)
  56.                     texDir=g.dirname
  57.                     diffuse=texDir+os.sep+g.word(g.i(1)[0])
  58.                     specular=texDir+os.sep+g.word(g.i(1)[0])
  59.                     normal=texDir+os.sep+g.word(g.i(1)[0])
  60.                     g.tell()
  61.                     g.i(1)
  62.                     g.word(g.i(1)[0])
  63.                     g.i(7)
  64.                     g.word(g.i(1)[0])
  65.                     g.word(g.i(1)[0])
  66.                     g.read(g.i(1)[0])
  67.                     g.i(4)
  68.                     g.tell()
  69.                     if materials!=None:
  70.                         texDir=g.dirname
  71.                         if matName in materials:
  72.                             if materials[matName]['diffuse'] is not None:
  73.                                 material.diffuse=texDir+os.sep+materials[matName]['diffuse']
  74.                             if materials[matName]['normalmap'] is not None:
  75.                                 material.normal=texDir+os.sep+materials[matName]['normalmap']
  76.                             if materials[matName]['specularmap'] is not None:
  77.                                 material.specular=texDir+os.sep+materials[matName]['specularmap']
  78.                     matList.append(material)
  79.                     g.word(4)
  80.             if name == 'HSMV':
  81.                 g.word(4)
  82.                 g.i(1)[0],g.f(1)[0],g.i(1)[0]
  83.                 v=g.B(15*4)
  84.                 nVerts = g.i(1)[0]
  85.                 g.B(13)
  86.                 nFaces = g.i(1)[0]
  87.                 g.seek(back+108)
  88.                 mesh=Mesh()
  89.                 mesh.TRIANGLE=True
  90.                 for m in range(nVerts):
  91.                     t=g.tell()
  92.                     mesh.vertPosList.append(g.f(3))
  93.                     g.seek(t+v[16])
  94.                     mesh.vertUVList.append(g.f(2))
  95.                     g.seek(t+v[8])
  96.                 mesh.indiceList=g.H(nFaces*3)
  97.             if name == 'LEKS':
  98.                 skeleton=Skeleton()
  99.                 skeleton.BONESPACE=True
  100.                 g.H(1)[0]
  101.                 nBones = g.H(1)[0]
  102.                 for m in range(nBones):
  103.                     bone=Bone()
  104.                     bone.name=g.word(g.i(1)[0])[-25:]
  105.                     bone.parentID=g.h(1)[0]
  106.                     g.f(3)
  107.                     g.f(4)
  108.                     bone.posMatrix=TranslationMatrix(Vector(g.f(3)))
  109.                     qx,qy,qz,qw = g.f(4)
  110.                     bone.rotMatrix=Quaternion(qw,qx,qy,qz).toMatrix().invert()
  111.                     skeleton.boneList.append(bone)
  112.                 skeleton.draw()
  113.             if name == 'THGW':
  114.                 g.H(2)
  115.                 skin=Skin()
  116.                 for m in range(nVerts):
  117.                     nGr = g.H(1)[0]
  118.                     indices=[]
  119.                     weights=[]
  120.                     for n in range(nGr):
  121.                         indices.append(g.H(1)[0])
  122.                         g.B(1)
  123.                         weights.append(g.B(1)[0]/255.0)
  124.                     mesh.skinIndiceList.append(indices)
  125.                     mesh.skinWeightList.append(weights)
  126.                    
  127.             if name=='MBUS':
  128.                 g.i(3)
  129.                 for m in range(g.i(1)[0]):
  130.                     v=g.i(8)
  131.                     g.f(6)
  132.                     w=g.i(2)
  133.                     matID=w[0]
  134.                     if len(matList)!=0:
  135.                         material=matList[matID]
  136.                         material.TRIANGLE=True
  137.                         material.IDStart=v[0]
  138.                         material.IDCount=v[1]
  139.                         mesh.matList.append(material)
  140.                        
  141.                
  142.             g.seek(back+seek)
  143.             g.i(1)[0]
  144.             g.word(4)
  145.         elif chunk==-1:break
  146.         else:
  147.             print 'unknow chunk',chunk
  148.             break
  149.        
  150.        
  151.     if mesh is not None:
  152.         if skeleton is not None:
  153.             if skin is not None:
  154.                 skin.boneMap=skeleton.boneNameList
  155.                 mesh.skinList.append(skin) 
  156.         if skeleton is not None:
  157.             mesh.BINDSKELETON='armature'
  158.     #   if material is not None:   
  159.         #   mesh.facesgroupslist.append(material)      
  160.         mesh.draw()
  161.    
  162.        
  163.    
  164. def SOPB(boneCount,g):
  165.     actionFile.write('SOPB')
  166.     frameCount=g.i(1)[0]
  167.     p.i([frameCount])
  168.     for m in range(frameCount):
  169.         actionFile.write(g.read(4+boneCount*12))
  170.            
  171.        
  172. def TORB(boneCount,g):
  173.     actionFile.write('TORB')
  174.     frameCount=g.i(1)[0]
  175.     p.i([frameCount])
  176.     for m in range(frameCount):
  177.         actionFile.write(g.read(4+boneCount*16))
  178.    
  179.    
  180.        
  181. def DAEH(g):
  182.     chunk = g.i(1)[0]
  183.     name = g.word(4)
  184.     size = g.i(1)[0]
  185.     back = g.tell()
  186.     animCount=g.i(1)[0]
  187.     g.seek(back+size)  
  188.     chunk = g.i(1)[0]
  189.     name = g.word(4)
  190.     return animCount
  191.  
  192. def LEKS(g):
  193.     global skeleton
  194.     chunk = g.i(1)[0]
  195.     name = g.word(4)
  196.     size = g.i(1)[0]
  197.     back = g.tell()
  198.     A=g.H(2)
  199.     skeleton=Skeleton()
  200.     skeleton.BONESPACE=True
  201.     for m in range(A[1]):
  202.         bone=Bone()
  203.         skeleton.boneList.append(bone)
  204.         bone.name=g.word(g.i(1)[0])
  205.         bone.parentID=g.h(1)[0]
  206.         parentPosMatrix=VectorMatrix(g.f(3))
  207.         parentRotMatrix=QuatMatrix(g.f(4)).resize4x4()
  208.         parentMatrix=parentRotMatrix.invert()*parentPosMatrix
  209.         posMatrix=VectorMatrix(g.f(3))
  210.         rotMatrix=QuatMatrix(g.f(4)).resize4x4()
  211.         matrix=rotMatrix.invert()*posMatrix
  212.         bone.matrix=matrix#*parentMatrix
  213.     skeleton.draw()
  214.     g.H(1)[0]
  215.     chunk = g.i(1)[0]
  216.     name = g.word(4)
  217.    
  218.        
  219.        
  220. def INAB(g):
  221.     global actionFile,p
  222.     chunk = g.i(1)[0]
  223.     name = g.word(4)
  224.     size = g.i(1)[0]
  225.     back = g.tell()
  226.     value=g.H(3)
  227.     boneCount=value[2]
  228.     animName=g.word(g.i(1)[0]) 
  229.     actionPath=g.dirname+os.sep+g.basename+'_animfiles'
  230.     if os.path.exists(actionPath)==False:
  231.         os.makedirs(actionPath)
  232.     actionFile=open(actionPath+os.sep+animName+'.action','wb')
  233.     p=BinaryReader(actionFile) 
  234.     p.i([len(skeleton.boneNameList)])
  235.     for name in skeleton.boneNameList:
  236.         actionFile.write(name+'\x00')
  237.    
  238.     start=g.tell()
  239.     sum=0
  240.     while(True):
  241.         g.i(1)[0]
  242.         iname = g.word(4)
  243.         isize = g.i(1)[0]
  244.         iback = g.tell()
  245.         if iname=='SOPB':SOPB(boneCount,g)
  246.         elif iname=='TORB':TORB(boneCount,g)
  247.        
  248.         g.seek(iback+isize)
  249.         g.i(1)[0]
  250.         g.word(4)
  251.         sum+=4+4+4+4+4+isize
  252.         if sum>=size-20:break  
  253.     g.seek(start)  
  254.    
  255.     actionFile.close()
  256.        
  257.        
  258.     g.seek(back+size)
  259.     chunk = g.i(1)[0]
  260.     name = g.word(4)
  261.    
  262.            
  263.        
  264. def MINA(size,g):
  265.     sum=0
  266.     g.H(1)     
  267.     animCount=DAEH(g)
  268.     LEKS(g)
  269.     for m in range(animCount):
  270.         INAB(g)
  271.    
  272. def animParser(filename,g):
  273.     g.seek(8)
  274.     while(True):
  275.         if g.tell()>=g.fileSize():break
  276.         chunk = g.i(1)[0]
  277.         if chunk==0:
  278.             name = g.word(4)
  279.             size = g.i(1)[0]
  280.             back = g.tell()
  281.             if size!=-1:
  282.                 if name=='MINA':   
  283.                     MINA(size,g)
  284.                 g.seek(back+size)
  285.                
  286.     g.tell()
  287.        
  288.        
  289. def actionParser(filename,g):
  290.     action=Action()
  291.     action.name=g.basename
  292.     action.BONESPACE=True
  293.     action.BONESORT=True
  294.     boneCount=g.i(1)[0]
  295.     for n in range(boneCount):
  296.         bone=ActionBone()
  297.         bone.name=g.find('\x00')
  298.         action.boneList.append(bone)
  299.     while(True):
  300.         if g.tell()>=g.fileSize():break
  301.         chunk=g.word(4)
  302.         if chunk=='TORB':
  303.             frameCount=g.i(1)[0]
  304.             for m in range(frameCount):
  305.                 time=g.f(1)[0]*33
  306.                 for n in range(boneCount):
  307.                     value=g.f(4)
  308.                     bone=action.boneList[n]
  309.                     bone.rotFrameList.append(time)
  310.                     bone.rotKeyList.append(QuatMatrix(value).invert().resize4x4())
  311.         if chunk=='SOPB':
  312.             frameCount=g.i(1)[0]
  313.             for m in range(frameCount):
  314.                 time=g.f(1)[0]*33
  315.                 for n in range(boneCount):
  316.                     value=g.f(3)
  317.                     bone=action.boneList[n]
  318.                     bone.posFrameList.append(time)
  319.                     bone.posKeyList.append(VectorMatrix(value))
  320.     action.draw()
  321.     action.setContext()
  322.                
  323.            
  324.    
  325. def Parser(filename):  
  326.     ext=filename.split('.')[-1].lower()
  327.    
  328.    
  329.        
  330.     if ext=='model':
  331.         file=open(filename,'rb')
  332.         g=BinaryReader(file)
  333.         modelParser(filename,g)
  334.         file.close()
  335.    
  336.     if ext=='anim':
  337.         file=open(filename,'rb')
  338.         g=BinaryReader(file)
  339.         animParser(filename,g)
  340.         file.close()
  341.        
  342.     if ext=='action':
  343.         file=open(filename,'rb')
  344.         g=BinaryReader(file)
  345.         #g.logOpen()
  346.         actionParser(filename,g)
  347.         #g.logClose()
  348.         file.close()
  349.    
  350.  
  351.    
  352. Blender.Window.FileSelector(Parser,'import','SELECT: *.model, *.anim, *.action')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement