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 ¶
__init__(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__(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__(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(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__()
→ int
Returns 3. This method lets you call len() on a Vector3.
__add__(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__(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__()
→ 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__(scalar_or_matrix3_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.Matrix3 or 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 to 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__(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]>
__div__(scalar)
→ hou.Vector3
Divide a vector by a float scalar, returning a new vector. This method
lets you write v / s
where v
is a vector and s
is a float.
This method is equivalent to
hou.Vector3(self[0] / scalar, self[1] / scalar, self[2] / scalar)
.
length()
→ 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()
→ 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()
→ 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())
.
multiplyAsDir(matrix4)
→ hou.Vector3
Interpret this vector as a direction and returns a transformed direction that has been rotated and scaled (not translated) by the matrix4
distanceTo(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(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)))
cross(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)))
.
angleTo(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.
matrixToRotateTo(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.
almostEqual(vector3, tolerance=0.00001)
→ bool
Deprecated. Use Vector3.isAlmostEqual instead.
isAlmostEqual(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.
smoothRotation(reference, rotate_order="xyz")
→ hou.Vector3
Returns the Euler rotations (in degrees) that have the closest values to
reference
while still describing the same orientation as this vector.
reference
A hou.Vector3 of Euler angles, in degrees. Typically, this will be the rotations from the previous sample or frame.
rotate_order
A string containing a permutation of the letters x
, y
, and z
that determines the order in which rotations are performed about
the coordinate axes.
ocio_transform(src_space, dest_space)
→ hou.Vector3
Use Open Color IO to transform the color from the source space to the destination space.
x()
→ float
Return the first component of the vector. Equivalent to v/hom/hou/0.html.
y()
→ float
Return the second component of the vector. Equivalent to v/hom/hou/1.html.
z()
→ float
Return the third component of the vector. Equivalent to v/hom/hou/2.html.
distanceToSegment(point1, point2)
→ float
Compute the distance of this vector to the line segment defined by point1 to point2.
`point1'
The first hou.Vector3 of the line segment.
`point2'
The second hou.Vector3 of the line segment.
distance2ToSegment(point1, point2)
→ float
Compute the distance squared of this vector to the line segment defined by point1 to point2.
`point1'
The first hou.Vector3 of the line segment.
`point2'
The second hou.Vector3 of the line segment.
pointOnSegment(point1, point2)
→ hou.Vector3
Project the underlying vector onto the line segment defined by point1 to point2. The returned point will lie on the line segment.
`point1'
The first hou.Vector3 of the line segment.
`point2'
The second hou.Vector3 of the line segment.