c4d.Vector

class c4d.Vector
Single precision vector mathematics.

A vector class represents a point, a color, a direction, an HPB-angle or a simple coordinate in a 3D space using the Cartesian coordinates x, y and z. The values are stored in the members x, y and z.

For example, positions are stored in the order (X position; Y position; Z position) and rotations in the order (H angle; P angle; B angle).

Cinema 4D also uses vectors to store colors and their values of an RGB (Red, Green, Blue) color model.
The vector class can also represent a direction, an arrow pointing from the origin of the coordinates, such as (0,0,0), to an endpoint3.

Note

Cinema 4D uses a left-handed coordinate system.

Warning

The Vector type does not support the function copy.copy(), please use the Vector copy constructor instead.

>>> import c4d
>>> import copy

>>> # A vector and its Python object id.
>>> vec = c4d.Vector(1)
>>> id(vec), vec
(1331515356992, Vector(1, 1, 1))

>>> # We are only creating a reference to the same object.
>>> ref = vec
>>> id(ref), ref
(1331515356992, Vector(1, 1, 1))

>>> # Cinema's types usually do not work with Python's copy module.
>>> copy.deepcopy(vec)
...
TypeError: can't pickle c4d.Vector objects.

>>> # But we can copy objects via copy constructors.
>>> copy = c4d.Vector(vec)
>>> id(copy), copy
(1331515387904, Vector(1, 1, 1))

Attributes

Vector.x

X component of the vector.

Type: float

Vector.y

Y component of the vector.

Type: float

Vector.z

Z component of the vector.

Type: float

Methods Signatures

Vector.__init__(self[, x, y, z])

Initializes a new Vector.

Vector.__str__(self)

Returns a string representation of the vector.

Vector.__getitem__(self, key)

Retrieves the value of the x, y or z component of the vector.

Vector.__setitem__(self, key, value)

Assigns a value to the x, y or z component of the vector.

Vector.__del__(self)

Not implemented.

Vector.__add__(self, other)

Adds an operand to the vector.

Vector.__radd__(self, other)

Adds the vector to an operand.

Vector.__sub__(self, other)

Subtracts an operand from the vector.

Vector.__rsub__(self, other)

Subtracts the vector from an operand.

Vector.__mul__(self, other)

Calculates the product of the vector and an left-side operand.

Vector.__rmul__(self, other)

Calculates the product of the vector and a right-side operand.

Vector.__div__(self, other)

Divides each vector component by a scalar.

Vector.__rdiv__(self, other)

Not implemented.

Vector.__mod__(self, other)

Calculates the cross product of the vector and an operand.

Vector.__xor__(self, other)

Calculates an alternative product operation of the vector and a left-side operand.

Vector.__rxor__(self, other)

Not implemented.

Vector.__abs__(self)

Returns a vector with the absolute (positive) value of each component.

Vector.__neg__(self)

Returns the inverse of the vector.

Vector.__invert__(self)

Returns the normalized vector.

Vector.__eq__(self, other)

Checks if two vectors are equal.

Vector.__ne__(self, other)

Check if two vectors are not equal.

Vector.__ge__(self, other)

Not implemented.

Vector.__gt__(self, other)

Not implemented.

Vector.__le__(self, other)

Not implemented.

Vector.__lt__(self, other)

Not implemented.

Vector.Dot(self, v2)

Calculates the dot product of the vector and vector v2.

Vector.Cross(self, v2)

Calculates the cross product of the vector and vector v2.

Vector.GetLength(self)

Calculates the length of the vector.

Vector.GetLengthSquared(self)

Calculates the squared length of the vector.

Vector.GetNormalized(self)

Calculates the normalized vector, so that GetLength() returns 1.0.

Vector.Normalize(self)

Normalizes the vector, so that GetLength() returns 1.0.

Static Methods Signatures

c4d.Vector.GetDistance(v1, v2)

Retrieve the distance between two vectors.

Methods Documentation

Vector.__init__(self, x=0.0, y=0.0, z=0.0)
Initializes a new Vector.

All arguments are optional so it is possible to create a new vector without any arguments. All components are simply 0.
Otherwise x can be a Vector so all components of the passed vector will be copied to the new vector.
If only a number is passed, all components will be set to this.
>>> c4d.Vector()
Vector(0,0,0)

>>> c4d.Vector(100)
Vector(100,100,100)

>>> v = c4d.Vector(100, 100, 100)
>>> c4d.Vector(v)
Vector(100,100,100)

>>> c4d.Vector(1, 2, 3)
Vector(1,2,3)
Parameters
  • x (Union[int, float, c4d.Vector]) – If x is a number and is the only passed argument, set this to all components. If x is a vector, clone it. Otherwise set the X component.

  • y (number) – Set the Y component.

  • z (number) – Set the Z component.

Return type

c4d.Vector

Returns

A new vector.

Vector.__str__(self)
Returns a string representation of the vector.

