Houdini 12 Python Scripting with the Houdini Object Model hou

A sequence of 3 floating point values, with associated mathematical operations.

A Vector3 might be used to represent a position in 3D space, or a 3D direction with a length.

See also hou.Vector2 and hou.Vector4.

Methods

__add__Add two vectors, returning a new vector with each component equal to the sum of the corresponding components in the two vectors. This method lets you write v1 + v2, where v1 and v2 are Vector3 objects.
__getitem__Return the float component at the specified index. This method makes vectors behave as sequences (so you can, for example, use a for loop on the elements of a vector, convert a vector to a tuple of floats, etc.) and lets you use square brackets to index into a vector.
__init__Return a new Vector3 from a sequence of floats. If this method is called without parameters, the resulting vector contains the values (0.0, 0.0, 0.0).
__len__Returns 3. This method lets you call len() on a Vector3.
__mul__Multiply this vector with a scalar or with a matrix, returning a new vector. This method lets you write v * s or v * m where v is a vector, s is a float scalar, and m is a hou.Matrix4.
__neg__Return a vector whose components contain the negative values of this vector’s components. This method lets you write -v, where v is a Vector3 object.
__rmul__Multiply this vector with a scalar, returning a new vector. This method lets you write s * v, where v is a vector and s is a float scalar. See also hou.Vector3.__mul__, which lets you write v * s.
__setitem__This method lets you use square brackets to set a value on a vector.
__sub__Subtract a vector from another, returning a new vector with each component equal to the first vector’s corresponding component minus the second vector's. This method lets you write v1 - v2, where v1 and v2 are Vector3 objects.
angleToInterprets this Vector3 and the parameter as directions and returns the angle (in degrees) formed between the two vectors when you place the origins at the same location.
crossReturn the cross product of this vector with another vector. The return value is a vector that is perpendicular to both vectors, pointing in the direction defined by the right-hand rule, with length self.length() * vector3.length() * math.sin(hou.hmath.degToRad(self.angleTo(vector3))).
distanceToInterpret this vector and the argument as 3D positions, and return the distance between them. The return value is equivalent to (self - vector3).length().
dotReturn the dot product between this vector and the one in the parameter. This value is equal to self[0]*vector3[0] + self[1]*vector3[1] + self[2]*vector3[2], which is also equal to self.length() * vector3.length() * math.cos(hou.hmath.degToRad(self.angleTo(vector3)))
isAlmostEqualReturn whether this vector is equal to another, within a tolerance. Verifies that the difference between each component of this vector and the corresponding component of the other vector is within the tolerance.
lengthInterpret this vector as a direction vector and return its length. The result is the same as math.sqrt(self[0]**2 + self[1]**2 + self[2]**2).
lengthSquaredInterpret this vector as a direction vector and return the square of its length. The result is the same as self[0]**2 + self[1]**2 + self[2]**2.
matrixToRotateToReturn a matrix that rotates this vector onto vector3, rotating about the axis perpendicular to the two vectors. If the two vectors have the same direction, return the identity matrix.
normalizedInterpret this vector as a direction and return a vector with the same direction but with a length of 1.
setToSet the contents of this vector to a sequence of floats.
__init__(self, values=(0.0, 0.0, 0.0))

Return a new Vector3 from a sequence of floats. If this method is called without parameters, the resulting vector contains the values (0.0, 0.0, 0.0).

You can also construct a Vector3 from a hou.Vector4. The result contains the first 3 values in the Vector4.

Raises InvalidSize if values is not 3 elements long, or TypeError if values is not a sequence of floats.

__getitem__(self, index) float

Return the float component at the specified index. This method makes vectors behave as sequences (so you can, for example, use a for loop on the elements of a vector, convert a vector to a tuple of floats, etc.) and lets you use square brackets to index into a vector.

>>> v = hou.Vector3((1.0, 2.0, 3.0))
>>> v[-1]
3.0
__setitem__(self, index, value)

This method lets you use square brackets to set a value on a vector.

>>> v = hou.Vector3((1.5, 2.5, 3.5))
>>> v[1] = 0.5
>>> print v
[1.5, 0.5, 3.5]
setTo(self, sequence)

Set the contents of this vector to a sequence of floats.

Raises InvalidSize if values is not 3 elements long, or TypeError if values is not a sequence of floats or ints.

__len__(self) int

Returns 3. This method lets you call len() on a Vector3.

__add__(self, vector3) hou.Vector3

Add two vectors, returning a new vector with each component equal to the sum of the corresponding components in the two vectors. This method lets you write v1 + v2, where v1 and v2 are Vector3 objects.

This method is equivalent to hou.Vector3(self[0] + vector3[0], self[1] + vector3[1], self[2] + vector3[2]).

__sub__(self, vector3) hou.Vector3

Subtract a vector from another, returning a new vector with each component equal to the first vector’s corresponding component minus the second vector's. This method lets you write v1 - v2, where v1 and v2 are Vector3 objects.

This method is equivalent to hou.Vector3(self[0] - vector3[0], self[1] - vector3[1], self[2] - vector3[2]).

