hou.Volume class

A Volume is a kind geometry primitive (Prim object) storing a three dimensional array of voxels.

Inheritence: hou.Prim >

Methods

resolution(self)hou.Vector3

Return the x, y, and z dimensions of the volume. For example, a resolution of (10, 20, 30) means the volume is 10 voxels in x by 20 voxels in y by 30 voxels in z.

sample(self, position)float

Given a sequence of three floats containing a 3D position, return the value of the volume at that position. If the position is not in the middle of a voxel, Houdini will interpolate using values from surrounding voxels.

See also hou.Volume.voxel and hou.Volume.posToIndex.

gradient(self, position)hou.Vector3

Given a sequence of three floats containing a 3D position, return a vector which points in the direction of the greatest rate of increase of the volume’s value.

See Wikipedia’s gradient page for more information.

voxel(self, index)float

Given a sequence of three integers containing a voxel index, return the value of the corresponding voxel.

>>> volume_sop = hou.node("/obj").createNode("geo").createNode("volume")
>>> volume_sop.parm("initialval1").set(0.3)
>>> volume = volume_sop.geometry().prims()[0]
>>> volume.resolution()
(10, 10, 10)
>>> volume.voxel((0, 0, 0))
0.3
setVoxel(self, index, value)

Set the value of a voxel. You would typically call this method from the code of a Python-defined SOP.

index

A sequence of three integers containing a voxel index. Raises hou.OperationFailed if any of the values in index are out of range.

value

A float containing the voxel’s new value.

Raises hou.GeometryPermissionError if this geometry is not modifiable.

allVoxels(self) → tuple of float

Return a tuple of floats containing the values of all voxels. It is faster to call this method to retrieve all the voxels than it is to loop through the voxel array in Python.

You can, for example, use Python’s Numpy library to perform operations on the voxel data and then store the result back into the volume from a Python SOP using hou.Volume.setAllVoxels. Note that Numpy allows you to reshape the flat tuple of floats to behave like a 3D matrix of floats.

This method can be approximately implemented as follows (though this Python implementation is much slower):

def allVoxels(self):
    result = []
    xres, yres, zres = self.resolution()
    for z in range(zres):
        for y in range(yres):
            for x in range(xres):
                result.append(self.voxel((x, y, z)))
    return tuple(result)

See also hou.Volume.allVoxelsAsString, hou.Geometry.pointFloatAttribValues, and hou.Geometry.primFloatAttribValues.

allVoxelsAsString(self) → str

Return a string representation of the floats containing all the values of all voxels. This method is faster than hou.Volume.allVoxels, and you can use the array module to convert the string into a Python sequence.

This method provides a faster implementation of the following:

import array
def allVoxelsAsString(self):
    return array.array("f", self.allVoxels()).tostring()

You can convert the return value from this method to an array using the following method:

import array
def allVoxelsAsArray(volume):
    a = array.array("f")
    a.fromstring(volume.allVoxelsAsString())
    return a

See hou.Volume.allVoxels for more information.

setAllVoxels(self, values)

Set the value of all voxels in this volume. You would typically call this method from the code of a Python-defined SOP.

Raises hou.OperationFailed if the sequence of values is not exactly the same as self.resolution()[0] * self.resolution()[1] * self.resolution()[2].

Raises hou.GeometryPermissionError if this geometry is not modifiable.

See also hou.Volume.allVoxels.

setAllVoxelsFromString(self, values)

Set the value of all voxels in this volume from a string representation of a sequence of single precision float values. This method is faster than hou.Volume.setAllVoxels.

Raises hou.OperationFailed if the length of the string is not exactly the same as self.resolution()[0] * self.resolution()[1] * self.resolution()[2] * 4.

See hou.Volume.setAllVoxels and hou.Volume.allVoxelsAsString for more information.

The following example function accepts an array.array("f") and sets the voxels to its contents:

def setAllVoxelsFromArray(volume, arr):
    assert(arr.typecode == "f")
    volume.setAllVoxelsFromString(arr.tostring())

posToIndex(self, position)tuple of int

Given a sequence of three floats containing a 3D position, return a tuple of three ints containing the corresponding index into the voxel array.

Note that the returned index will be invalid if the position is outside the volume. Use hou.Volume.isValidIndex to determine if the index is valid.

indexToPos(self, index)hou.Vector3

Given a sequence of three ints containing an index into the voxel array, return the corresponding 3D position of the middle of that voxel.

isValidIndex(self, index)bool

Return whether or sequence of three ints containing an index into the voxel array is valid.

This method can approximately be implemented as follows:

def isValidIndex(self, index):
    for i, maximum in zip(index, self.resolution()):
        if i < 0 or i >= maximum:
            return False
    return True

volumeAverage(self)float

Return the average value of all voxels.

volumeMin(self)float

Return the minimum value of all voxels.

volumeMax(self)float

Return the maximum value of all voxels.