Called if str() is invoked on a vector object. See object.__str__() for more information on Python’s data model.
>>> c4d.Vector(30, 40, 2)
Vector(30.0, 40.0, 2.0)
Return type

str

Returns

The Vector as string.

Vector.__getitem__(self, key)
Retrieves the value of the x, y or z component of the vector.

The component is identified by an index in the interval [0, 2].

New in version R14.014.

>>> v = c4d.Vector(1, 2, 3)
>>> v[0]
1.0
>>> v[1]
2.0
>>> v[2]
3.0
Parameters

key (int) – The component index.

Raises

IndexError – When key does not satisfy 0 <= key < 2.

Return type

float

Returns

The vector component.

Vector.__setitem__(self, key, value)
Assigns a value to the x, y or z component of the vector.

The component is identified by an index in the interval [0, 2].

New in version R14.014.

>>> v = c4d.Vector(1, 2, 3)
>>> v[0] = 0
>>> v
Vector(0, 2, 3)
Parameters
  • key (int) – The component index.

  • value (Union[float, int]) – The new vector component.

Raises

IndexError – When key does not satisfy 0 <= key < 2.

Vector.__del__(self)

Not implemented.

Note

The Vector does not support item deletion, e.g., by calling del(v[0]).

Raise

TypeError.

Vector.__add__(self, other)

Adds an operand to the vector.

>>> c4d.Vector(1, 2, 3) + 1
Vector(2,3,4)

>>> c4d.Vector(1, 2, 3) + c4d.Vector(2, 3, 4)
Vector(3,5,7)
Parameters

other (Union[c4d.Vector, int, float]) – The other operand.

Return type

c4d.Vector

Returns

The resulting vector.

Vector.__radd__(self, other)

Adds the vector to an operand.

>>> 1 + c4d.Vector(1, 2, 3)
Vector(2,3,4)

>>> c4d.Vector(1, 2, 3) + c4d.Vector(2, 3, 4)
Vector(3,5,7)
Parameters

other (Union[c4d.Vector, int, float]) – The other operand.

Return type

c4d.Vector

Returns

The resulting vector.

Vector.__sub__(self, other)

Subtracts an operand from the vector.

>>> c4d.Vector(1, 2, 3) - 1
Vector(0,1,2)

c4d.Vector(1, 2, 3) - c4d.Vector(2, 3, 4)
Vector(-1,-1,-1)
Parameters

other (Union[c4d.Vector, int, float]) – The other operand.

Return type

c4d.Vector

Returns

The resulting vector.

Vector.__rsub__(self, other)

Subtracts the vector from an operand.

>>> 1 - c4d.Vector(1, 2, 3)
Vector(0,-1,-2)

>>> c4d.Vector(1, 2, 3) - c4d.Vector(2, 3, 4)
Vector(-1,-1,-1)
Parameters

other (Union[c4d.Vector, int, float]) – The other operand.

Return type

c4d.Vector

Returns

The resulting vector.

Vector.__mul__(self, other)
Calculates the product of the vector and an left-side operand.

If other is a float, it multiplies each vector component by the scalar.
If other is a vector, calculates the dot product of the vectors.
If other is a matrix, the vector is transformed by it, including translations.

See also

Vector.__xor__() is the operator implementation for alternative product operations.

>>> u = c4d.Vector(1, 0, 0)
>>> v = c4d.Vector(2, 0, 0)

>>> # A transform rotating by 90° ccw and translating by 100 units on v3 (i.e., the 
>>> # z-axis, the basis vector k).
>>> M = c4d.Matrix(v1=c4d.Vector( 0, 1, 0),
                   v2=c4d.Vector(-1, 0, 0),
                   v3=c4d.Vector( 0, 0, 1),
                   off=c4d.Vector(0, 0, 100))

>>> # Calculates the dot product of u and v.
>>> u * v
2.0

>>> # Scales u by the factor of 5.
>>> u * 5.
Vector(5, 0, 0)

>>> # Transforms u by M, including translations.
>>> u * M
Vector(0, 1, 100)
Parameters

other (Union[int, float, c4d.Vector, c4d.Matrix]) – The other operand.

Return type

c4d.Vector or float

Returns

A float, if other is of type Vector. | A vector, if other is of type float or Matrix.

Vector.__rmul__(self, other)
Calculates the product of the vector and a right-side operand.

If other is a float, it multiplies each vector component by the scalar.
If other is a matrix, the vector is transformed by it, including translations.
>>> u = c4d.Vector(1, 0, 0)
>>> v = c4d.Vector(2, 0, 0)

>>> # A transform rotating by 90° ccw and translating by 100 units on v3 (i.e., the 
>>> # z-axis, the basis vector k).
>>> M = c4d.Matrix(v1=c4d.Vector( 0, 1, 0),
                   v2=c4d.Vector(-1, 0, 0),
                   v3=c4d.Vector( 0, 0, 1),
                   off=c4d.Vector(0, 0, 100))

>>> # Calculates the dot product of u and v.
>>> u * v
2.0

>>> # Scales u by the factor of 5.
>>> 5. * u
Vector(5, 0, 0)

>>> # Transforms u by M, including translations.
>>> M * u
Vector(0, 1, 100)
Parameters

other (Union[int, float, c4d.Vector, c4d.Matrix]) – The other operand.

Return type

c4d.Vector or float

Returns

A float, if other is of type Vector. | A vector, if other is of type float or Matrix.

Vector.__div__(self, other)

Divides each vector component by a scalar.

>>> c4d.Vector(1, 2, 3) / 5
Vector(0.2, 0.4, 0.6)
Parameters

other (Union[int, float]) – The other operand.

Return type

c4d.Vector

Returns

The resulting vector.

Vector.__rdiv__(self, other)

Not implemented.

Raise

TypeError.

Vector.__mod__(self, other)

Calculates the cross product of the vector and an operand.

>>> c4d.Vector(1, 0, 0) % c4d.Vector(0, 1, 0)
Vector(0, 0, 1)
Parameters

other (c4d.Vector) – The other operand.

Return type

c4d.Vector

Returns

The resulting vector.

Vector.__xor__(self, other)
Calculates an alternative product operation of the vector and a left-side operand.

If other is a vector, calculates the component-wise product of the vectors.
If other is a matrix, the vector is transformed by it, excluding translations.

See also

Vector.__mul__() is the operator implementation for the primary product operations.

>>> u = c4d.Vector(1, 0, 0)
>>> v = c4d.Vector(2, 0, 0)

>>> # A transform rotating by 90° ccw and translating by 100 units on v3 (i.e., the 
>>> # z-axis, the basis vector k).
>>> M = c4d.Matrix(v1=c4d.Vector( 0, 1, 0),
                   v2=c4d.Vector(-1, 0, 0),
                   v3=c4d.Vector( 0, 0, 1),
                   off=c4d.Vector(0, 0, 100))

>>> # Computes the component-wise product of u and v.
>>> u ^ v
Vector(3, 4, 3)

>>> # Transforms u by M, excluding translations. 
>>> u ^ M
Vector(2, 1, 3)
Parameters

other (Union[c4d.Vector, c4d.Matrix]) – The other operand.

Return type

c4d.Vector or float

Returns

A float, if other is of type Vector. | A vector, if other is of type float or Matrix.

Vector.__rxor__(self, other)

Not implemented.

Raise

TypeError.

Vector.__abs__(self)

Returns a vector with the absolute (positive) value of each component.

>>> abs(c4d.Vector(-1, -2, -3))
Vector(1,2,3)
Return type

c4d.Vector

Returns

The absolute vector.

Vector.__neg__(self)

Returns the inverse of the vector.

>>> -c4d.Vector(1, 2, 3)
Vector(-1,-2,-3)
Return type

c4d.Vector

Returns

The inverse vector.

Vector.__invert__(self)

Returns the normalized vector.

>>> ~c4d.Vector(1, 2, 3)
Vector(0.267,0.535,0.802)
Return type

c4d.Vector

Returns

The normalized vector.

Vector.__eq__(self, other)

Checks if two vectors are equal.

Parameters

other (c4d.Vector) – The other vector.

Return type

bool

Returns

True if both vectors are equal.

Vector.__ne__(self, other)

Check if two vectors are not equal.

Parameters

other (c4d.Vector) – The other vector.

Return type

bool

Returns

True if both vectors are not equal.

Vector.__ge__(self, other)

Not implemented.

Raise

TypeError.

Vector.__gt__(self, other)

Not implemented.

Raise

TypeError.

Vector.__le__(self, other)

Not implemented.

Raise

TypeError.

Vector.__lt__(self, other)

Not implemented.

Raise

TypeError.

Vector.Dot(self, v2)

Calculates the dot product of the vector and vector v2.

Parameters

v2 (c4d.Vector) – The second vector.

Return type

float

Returns

The dot product.

Vector.Cross(self, v2)

Calculates the cross product of the vector and vector v2.

Parameters

v2 (c4d.Vector) – The other vector.

Return type

c4d.Vector

Returns

The result.

Vector.GetLength(self)

Calculates the length of the vector.

Return type

float

Returns

The length.

Vector.GetLengthSquared(self)

Calculates the squared length of the vector.

Return type

float

Returns

The squared length.

Vector.GetNormalized(self)

Calculates the normalized vector, so that GetLength() returns 1.0.

Return type

c4d.Vector

Returns

The normalized vector.

Vector.Normalize(self)

Normalizes the vector, so that GetLength() returns 1.0.

Static Methods Documentation

static c4d.Vector.GetDistance(v1, v2)

Retrieve the distance between two vectors.

Parameters
Returns

the distance from v1 to v2.

Return type

float