__neg__(self) hou.Vector3

Return a vector whose components contain the negative values of this vector’s components. This method lets you write -v, where v is a Vector3 object.

This method is equivalent to hou.Vector3(-self[0], -self[1], -self[2]).

__mul__(self, scalar_or_matrix4) hou.Vector3

Multiply this vector with a scalar or with a matrix, returning a new vector. This method lets you write v * s or v * m where v is a vector, s is a float scalar, and m is a hou.Matrix4.

When the parameter is a float scalar s, this method is equivalent to hou.Vector3(self[0] * s, self[1] * s, self[2] * s).

In order multiply the Vector3 by a Matrix4, the Vector3 is converted to a Vector4 with the fourth component set to 1.0. The effect is that the vector is treated as a position, so if the transformation matrix contains a translation component, the return value will be translated. If you would like to transform a vector (so translations are ignored but rotations, for example, apply), you’ll need to transform a corresponding hou.Vector4 with the fourth component set to zero:

# Build a transformation matrix that rotates 180 degrees about z and then translates by 1 in x.
>>> matrix = hou.hmath.buildRotateAboutAxis((0, 0, 1), 180) * hou.hmath.buildTranslate((1, 0, 0))
>>> position = hou.Vector3(0.0, 1.0, 0.0)

# Rotate the point (0,1,0) to (0,-1,0), then translate to (1,-1,0).
>>> position * matrix
<hou.Vector3 [1, -1, 0]>

# Rotate the vector (0,1,0) to (0,-1,0), ignoring the translation.
>>> vector = hou.Vector4(tuple(position) + (0.0,))
>>> vector
<hou.Vector4 [0, 1, 0, 0]>
>>> vector * matrix
<hou.Vector4 [0, -1, 0, 0]>
>>> hou.Vector3(vector * matrix)
<hou.Vector3 [0, -1, 0]>

# We could have wrapped the above in a function:
>>> def transformAsVector(vector3):
...     return hou.Vector3(hou.Vector4(tuple(vector3) + (0.0,)) * matrix)
>>> transformAsVector(position)
<hou.Vector3 [0, -1, 0]>

# Change the Vector4's last component to 1 to illustrate that it's transformed as a point again.
>>> vector[-1] = 1.0
>>> vector
<hou.Vector4 [0, 1, 0, 1]>
>>> vector * matrix
<hou.Vector4 [1, -1, 0, 1]>

See also hou.Matrix4.

__rmul__(self, scalar) hou.Vector3

Multiply this vector with a scalar, returning a new vector. This method lets you write s * v, where v is a vector and s is a float scalar. See also hou.Vector3.__mul__, which lets you write v * s.

>>> v = hou.Vector3(1, 2, 3)
>>> v * 2
<hou.Vector3 [2, 4, 6]>
>>> 2 * v
<hou.Vector3 [2, 4, 6]>
length(self) float

Interpret this vector as a direction vector and return its length. The result is the same as math.sqrt(self[0]**2 + self[1]**2 + self[2]**2).

lengthSquared(self) float

Interpret this vector as a direction vector and return the square of its length. The result is the same as self[0]**2 + self[1]**2 + self[2]**2.

normalized(self) hou.Vector3

Interpret this vector as a direction and return a vector with the same direction but with a length of 1.

If the vector’s length is 0 (or close to it), the result is the original vector.

For vectors with non-zero lengths, this method is equivalent to self * (1.0/self.length()).

distanceTo(self, vector3) float

Interpret this vector and the argument as 3D positions, and return the distance between them. The return value is equivalent to (self - vector3).length().

dot(self, vector3) float

Return the dot product between this vector and the one in the parameter. This value is equal to self[0]*vector3[0] + self[1]*vector3[1] + self[2]*vector3[2], which is also equal to self.length() * vector3.length() * math.cos(hou.hmath.degToRad(self.angleTo(vector3)))

See Wikipedia’s dot product page.

cross(self, vector3) hou.Vector3

Return the cross product of this vector with another vector. The return value is a vector that is perpendicular to both vectors, pointing in the direction defined by the right-hand rule, with length self.length() * vector3.length() * math.sin(hou.hmath.degToRad(self.angleTo(vector3))).

See Wikipedia’s cross product page.

angleTo(self, vector3) float

Interprets this Vector3 and the parameter as directions and returns the angle (in degrees) formed between the two vectors when you place the origins at the same location.

angularVelocityTo(self, vector3, time_interval)

This feature is not yet implemented

clampLengthAsNew(self, min_length, max_length) hou.Vector3

This feature is not yet implemented

matrixToRotateTo(self, vector3) hou.Matrix4

Return a matrix that rotates this vector onto vector3, rotating about the axis perpendicular to the two vectors. If the two vectors have the same direction, return the identity matrix.

isAlmostEqual(self, vector3, tolerance=0.00001) bool

Return whether this vector is equal to another, within a tolerance. Verifies that the difference between each component of this vector and the corresponding component of the other vector is within the tolerance.