FANDOM


import struct

geo = hou.pwd().geometry()

geo.deletePrims(geo.prims())

voxPath = hou.parmTuple("rawvox").eval()[0]

voxFile = open(voxPath,"rb")

voxFile.seek(0)

## Header

#   Little Endian!  Weeeeee!

#   int Magic 'RVOX'

#   int SizeX

#   int SizeY

#   int SizeZ

#   int bits_per_voxel - 8,16,32, 32 means float values

headerStr = '<4s 1i 1i 1i 1i'

headerSize = struct.calcsize(headerStr)

(voxMagic, voxSizeX, voxSizeY, voxSizeZ, voxBPV) = struct.unpack(headerStr,voxFile.read(headerSize))


if voxMagic != 'XOVR':

    raise hou.Error("Invalid RawVox File " + voxPath)

bbx = ((voxSizeX*0.01)/2)

bby = ((voxSizeY*0.01)/2)

bbz = ((voxSizeZ*0.01)/2)

bb = hou.BoundingBox(bbx*-1,bby*-1,bbz*-1,bbx,bby,bbz)

vol = geo.createVolume(voxSizeX, voxSizeY, voxSizeZ,bb)

format = 'f'

if voxBPV == 8:

    format = 'B' # Unsigned Int

elif voxBPV == 16:

    format = 'H' # Unsigned Short

elif voxBPV == 32:

    format = 'f'

else:

    raise hou.Error("Invalid Bits per Voxel")

numVoxels = voxSizeX * voxSizeY * voxSizeZ

voxelDataStr = '<' + str(numVoxels) + format

voxelDataSize = struct.calcsize(voxelDataStr)

voxels = struct.unpack(voxelDataStr,voxFile.read(voxelDataSize))

# Convert to Floats

voxList = []

if format == 'B':

    for voxel in voxels:

        voxList.append(voxel/float(2**8));

    voxels = voxList

elif format == 'H':

    for voxel in voxels:

        voxList.append(voxel/float(2**16));

    voxels = voxList

vol.setAllVoxels(voxels)

voxFile.close()