Skip to content

hou.Vector2/3 are not compatible with collections.abc.Sequence #25

@chadrik

Description

@chadrik

In order to implement the Sequence protocol, a class must implement the following methods, in addition to those impemented by Collection and Reversible:

class Sequence(Collection[_T_co], Reversible[_T_co], Generic[_T_co]):
    @overload
    @abstractmethod
    def __getitem__(self, index: int) -> _T_co: ...
    @overload
    @abstractmethod
    def __getitem__(self, index: slice) -> Sequence[_T_co]: ...
    # Mixin methods
    def index(self, value: Any, start: int = 0, stop: int = ...) -> int: ...
    def count(self, value: Any) -> int: ...
    def __contains__(self, value: object) -> bool: ...
    def __iter__(self) -> Iterator[_T_co]: ...
    def __reversed__(self) -> Iterator[_T_co]: ...

Notably hou.Vector2/3 do not implement index or count:

class Vector2:
    """

    hou.Vector2

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

    A Vector2 might be used to represent a position in 2D space, a 2D
    direction and length, or the size of a rectangle. For example,
    hou.Node.position returns a position and hou.Node.size returns the size
    of a rectangle.

    See also hou.Vector3 and hou.Vector4.


    """
    thisown: Incomplete
    def __init__(self, x: Sequence[float]|float = ..., y: float = ...) -> None:
        """

        __init__(self, values=(0.0, 0.0))

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

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


        """
    __swig_destroy__: Incomplete
    def __eq__(self, other: object) -> bool: ...
    def __ne__(self, other: object) -> bool: ...
    def isAlmostEqual(self, vector2: Vector2, tolerance: float = 1e-05) -> bool:
        """

        isAlmostEqual(self, vector2, 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.


        """
    def almostEqual(self, vector2: Vector2, tolerance: float = 1e-05) -> bool:
        """

        almostEqual(self, vector2, tolerance=0.00001) -> bool

            Deprecated. Use Vector2.isAlmostEqual instead.


        """
    def __hash__(self) -> int: ...
    def __getitem__(self, index: int) -> float:
        """

        __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.Vector2((1.0, 2.0))
          > >>> v[-1]
          > 2.0

        """
    def __setitem__(self, index: int, value: float) -> None:
        """

        __setitem__(self, index, value)

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

          > >>> v = hou.Vector2((1.5, 2.5))
          > >>> v[0] = 0.5
          > >>> print v
          > [0.5, 2.5]

        """
    def __len__(self) -> int:
        """

        __len__(self) -> int

            Returns 2. This method lets you call len() on a Vector2.


        """
    def setTo(self, sequence: Sequence[float]) -> None:
        """

        setTo(self, sequence)

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

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


        """
    def __add__(self, vector2: Vector2) -> Vector2:
        """

        __add__(self, vector2) -> hou.Vector2

            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 Vector2 objects.

            This method is equivalent to hou.Vector2(self[0] + vector2[0],
            self[1] + vector2[1]).


        """
    def __sub__(self, vector2: Vector2) -> Vector2:
        """

        __sub__(self, vector2) -> hou.Vector2

            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 Vector2 objects.

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


        """
    def __neg__(self) -> Vector2:
        """

        __neg__(self) -> hou.Vector2

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

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


        """
    def __rmul__(self, scalar: float) -> Vector2:
        """

        __rmul__(self, scalar) -> hou.Vector2

            Multiply a vector with a float scalar, returning a new vector. This
            method lets you write s * v where v is a vector and s is a float.

            This method is equivalent to hou.Vector2(self[0] * scalar, self[1] *
            scalar).


        """
    def __mul__(self, scalar_or_matrix2: float|Matrix2) -> Vector2:
        """

        __mul__(self, scalar_or_matrix2) -> hou.Vector2

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

            This method is equivalent to hou.Vector2(self[0] * scalar, self[1] *
            scalar).


        """
    def __div__(self, scalar: float) -> Vector2:
        """

        __div__(self, scalar) -> hou.Vector2

            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.Vector2(self[0] / scalar, self[1] /
            scalar).


        """
    def __truediv__(self, scalar: float) -> Vector2: ...
    def normalized(self) -> Vector2:
        """

        normalized(self) -> hou.Vector2

            Interpreting this vector as a direction, 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 vector's with non-zero lengths, this method is equivalent to
            self * (1.0/self.length()).


        """
    def length(self) -> float:
        """

        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).


        """
    def lengthSquared(self) -> float:
        """

        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.


        """
    def distanceTo(self, vector2: Vector2) -> float:
        """

        distanceTo(self, vector2) -> float

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


        """
    def dot(self, vector2: Vector2) -> float:
        """

        dot(self, vector2) -> float

            Return the dot product between this vector and the one in the
            parameter.

            See Wikipedia's dot product page.


        """
    def x(self) -> float:
        """

        x(self) -> float

            Return the first component of the vector. Equivalent to v.


        """
    def y(self) -> float:
        """

        y(self) -> float

            Return the second component of the vector. Equivalent to v.


        """
    def __contains__(self, other: float) -> bool: ...
    def __iter__(self) -> Iterator[float]: ...
    def __reversed__(self) -> Iterator[float]: ...

We should do some tests against some hou functions that accept vectors to determine the minimum set of methods required, and make our own protocol with those methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions