Advertisement
eigenbom

Qubicle Import for Python

Jan 13th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. # Reads a qubicle file, by @eigenbom 2017
  2. # From reference implementation:
  3. #   http://minddesk.com/wiki/index.php?title=Qubicle_Constructor_1:Data_Exchange_With_Qubicle_Binary
  4. # TODO: Process colours etc
  5.  
  6. import sys, struct
  7.  
  8. def fopen(filename):
  9.     return open(filename, "rb")
  10.  
  11. def fread_byte(f):
  12.     return struct.unpack("b", f.read(1))[0]
  13.  
  14. def fread_uint32(f):
  15.     return struct.unpack("I", f.read(4))[0]
  16.  
  17. def fread_int32(f):
  18.     return struct.unpack("i", f.read(4))[0]
  19.  
  20. def fread_string(f, length):
  21.     return f.read(length)
  22.  
  23. def array(x_size, y_size, z_size):
  24.     return [[[0 for x in range(x_size)] for y in range(y_size)] for z in range(z_size)]
  25.  
  26. def loadQubicleBinary(filename):
  27.     VERSION = 0x00000101 # 1.1.0.0
  28.     CODEFLAG = 2
  29.     NEXTSLICEFLAG = 6
  30.  
  31.     file = fopen(filename)
  32.     version = fread_uint32(file)
  33.     colorFormat = fread_uint32(file)
  34.     zAxisOrientation = fread_uint32(file)
  35.     compressed = fread_uint32(file)
  36.     visibilityMaskEncoded = fread_uint32(file)
  37.     numMatrices = fread_uint32(file)
  38.     matrixList = []
  39.  
  40.     if version!=VERSION:
  41.         print("Unknown version!")
  42.         return None
  43.  
  44.     print("Compressed" if compressed else "Uncompressed")
  45.    
  46.     for i in range(numMatrices): # for each matrix stored in file    
  47.         # read matrix name
  48.         nameLength = fread_byte(file)
  49.         name = fread_string(file, nameLength)
  50.  
  51.         # read matrix size
  52.         sizeX = fread_uint32(file)
  53.         sizeY = fread_uint32(file)
  54.         sizeZ = fread_uint32(file)
  55.  
  56.         # read matrix position (in this example the position is irrelevant)
  57.         posX = fread_int32(file)
  58.         posY = fread_int32(file)
  59.         posZ = fread_int32(file)
  60.  
  61.         print("Reading object %s of size %dx%dx%d.."%(name, sizeX, sizeY, sizeZ))
  62.    
  63.         # create matrix and add to matrix list
  64.         matrix = array(sizeX, sizeY, sizeZ)
  65.    
  66.         if (compressed == 0):
  67.             print("Warning, uncompressed files haven't been tested!")
  68.             for z in range(sizeZ):
  69.                 for y in range(sizeY):                    
  70.                     for x in range(sizeX):
  71.                         matrix[z][y][x] = fread_uint32(file)
  72.         else:
  73.             z = 0
  74.             while z < sizeZ:
  75.                 index = 0
  76.                 while True:
  77.                     data = fread_uint32(file)                    
  78.                     if data == NEXTSLICEFLAG:
  79.                         break
  80.                     elif data == CODEFLAG:
  81.                         count = fread_uint32(file)
  82.                         data = fread_uint32(file)                    
  83.                         for j in range(count):
  84.                             x = index % sizeX
  85.                             y = index // sizeX
  86.                             matrix[z][y][x] = data                            
  87.                             index += 1
  88.                     else:
  89.                         x = index % sizeX
  90.                         y = index // sizeX
  91.                         matrix[z][y][x] = data
  92.                         index += 1
  93.                 z += 1
  94.        
  95.         matrixList.append(matrix)
  96.  
  97.     return matrixList
  98.  
  99. if __name__=="__main__":
  100.     data = loadQubicleBinary(r"rawfiles\Voxel\Skull.qb")
  101.     for mtx in data:
  102.         print(mtx)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement