Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Reads a qubicle file, by @eigenbom 2017
- # From reference implementation:
- # http://minddesk.com/wiki/index.php?title=Qubicle_Constructor_1:Data_Exchange_With_Qubicle_Binary
- # TODO: Process colours etc
- import sys, struct
- def fopen(filename):
- return open(filename, "rb")
- def fread_byte(f):
- return struct.unpack("b", f.read(1))[0]
- def fread_uint32(f):
- return struct.unpack("I", f.read(4))[0]
- def fread_int32(f):
- return struct.unpack("i", f.read(4))[0]
- def fread_string(f, length):
- return f.read(length)
- def array(x_size, y_size, z_size):
- return [[[0 for x in range(x_size)] for y in range(y_size)] for z in range(z_size)]
- def loadQubicleBinary(filename):
- VERSION = 0x00000101 # 1.1.0.0
- CODEFLAG = 2
- NEXTSLICEFLAG = 6
- file = fopen(filename)
- version = fread_uint32(file)
- colorFormat = fread_uint32(file)
- zAxisOrientation = fread_uint32(file)
- compressed = fread_uint32(file)
- visibilityMaskEncoded = fread_uint32(file)
- numMatrices = fread_uint32(file)
- matrixList = []
- if version!=VERSION:
- print("Unknown version!")
- return None
- print("Compressed" if compressed else "Uncompressed")
- for i in range(numMatrices): # for each matrix stored in file
- # read matrix name
- nameLength = fread_byte(file)
- name = fread_string(file, nameLength)
- # read matrix size
- sizeX = fread_uint32(file)
- sizeY = fread_uint32(file)
- sizeZ = fread_uint32(file)
- # read matrix position (in this example the position is irrelevant)
- posX = fread_int32(file)
- posY = fread_int32(file)
- posZ = fread_int32(file)
- print("Reading object %s of size %dx%dx%d.."%(name, sizeX, sizeY, sizeZ))
- # create matrix and add to matrix list
- matrix = array(sizeX, sizeY, sizeZ)
- if (compressed == 0):
- print("Warning, uncompressed files haven't been tested!")
- for z in range(sizeZ):
- for y in range(sizeY):
- for x in range(sizeX):
- matrix[z][y][x] = fread_uint32(file)
- else:
- z = 0
- while z < sizeZ:
- index = 0
- while True:
- data = fread_uint32(file)
- if data == NEXTSLICEFLAG:
- break
- elif data == CODEFLAG:
- count = fread_uint32(file)
- data = fread_uint32(file)
- for j in range(count):
- x = index % sizeX
- y = index // sizeX
- matrix[z][y][x] = data
- index += 1
- else:
- x = index % sizeX
- y = index // sizeX
- matrix[z][y][x] = data
- index += 1
- z += 1
- matrixList.append(matrix)
- return matrixList
- if __name__=="__main__":
- data = loadQubicleBinary(r"rawfiles\Voxel\Skull.qb")
- for mtx in data:
- print(mtx)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement