File size: 8,006 Bytes
4b876a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | # io_qubicle.py
# Qubicle Constructor Binary File IO
# Copyright (c) 2014, Graham R King
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from plugin_api import register_plugin
from PySide.QtGui import QMessageBox
from struct import unpack
# http://www.minddesk.com/wiki/index.php?title=Qubicle_Constructor_1:Data_Exchange_With_Qubicle_Binary
class QubicleFile(object):
# Description of file type
description = "Qubicle Files"
# File type filter
filetype = "*.qb"
def __init__(self, api):
self.api = api
# Register our exporter
self.api.register_file_handler(self)
# Helper function to read/write uint32
def uint32(self, f, value=None):
if value is not None:
# Write
data = bytearray()
data.append((value & 0xff))
data.append((value & 0xff00) >> 8)
data.append((value & 0xff0000) >> 16)
data.append((value & 0xff000000) >> 24)
f.write(data)
else:
# Read
x = bytearray(f.read(4))
if len(x) == 4:
return x[0] | x[1] << 8 | x[2] << 16 | x[3] << 24
return 0
def int32(self, f):
return unpack('i', f.read(4))[0]
# Called when we need to save. Should raise an exception if there is a
# problem saving.
def save(self, filename):
# grab the voxel data
voxels = self.api.get_voxel_data()
# Open our file
f = open(filename, "wb")
# Version
self.uint32(f, 0x00000101)
# Color format RGBA
self.uint32(f, 0)
# Right handed z-coords
self.uint32(f, 1)
# Uncompressed
self.uint32(f, 0)
# Visability mask
self.uint32(f, 0)
# Matrix count
self.uint32(f, 1)
# Model name length
name = "Model"
f.write(str(chr(len(name))))
# Model name
f.write(name)
# X, Y, Z dimensions
self.uint32(f, voxels.width)
self.uint32(f, voxels.height)
self.uint32(f, voxels.depth)
# Matrix position
self.uint32(f, 0)
self.uint32(f, 0)
self.uint32(f, 0)
# Data
for z in xrange(voxels.depth - 1, -1, -1):
for y in xrange(voxels.height):
for x in xrange(voxels.width - 1, -1, -1):
vox = voxels.get(x, y, z)
alpha = 0xff
if not vox:
alpha = 0x00
r = (vox & 0xff000000) >> 24
g = (vox & 0xff0000) >> 16
b = (vox & 0xff00) >> 8
vox = r | g << 8 | b << 16 | alpha << 24
self.uint32(f, vox)
# Tidy up
f.close()
# sets alpha to ff and converts brga to rgba if format
def formatVox(self, vox, format):
r = (vox & 0x000000ff) >> 0
g = (vox & 0x0000ff00) >> 8
b = (vox & 0x00ff0000) >> 16
if format:
return (b << 24) | (g << 16) | (r << 8) | 0xff
return (r << 24) | (g << 16) | (b << 8) | 0xff
# Load a Qubicle Constructor binary file
def load(self, filename):
# grab the voxel data
voxels = self.api.get_voxel_data()
# Open our file
f = open(filename, "rb")
# Version
version = self.uint32(f)
# Color format 0 for RGBA and 1 for BRGA
format = self.uint32(f)
# Left handed coords
coords = self.uint32(f)
# Uncompressed
compression = self.uint32(f)
# Visability mask
mask = self.uint32(f)
# Matrix count
matrix_count = self.uint32(f)
# Warn about multiple matrices
if matrix_count > 1:
self.api.warning("Qubicle files with more than 1 matrix are not yet properly supported. "
"All matrices will be (badly) merged.")
max_width = 0
max_height = 0
max_depth = 0
for i in xrange(matrix_count):
# Name length
namelen = int(ord(f.read(1)))
name = f.read(namelen)
# X, Y, Z dimensions
width = self.uint32(f)
height = self.uint32(f)
depth = self.uint32(f)
# Don't allow huge models
if width > 127 or height > 127 or depth > 127:
raise Exception("Model to large - max 127x127x127")
if width > max_width:
max_width = width
if height > max_height:
max_height = height
if depth > max_depth:
max_depth = depth
voxels.resize(max_width, max_height, max_depth)
# Matrix position - FIXME not yet supported
dx = self.int32(f)
dy = self.int32(f)
dz = self.int32(f)
# Data
if compression:
for z in xrange(depth):
index = 0
while True:
data = self.uint32(f)
if data == 6:
break
elif data == 2:
count = self.uint32(f)
vox = self.uint32(f)
if (vox & 0xff000000) >> 24:
vox = self.formatVox(vox, format)
for j in xrange(count):
x = index % width
y = index / width
index += 1
voxels.set((width - x - 1), y, depth - z - 1 if coords == 1 else z, vox)
else:
index += count
else:
x = index % width
y = index / width
index += 1
if (data & 0xff000000) >> 24:
voxels.set((width - x - 1), y, depth - z - 1 if coords == 1 else z,
self.formatVox(data, format))
else:
for z in xrange(depth):
for y in xrange(height):
for x in xrange(width):
vox = self.uint32(f)
if (vox & 0xff000000) >> 24:
voxels.set((width - x - 1), y, depth - z - 1 if coords == 1 else z,
self.formatVox(vox, format))
# restore attachment point
if matrix_count == 1 and dx <= 0 and dy <= 0 and dz <= 0 and (dx < 0 or dy < 0 or dz < 0):
r = QMessageBox.question(None, "Restore attachment point?",
("It looks like your are opening a voxel model exported by Trove.\nShould we"
" try to restore the attachment point out of the .qb's metadata for you?"),
QMessageBox.No, QMessageBox.Yes)
if r == QMessageBox.Yes:
voxels.set(max_width + dx - 1, -dy, max_depth + dz - 1 if coords == 1 else -dz, 0xff00ffff)
f.close()
register_plugin(QubicleFile, "Qubicle Constructor file format IO", "1.0")
|