Skip to content

Link

This module contains dataclasses for creating a link in a URDF robot model.

Dataclass
  • Origin: Represents the origin of a link in the robot model.
  • Axis: Represents the axis of a link in the robot model.
  • Inertia: Represents the inertia properties of a link in the robot model.
  • Material: Represents the material properties of a link in the robot model.
  • InertialLink: Represents the inertial properties of a link in the robot model.
  • VisualLink: Represents the visual properties of a link in the robot model.
  • CollisionLink: Represents the collision properties of a link in the robot model.
  • Link: Represents a link in the robot model.
Enum
  • Colors: Enumerates the possible colors for a link in the robot model.

Axis dataclass

Represents the axis of rotation or translation for a joint in the robot model.

Attributes:

Name Type Description
xyz tuple[float, float, float]

The direction vector of the axis. Should be a unit vector (normalized).

Methods:

Name Description
to_xml

Converts the axis to an XML element.

to_mjcf

Converts the axis to a MuJoCo compatible XML element.

Class Methods

from_xml: Creates an axis from an XML element.

Examples:

>>> axis = Axis(xyz=(1.0, 0.0, 0.0))  # X-axis rotation
>>> axis.to_xml()
<Element 'axis' at 0x...>
>>> xml_str = '<axis xyz="0 1 0"/>'
>>> xml_element = ET.fromstring(xml_str)
>>> axis = Axis.from_xml(xml_element)
>>> axis.xyz
(0.0, 1.0, 0.0)
Source code in onshape_robotics_toolkit\models\link.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
@dataclass
class Axis:
    """
    Represents the axis of rotation or translation for a joint in the robot model.

    Attributes:
        xyz (tuple[float, float, float]): The direction vector of the axis.
            Should be a unit vector (normalized).

    Methods:
        to_xml: Converts the axis to an XML element.
        to_mjcf: Converts the axis to a MuJoCo compatible XML element.

    Class Methods:
        from_xml: Creates an axis from an XML element.

    Examples:
        >>> axis = Axis(xyz=(1.0, 0.0, 0.0))  # X-axis rotation
        >>> axis.to_xml()
        <Element 'axis' at 0x...>

        >>> xml_str = '<axis xyz="0 1 0"/>'
        >>> xml_element = ET.fromstring(xml_str)
        >>> axis = Axis.from_xml(xml_element)
        >>> axis.xyz
        (0.0, 1.0, 0.0)
    """

    xyz: tuple[float, float, float]

    def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the axis to an XML element.

        Args:
            root: The root element to append the axis to.

        Returns:
            The XML element representing the axis.

        Examples:
            >>> axis = Axis(xyz=(1.0, 0.0, 0.0))
            >>> axis.to_xml()
            <Element 'axis' at 0x7f8b3c0b4c70>
        """

        axis = ET.Element("axis") if root is None else ET.SubElement(root, "axis")
        axis.set("xyz", " ".join(format_number(v) for v in self.xyz))
        return axis

    def to_mjcf(self, root: ET.Element) -> None:
        """
        Convert the axis to an MuJoCo compatible XML element.

        Args:
            root: The root element to append the axis to.

        Returns:
            The XML element representing the axis.

        Examples:
            >>> axis = Axis(xyz=(1.0, 0.0, 0.0))
            >>> axis.to_mjcf()
            <Element 'axis' at 0x7f8b3c0b4c70>
        """
        root.set("axis", " ".join(format_number(v) for v in self.xyz))

    @classmethod
    def from_xml(cls, xml: ET.Element) -> "Axis":
        """
        Create an axis from an XML element.

        Args:
            xml: The XML element to create the axis from.

        Returns:
            The axis created from the XML element.

        Examples:
            >>> xml = ET.Element('axis')
            >>> Axis.from_xml(xml)
            Axis(xyz=(0.0, 0.0, 0.0))
        """
        xyz = tuple(map(float, xml.get("xyz").split()))
        return cls(xyz)

from_xml(xml) classmethod

Create an axis from an XML element.

Parameters:

Name Type Description Default
xml Element

The XML element to create the axis from.

required

Returns:

Type Description
Axis

The axis created from the XML element.

Examples:

>>> xml = ET.Element('axis')
>>> Axis.from_xml(xml)
Axis(xyz=(0.0, 0.0, 0.0))
Source code in onshape_robotics_toolkit\models\link.py
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
@classmethod
def from_xml(cls, xml: ET.Element) -> "Axis":
    """
    Create an axis from an XML element.

    Args:
        xml: The XML element to create the axis from.

    Returns:
        The axis created from the XML element.

    Examples:
        >>> xml = ET.Element('axis')
        >>> Axis.from_xml(xml)
        Axis(xyz=(0.0, 0.0, 0.0))
    """
    xyz = tuple(map(float, xml.get("xyz").split()))
    return cls(xyz)

to_mjcf(root)

Convert the axis to an MuJoCo compatible XML element.

Parameters:

Name Type Description Default
root Element

The root element to append the axis to.

required

Returns:

Type Description
None

The XML element representing the axis.

Examples:

>>> axis = Axis(xyz=(1.0, 0.0, 0.0))
>>> axis.to_mjcf()
<Element 'axis' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
def to_mjcf(self, root: ET.Element) -> None:
    """
    Convert the axis to an MuJoCo compatible XML element.

    Args:
        root: The root element to append the axis to.

    Returns:
        The XML element representing the axis.

    Examples:
        >>> axis = Axis(xyz=(1.0, 0.0, 0.0))
        >>> axis.to_mjcf()
        <Element 'axis' at 0x7f8b3c0b4c70>
    """
    root.set("axis", " ".join(format_number(v) for v in self.xyz))

to_xml(root=None)

Convert the axis to an XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the axis to.

None

Returns:

Type Description
Element

The XML element representing the axis.

Examples:

>>> axis = Axis(xyz=(1.0, 0.0, 0.0))
>>> axis.to_xml()
<Element 'axis' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the axis to an XML element.

    Args:
        root: The root element to append the axis to.

    Returns:
        The XML element representing the axis.

    Examples:
        >>> axis = Axis(xyz=(1.0, 0.0, 0.0))
        >>> axis.to_xml()
        <Element 'axis' at 0x7f8b3c0b4c70>
    """

    axis = ET.Element("axis") if root is None else ET.SubElement(root, "axis")
    axis.set("xyz", " ".join(format_number(v) for v in self.xyz))
    return axis

Represents the collision properties of a link in the robot model.

This class defines the geometry used for collision detection in physics simulations, which may be different from the visual geometry.

Attributes:

Name Type Description
name Union[str, None]

Optional name identifier for the collision element.

origin Origin

The position and orientation of the collision geometry.

geometry BaseGeometry

The shape used for collision detection.

friction Optional[tuple[float, float, float]]

Optional friction coefficients (static, dynamic, rolling).

Methods:

Name Description
to_xml

Converts the collision properties to an XML element.

to_mjcf

Converts the collision properties to a MuJoCo compatible XML element.

transform

Applies a transformation matrix to the collision geometry's origin.

Class Methods

from_xml: Creates a CollisionLink from an XML element.

Examples:

>>> collision = CollisionLink(
...     name="link_collision",
...     origin=Origin.zero_origin(),
...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0))
... )
>>> collision.to_xml()
<Element 'collision' at 0x...>
Source code in onshape_robotics_toolkit\models\link.py
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
@dataclass
class CollisionLink:
    """
    Represents the collision properties of a link in the robot model.

    This class defines the geometry used for collision detection in physics
    simulations, which may be different from the visual geometry.

    Attributes:
        name (Union[str, None]): Optional name identifier for the collision element.
        origin (Origin): The position and orientation of the collision geometry.
        geometry (BaseGeometry): The shape used for collision detection.
        friction (Optional[tuple[float, float, float]]): Optional friction coefficients
            (static, dynamic, rolling).

    Methods:
        to_xml: Converts the collision properties to an XML element.
        to_mjcf: Converts the collision properties to a MuJoCo compatible XML element.
        transform: Applies a transformation matrix to the collision geometry's origin.

    Class Methods:
        from_xml: Creates a CollisionLink from an XML element.

    Examples:
        >>> collision = CollisionLink(
        ...     name="link_collision",
        ...     origin=Origin.zero_origin(),
        ...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0))
        ... )
        >>> collision.to_xml()
        <Element 'collision' at 0x...>
    """

    name: Union[str, None]
    origin: Origin
    geometry: BaseGeometry

    friction: Optional[tuple[float, float, float]] = None

    def transform(self, transformation_matrix: np.ndarray) -> None:
        """
        Apply a transformation to the visual link's origin.

        Args:
            transformation_matrix (np.ndarray): A 4x4 transformation matrix (homogeneous).
        """
        # Apply translation and rotation to the origin position
        pos = np.array([self.origin.xyz[0], self.origin.xyz[1], self.origin.xyz[2], 1])
        new_pos = transformation_matrix @ pos
        self.origin.xyz = tuple(new_pos[:3])  # Update position

        # Extract the rotation from the transformation matrix
        rotation_matrix = transformation_matrix[:3, :3]
        current_rotation = R.from_euler("xyz", self.origin.rpy)
        new_rotation = R.from_matrix(rotation_matrix @ current_rotation.as_matrix())
        self.origin.rpy = new_rotation.as_euler("xyz").tolist()

    def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the collision properties to an XML element.

        Args:
            root: The root element to append the collision properties to.

        Returns:
            The XML element representing the collision properties.

        Examples:
            >>> collision = CollisionLink(origin=Origin(...), geometry=BoxGeometry(...))
            >>> collision.to_xml()
            <Element 'collision' at 0x7f8b3c0b4c70>
        """
        collision = ET.Element("collision") if root is None else ET.SubElement(root, "collision")
        if self.name:
            collision.set("name", self.name)
        self.origin.to_xml(collision)
        self.geometry.to_xml(collision)
        return collision

    def to_mjcf(self, root: ET.Element) -> None:
        """
        Convert the collision properties to an MuJoCo compatible XML element.

        Example XML:
        ```xml
              <geom name="Assembly-2-1-SUB-Part-5-1-collision"
                    pos="0.0994445 -0.000366963 0.0171076"
                    quat="-0.92388 -4.28774e-08 0.382683 0"
                    type="mesh"
                    rgba="1 0.5 0 1"
                    mesh="Assembly-2-1-SUB-Part-5-1"
                    contype="1"
                    conaffinity="0"
                    density="0"
                    group="1"/>
        ```
        Args:
            root: The root element to append the collision properties to.

        Returns:
            The XML element representing the collision properties.

        Examples:
            >>> collision = CollisionLink(origin=Origin(...), geometry=BoxGeometry(...))
            >>> collision.to_mjcf()
            <Element 'collision' at 0x7f8b3c0b4c70>
        """
        collision = root if root.tag == "geom" else ET.SubElement(root, "geom")
        if self.name:
            collision.set("name", self.name)
        collision.set("contype", "1")
        collision.set("conaffinity", "1")
        self.origin.to_mjcf(collision)

        if self.geometry:
            self.geometry.to_mjcf(collision)

        collision.set("group", "0")

        if self.friction:
            collision.set("friction", " ".join(format_number(v) for v in self.friction))

    @classmethod
    def from_xml(cls, xml: ET.Element) -> "CollisionLink":
        """
        Create a collision link from an XML element.

        Args:
            xml: The XML element to create the collision link from.

        Returns:
            The collision link created from the XML element.

        Examples:
            >>> xml = ET.Element('collision')
            >>> CollisionLink.from_xml(xml)
            CollisionLink(name='collision', origin=None, geometry=None)
        """
        name = xml.get("name")

        origin_element = xml.find("origin")
        origin = Origin.from_xml(origin_element) if origin_element is not None else None

        geometry_element = xml.find("geometry")
        geometry = set_geometry_from_xml(geometry_element) if geometry_element is not None else None

        return cls(name=name, origin=origin, geometry=geometry)

from_xml(xml) classmethod

Create a collision link from an XML element.

Parameters:

Name Type Description Default
xml Element

The XML element to create the collision link from.

required

Returns:

Type Description
CollisionLink

The collision link created from the XML element.

Examples:

>>> xml = ET.Element('collision')
>>> CollisionLink.from_xml(xml)
CollisionLink(name='collision', origin=None, geometry=None)
Source code in onshape_robotics_toolkit\models\link.py
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
@classmethod
def from_xml(cls, xml: ET.Element) -> "CollisionLink":
    """
    Create a collision link from an XML element.

    Args:
        xml: The XML element to create the collision link from.

    Returns:
        The collision link created from the XML element.

    Examples:
        >>> xml = ET.Element('collision')
        >>> CollisionLink.from_xml(xml)
        CollisionLink(name='collision', origin=None, geometry=None)
    """
    name = xml.get("name")

    origin_element = xml.find("origin")
    origin = Origin.from_xml(origin_element) if origin_element is not None else None

    geometry_element = xml.find("geometry")
    geometry = set_geometry_from_xml(geometry_element) if geometry_element is not None else None

    return cls(name=name, origin=origin, geometry=geometry)

to_mjcf(root)

Convert the collision properties to an MuJoCo compatible XML element.

Example XML:

      <geom name="Assembly-2-1-SUB-Part-5-1-collision"
            pos="0.0994445 -0.000366963 0.0171076"
            quat="-0.92388 -4.28774e-08 0.382683 0"
            type="mesh"
            rgba="1 0.5 0 1"
            mesh="Assembly-2-1-SUB-Part-5-1"
            contype="1"
            conaffinity="0"
            density="0"
            group="1"/>
Args: root: The root element to append the collision properties to.

Returns:

Type Description
None

The XML element representing the collision properties.

Examples:

>>> collision = CollisionLink(origin=Origin(...), geometry=BoxGeometry(...))
>>> collision.to_mjcf()
<Element 'collision' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
def to_mjcf(self, root: ET.Element) -> None:
    """
    Convert the collision properties to an MuJoCo compatible XML element.

    Example XML:
    ```xml
          <geom name="Assembly-2-1-SUB-Part-5-1-collision"
                pos="0.0994445 -0.000366963 0.0171076"
                quat="-0.92388 -4.28774e-08 0.382683 0"
                type="mesh"
                rgba="1 0.5 0 1"
                mesh="Assembly-2-1-SUB-Part-5-1"
                contype="1"
                conaffinity="0"
                density="0"
                group="1"/>
    ```
    Args:
        root: The root element to append the collision properties to.

    Returns:
        The XML element representing the collision properties.

    Examples:
        >>> collision = CollisionLink(origin=Origin(...), geometry=BoxGeometry(...))
        >>> collision.to_mjcf()
        <Element 'collision' at 0x7f8b3c0b4c70>
    """
    collision = root if root.tag == "geom" else ET.SubElement(root, "geom")
    if self.name:
        collision.set("name", self.name)
    collision.set("contype", "1")
    collision.set("conaffinity", "1")
    self.origin.to_mjcf(collision)

    if self.geometry:
        self.geometry.to_mjcf(collision)

    collision.set("group", "0")

    if self.friction:
        collision.set("friction", " ".join(format_number(v) for v in self.friction))

to_xml(root=None)

Convert the collision properties to an XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the collision properties to.

None

Returns:

Type Description
Element

The XML element representing the collision properties.

Examples:

>>> collision = CollisionLink(origin=Origin(...), geometry=BoxGeometry(...))
>>> collision.to_xml()
<Element 'collision' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the collision properties to an XML element.

    Args:
        root: The root element to append the collision properties to.

    Returns:
        The XML element representing the collision properties.

    Examples:
        >>> collision = CollisionLink(origin=Origin(...), geometry=BoxGeometry(...))
        >>> collision.to_xml()
        <Element 'collision' at 0x7f8b3c0b4c70>
    """
    collision = ET.Element("collision") if root is None else ET.SubElement(root, "collision")
    if self.name:
        collision.set("name", self.name)
    self.origin.to_xml(collision)
    self.geometry.to_xml(collision)
    return collision

transform(transformation_matrix)

Apply a transformation to the visual link's origin.

Parameters:

Name Type Description Default
transformation_matrix ndarray

A 4x4 transformation matrix (homogeneous).

required
Source code in onshape_robotics_toolkit\models\link.py
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
def transform(self, transformation_matrix: np.ndarray) -> None:
    """
    Apply a transformation to the visual link's origin.

    Args:
        transformation_matrix (np.ndarray): A 4x4 transformation matrix (homogeneous).
    """
    # Apply translation and rotation to the origin position
    pos = np.array([self.origin.xyz[0], self.origin.xyz[1], self.origin.xyz[2], 1])
    new_pos = transformation_matrix @ pos
    self.origin.xyz = tuple(new_pos[:3])  # Update position

    # Extract the rotation from the transformation matrix
    rotation_matrix = transformation_matrix[:3, :3]
    current_rotation = R.from_euler("xyz", self.origin.rpy)
    new_rotation = R.from_matrix(rotation_matrix @ current_rotation.as_matrix())
    self.origin.rpy = new_rotation.as_euler("xyz").tolist()

Colors

Bases: tuple[float, float, float], Enum

Enumerates the possible colors in RGBA format for a link in the robot model.

Each color is represented as a tuple of four float values (r, g, b, a), where each component ranges from 0.0 to 1.0.

Attributes:

Name Type Description
RED tuple[float, float, float, float]

Color red (1, 0, 0, 1).

GREEN tuple[float, float, float, float]

Color green (0, 1, 0, 1).

BLUE tuple[float, float, float, float]

Color blue (0, 0, 1, 1).

YELLOW tuple[float, float, float, float]

Color yellow (1, 1, 0, 1).

CYAN tuple[float, float, float, float]

Color cyan (0, 1, 1, 1).

MAGENTA tuple[float, float, float, float]

Color magenta (1, 0, 1, 1).

WHITE tuple[float, float, float, float]

Color white (1, 1, 1, 1).

BLACK tuple[float, float, float, float]

Color black (0, 0, 0, 1).

ORANGE tuple[float, float, float, float]

Color orange (1, 0.5, 0, 1).

PINK tuple[float, float, float, float]

Color pink (1, 0, 0.5, 1).

Examples:

>>> Colors.RED
<Colors.RED: (1.0, 0.0, 0.0, 1.0)>
>>> Colors.BLUE.value
(0.0, 0.0, 1.0, 1.0)
Source code in onshape_robotics_toolkit\models\link.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Colors(tuple[float, float, float], Enum):
    """
    Enumerates the possible colors in RGBA format for a link in the robot model.

    Each color is represented as a tuple of four float values (r, g, b, a),
    where each component ranges from 0.0 to 1.0.

    Attributes:
        RED (tuple[float, float, float, float]): Color red (1, 0, 0, 1).
        GREEN (tuple[float, float, float, float]): Color green (0, 1, 0, 1).
        BLUE (tuple[float, float, float, float]): Color blue (0, 0, 1, 1).
        YELLOW (tuple[float, float, float, float]): Color yellow (1, 1, 0, 1).
        CYAN (tuple[float, float, float, float]): Color cyan (0, 1, 1, 1).
        MAGENTA (tuple[float, float, float, float]): Color magenta (1, 0, 1, 1).
        WHITE (tuple[float, float, float, float]): Color white (1, 1, 1, 1).
        BLACK (tuple[float, float, float, float]): Color black (0, 0, 0, 1).
        ORANGE (tuple[float, float, float, float]): Color orange (1, 0.5, 0, 1).
        PINK (tuple[float, float, float, float]): Color pink (1, 0, 0.5, 1).

    Examples:
        >>> Colors.RED
        <Colors.RED: (1.0, 0.0, 0.0, 1.0)>
        >>> Colors.BLUE.value
        (0.0, 0.0, 1.0, 1.0)
    """

    RED = (1.0, 0.0, 0.0, 1.0)
    GREEN = (0.0, 1.0, 0.0, 1.0)
    BLUE = (0.0, 0.0, 1.0, 1.0)
    YELLOW = (1.0, 1.0, 0.0, 1.0)
    CYAN = (0.0, 1.0, 1.0, 1.0)
    MAGENTA = (1.0, 0.0, 1.0, 1.0)
    WHITE = (1.0, 1.0, 1.0, 1.0)
    BLACK = (0.0, 0.0, 0.0, 1.0)
    ORANGE = (1.0, 0.5, 0.0, 1.0)
    PINK = (1.0, 0.0, 0.5, 1.0)

Inertia dataclass

Represents the inertia tensor of a link in the robot model.

The inertia tensor is a 3x3 symmetric matrix that describes how the body's mass is distributed relative to its center of mass.

Attributes:

Name Type Description
ixx float

Moment of inertia about the x-axis.

iyy float

Moment of inertia about the y-axis.

izz float

Moment of inertia about the z-axis.

ixy float

Product of inertia about the xy-plane.

ixz float

Product of inertia about the xz-plane.

iyz float

Product of inertia about the yz-plane.

Methods:

Name Description
to_xml

Converts the inertia tensor to an XML element.

to_mjcf

Converts the inertia tensor to a MuJoCo compatible XML element.

to_matrix

Returns the inertia tensor as a 3x3 numpy array.

Class Methods

from_xml: Creates an inertia tensor from an XML element. zero_inertia: Creates an inertia tensor with all zero values.

Examples:

>>> inertia = Inertia(ixx=1.0, iyy=1.0, izz=1.0, ixy=0.0, ixz=0.0, iyz=0.0)
>>> inertia.to_matrix
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
Source code in onshape_robotics_toolkit\models\link.py
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
@dataclass
class Inertia:
    """
    Represents the inertia tensor of a link in the robot model.

    The inertia tensor is a 3x3 symmetric matrix that describes how the body's mass
    is distributed relative to its center of mass.

    Attributes:
        ixx (float): Moment of inertia about the x-axis.
        iyy (float): Moment of inertia about the y-axis.
        izz (float): Moment of inertia about the z-axis.
        ixy (float): Product of inertia about the xy-plane.
        ixz (float): Product of inertia about the xz-plane.
        iyz (float): Product of inertia about the yz-plane.

    Methods:
        to_xml: Converts the inertia tensor to an XML element.
        to_mjcf: Converts the inertia tensor to a MuJoCo compatible XML element.
        to_matrix: Returns the inertia tensor as a 3x3 numpy array.

    Class Methods:
        from_xml: Creates an inertia tensor from an XML element.
        zero_inertia: Creates an inertia tensor with all zero values.

    Examples:
        >>> inertia = Inertia(ixx=1.0, iyy=1.0, izz=1.0, ixy=0.0, ixz=0.0, iyz=0.0)
        >>> inertia.to_matrix
        array([[1., 0., 0.],
               [0., 1., 0.],
               [0., 0., 1.]])
    """

    ixx: float
    iyy: float
    izz: float
    ixy: float
    ixz: float
    iyz: float

    def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the inertia tensor to an XML element.

        Args:
            root: The root element to append the inertia tensor to.

        Returns:
            The XML element representing the inertia tensor.

        Examples:
            >>> inertia = Inertia(ixx=1.0, iyy=2.0, izz=3.0, ixy=0.0, ixz=0.0, iyz=0.0)
            >>> inertia.to_xml()
            <Element 'inertia' at 0x7f8b3c0b4c70>
        """

        inertia = ET.Element("inertia") if root is None else ET.SubElement(root, "inertia")
        inertia.set("ixx", format_number(self.ixx))
        inertia.set("iyy", format_number(self.iyy))
        inertia.set("izz", format_number(self.izz))
        inertia.set("ixy", format_number(self.ixy))
        inertia.set("ixz", format_number(self.ixz))
        inertia.set("iyz", format_number(self.iyz))
        return inertia

    def to_mjcf(self, root: ET.Element) -> None:
        """
        Convert the inertia tensor to an MuJoCo compatible XML element.

        Args:
            root: The root element to append the inertia tensor to.

        Returns:
            The XML element representing the inertia tensor.

        Examples:
            >>> inertia = Inertia(ixx=1.0, iyy=2.0, izz=3.0, ixy=0.0, ixz=0.0, iyz=0.0)
            >>> inertia.to_mjcf()
            <Element 'inertia' at 0x7f8b3c0b4c70>
        """
        inertial = root if root.tag == "inertial" else ET.SubElement(root, "inertial")
        inertial.set("diaginertia", " ".join(format_number(v) for v in [self.ixx, self.iyy, self.izz]))

    @classmethod
    def from_xml(cls, xml: ET.Element) -> "Inertia":
        """
        Create an inertia tensor from an XML element.

        Args:
            xml: The XML element to create the inertia tensor from.

        Returns:
            The inertia tensor created from the XML element.

        Examples:
            >>> xml = ET.Element('inertia')
            >>> Inertia.from_xml(xml)
            Inertia(ixx=0.0, iyy=0.0, izz=0.0, ixy=0.0, ixz=0.0, iyz=0.0)
        """
        ixx = float(xml.get("ixx"))
        iyy = float(xml.get("iyy"))
        izz = float(xml.get("izz"))
        ixy = float(xml.get("ixy"))
        ixz = float(xml.get("ixz"))
        iyz = float(xml.get("iyz"))
        return cls(ixx, iyy, izz, ixy, ixz, iyz)

    @classmethod
    def zero_inertia(cls) -> "Inertia":
        """
        Create an inertia tensor with zero values.

        Returns:
            The inertia tensor with zero values.

        Examples:
            >>> Inertia.zero_inertia()
            Inertia(ixx=0.0, iyy=0.0, izz=0.0, ixy=0.0, ixz=0.0, iyz=0.0)
        """
        return cls(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)

    @property
    def to_matrix(self) -> np.array:
        """
        Returns the inertia tensor as a 3x3 numpy array.

        Returns:
            The inertia tensor as a 3x3 numpy array.
        """
        return np.array([
            [self.ixx, self.ixy, self.ixz],
            [self.ixy, self.iyy, self.iyz],
            [self.ixz, self.iyz, self.izz]
        ])

to_matrix property

Returns the inertia tensor as a 3x3 numpy array.

Returns:

Type Description
array

The inertia tensor as a 3x3 numpy array.

from_xml(xml) classmethod

Create an inertia tensor from an XML element.

Parameters:

Name Type Description Default
xml Element

The XML element to create the inertia tensor from.

required

Returns:

Type Description
Inertia

The inertia tensor created from the XML element.

Examples:

>>> xml = ET.Element('inertia')
>>> Inertia.from_xml(xml)
Inertia(ixx=0.0, iyy=0.0, izz=0.0, ixy=0.0, ixz=0.0, iyz=0.0)
Source code in onshape_robotics_toolkit\models\link.py
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
@classmethod
def from_xml(cls, xml: ET.Element) -> "Inertia":
    """
    Create an inertia tensor from an XML element.

    Args:
        xml: The XML element to create the inertia tensor from.

    Returns:
        The inertia tensor created from the XML element.

    Examples:
        >>> xml = ET.Element('inertia')
        >>> Inertia.from_xml(xml)
        Inertia(ixx=0.0, iyy=0.0, izz=0.0, ixy=0.0, ixz=0.0, iyz=0.0)
    """
    ixx = float(xml.get("ixx"))
    iyy = float(xml.get("iyy"))
    izz = float(xml.get("izz"))
    ixy = float(xml.get("ixy"))
    ixz = float(xml.get("ixz"))
    iyz = float(xml.get("iyz"))
    return cls(ixx, iyy, izz, ixy, ixz, iyz)

to_mjcf(root)

Convert the inertia tensor to an MuJoCo compatible XML element.

Parameters:

Name Type Description Default
root Element

The root element to append the inertia tensor to.

required

Returns:

Type Description
None

The XML element representing the inertia tensor.

Examples:

>>> inertia = Inertia(ixx=1.0, iyy=2.0, izz=3.0, ixy=0.0, ixz=0.0, iyz=0.0)
>>> inertia.to_mjcf()
<Element 'inertia' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
def to_mjcf(self, root: ET.Element) -> None:
    """
    Convert the inertia tensor to an MuJoCo compatible XML element.

    Args:
        root: The root element to append the inertia tensor to.

    Returns:
        The XML element representing the inertia tensor.

    Examples:
        >>> inertia = Inertia(ixx=1.0, iyy=2.0, izz=3.0, ixy=0.0, ixz=0.0, iyz=0.0)
        >>> inertia.to_mjcf()
        <Element 'inertia' at 0x7f8b3c0b4c70>
    """
    inertial = root if root.tag == "inertial" else ET.SubElement(root, "inertial")
    inertial.set("diaginertia", " ".join(format_number(v) for v in [self.ixx, self.iyy, self.izz]))

to_xml(root=None)

Convert the inertia tensor to an XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the inertia tensor to.

None

Returns:

Type Description
Element

The XML element representing the inertia tensor.

Examples:

>>> inertia = Inertia(ixx=1.0, iyy=2.0, izz=3.0, ixy=0.0, ixz=0.0, iyz=0.0)
>>> inertia.to_xml()
<Element 'inertia' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the inertia tensor to an XML element.

    Args:
        root: The root element to append the inertia tensor to.

    Returns:
        The XML element representing the inertia tensor.

    Examples:
        >>> inertia = Inertia(ixx=1.0, iyy=2.0, izz=3.0, ixy=0.0, ixz=0.0, iyz=0.0)
        >>> inertia.to_xml()
        <Element 'inertia' at 0x7f8b3c0b4c70>
    """

    inertia = ET.Element("inertia") if root is None else ET.SubElement(root, "inertia")
    inertia.set("ixx", format_number(self.ixx))
    inertia.set("iyy", format_number(self.iyy))
    inertia.set("izz", format_number(self.izz))
    inertia.set("ixy", format_number(self.ixy))
    inertia.set("ixz", format_number(self.ixz))
    inertia.set("iyz", format_number(self.iyz))
    return inertia

zero_inertia() classmethod

Create an inertia tensor with zero values.

Returns:

Type Description
Inertia

The inertia tensor with zero values.

Examples:

>>> Inertia.zero_inertia()
Inertia(ixx=0.0, iyy=0.0, izz=0.0, ixy=0.0, ixz=0.0, iyz=0.0)
Source code in onshape_robotics_toolkit\models\link.py
460
461
462
463
464
465
466
467
468
469
470
471
472
@classmethod
def zero_inertia(cls) -> "Inertia":
    """
    Create an inertia tensor with zero values.

    Returns:
        The inertia tensor with zero values.

    Examples:
        >>> Inertia.zero_inertia()
        Inertia(ixx=0.0, iyy=0.0, izz=0.0, ixy=0.0, ixz=0.0, iyz=0.0)
    """
    return cls(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)

Represents the inertial properties of a link in the robot model.

This class combines mass, center of mass (via origin), and inertia tensor properties to fully describe a link's inertial characteristics.

Attributes:

Name Type Description
mass float

The mass of the link in kilograms.

inertia Inertia

The inertia tensor of the link.

origin Origin

The center of mass position and orientation.

Methods:

Name Description
to_xml

Converts the inertial properties to an XML element.

to_mjcf

Converts the inertial properties to a MuJoCo compatible XML element.

transform

Applies a transformation matrix to the inertial properties.

Class Methods

from_xml: Creates an InertialLink from an XML element.

Examples:

>>> inertial = InertialLink(
...     mass=1.0,
...     inertia=Inertia(1.0, 1.0, 1.0, 0.0, 0.0, 0.0),
...     origin=Origin.zero_origin()
... )
>>> inertial.to_xml()
<Element 'inertial' at 0x...>
Source code in onshape_robotics_toolkit\models\link.py
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
@dataclass
class InertialLink:
    """
    Represents the inertial properties of a link in the robot model.

    This class combines mass, center of mass (via origin), and inertia tensor
    properties to fully describe a link's inertial characteristics.

    Attributes:
        mass (float): The mass of the link in kilograms.
        inertia (Inertia): The inertia tensor of the link.
        origin (Origin): The center of mass position and orientation.

    Methods:
        to_xml: Converts the inertial properties to an XML element.
        to_mjcf: Converts the inertial properties to a MuJoCo compatible XML element.
        transform: Applies a transformation matrix to the inertial properties.

    Class Methods:
        from_xml: Creates an InertialLink from an XML element.

    Examples:
        >>> inertial = InertialLink(
        ...     mass=1.0,
        ...     inertia=Inertia(1.0, 1.0, 1.0, 0.0, 0.0, 0.0),
        ...     origin=Origin.zero_origin()
        ... )
        >>> inertial.to_xml()
        <Element 'inertial' at 0x...>
    """

    mass: float
    inertia: Inertia
    origin: Origin

    def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the inertial properties to an XML element.

        Args:
            root: The root element to append the inertial properties to.

        Returns:
            The XML element representing the inertial properties.

        Examples:
            >>> inertial = InertialLink(
            ...     mass=1.0,
            ...     inertia=Inertia(1.0, 1.0, 1.0, 0.0, 0.0, 0.0),
            ...     origin=Origin.zero_origin()
            ... )
            >>> inertial.to_xml()
            <Element 'inertial' at 0x...>
        """
        inertial = ET.Element("inertial") if root is None else ET.SubElement(root, "inertial")
        ET.SubElement(inertial, "mass", value=format_number(self.mass))
        self.inertia.to_xml(inertial)
        self.origin.to_xml(inertial)
        return inertial

    def to_mjcf(self, root: ET.Element) -> None:
        """
        Convert the inertial properties to an MuJoCo compatible XML element.

        Example XML:
        ```xml
        <inertial pos="0 0 -0.0075" euler="0.5 0.5 -0.5" mass="0.624"
                  diaginertia="0.073541512 0.07356916 0.073543931" />
        ```
        Args:
            root: The root element to append the inertial properties to.
        """
        inertial = root if root.tag == "inertial" else ET.SubElement(root, "inertial")
        inertial.set("mass", format_number(self.mass))
        self.origin.to_mjcf(inertial)
        self.inertia.to_mjcf(inertial)

    def transform(self, tf_matrix: np.matrix, inplace: bool = False) -> Union["InertialLink", None]:
        """
        Apply a transformation matrix to the Inertial Properties of the a link.

        Args:
            matrix: The transformation matrix to apply to the origin.
            inplace: Whether to apply the transformation in place.

        Returns:
            An updated Inertial Link with the transformation applied to both:
            * the inertia tensor (giving a transformed "inertia tensor prime" = [ixx', iyy', izz', ixy', ixz', iyz'])
            * AND to the origin too (via the Origin class's transform logic [~line 100])

        Examples {@}:
            >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
            >>> matrix = np.eye(4)
            >>> inertial.transform(matrix)

        Analysis and References:
            The essence is to convert the Inertia tensor to a matrix and then transform the matrix via the equation
            I_prime = R·I·Transpose[R] + m(||d||^2·I - d·Transpose[d]) 
            Then we put the components into the resultant Inertial Link
            An analysis (on 100k runs) suggests that this is 3× faster than a direct approach on the tensor elements likely because numpy's libraries are optimized for matrix operations.
            Ref: https://chatgpt.com/share/6781b6ac-772c-8006-b1a9-7f2dc3e3ef4d
        """

        R = tf_matrix[:3, :3]  # Top-left 3x3 block is the rotation matrix
        p = tf_matrix[:3, 3]   # Top-right 3x1 block is the translation vector

        inertia_matrix = self.inertia.to_matrix
        I_rot = R @ inertia_matrix @ R.T

        # Compute the parallel axis theorem adjustment
        parallel_axis_adjustment = self.mass * (np.dot(p, p) * np.eye(3) - np.outer(p, p))

        # Final transformed inertia matrix
        I_transformed = I_rot + parallel_axis_adjustment

        ixx_prime = I_transformed[0, 0]
        iyy_prime = I_transformed[1, 1]
        izz_prime = I_transformed[2, 2]
        ixy_prime = I_transformed[0, 1]
        ixz_prime = I_transformed[0, 2]
        iyz_prime = I_transformed[1, 2]

        # Transform the Origin (Don't replace the original in case the user keeps the inplace flag false)
        Origin_prime = self.origin.transform(tf_matrix)

        # Update values and (if requested) put the extracted values into a new_InertialLink
        if inplace:
            # mass stays the same :-) ==> self.mass = new_InertialLink.mass
            self.inertia.ixx = ixx_prime
            self.inertia.iyy = iyy_prime
            self.inertia.izz = izz_prime
            self.inertia.ixy = ixy_prime
            self.inertia.ixz = ixz_prime
            self.inertia.iyz = iyz_prime
            self.origin = Origin_prime
            return None
        else:
            new_InertialLink = InertialLink(mass=self.mass, inertia=Inertia(ixx_prime, iyy_prime, izz_prime, ixy_prime, ixz_prime, iyz_prime), origin=Origin_prime)
            return new_InertialLink

    @classmethod
    def from_xml(cls, xml: ET.Element) -> "InertialLink":
        """
        Create inertial properties from an XML element.

        Args:
            xml: The XML element to create the inertial properties from.

        Returns:
            The inertial properties created from the XML element.

        Examples:
            >>> xml = ET.Element('inertial')
            >>> InertialLink.from_xml(xml)
            InertialLink(mass=0.0, inertia=None, origin=None)
        """
        mass = float(xml.find("mass").get("value"))

        inertia_element = xml.find("inertia")
        inertia = Inertia.from_xml(inertia_element) if inertia_element is not None else None

        origin_element = xml.find("origin")
        origin = Origin.from_xml(origin_element) if origin_element is not None else None

        return cls(mass=mass, inertia=inertia, origin=origin)

from_xml(xml) classmethod

Create inertial properties from an XML element.

Parameters:

Name Type Description Default
xml Element

The XML element to create the inertial properties from.

required

Returns:

Type Description
InertialLink

The inertial properties created from the XML element.

Examples:

>>> xml = ET.Element('inertial')
>>> InertialLink.from_xml(xml)
InertialLink(mass=0.0, inertia=None, origin=None)
Source code in onshape_robotics_toolkit\models\link.py
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
@classmethod
def from_xml(cls, xml: ET.Element) -> "InertialLink":
    """
    Create inertial properties from an XML element.

    Args:
        xml: The XML element to create the inertial properties from.

    Returns:
        The inertial properties created from the XML element.

    Examples:
        >>> xml = ET.Element('inertial')
        >>> InertialLink.from_xml(xml)
        InertialLink(mass=0.0, inertia=None, origin=None)
    """
    mass = float(xml.find("mass").get("value"))

    inertia_element = xml.find("inertia")
    inertia = Inertia.from_xml(inertia_element) if inertia_element is not None else None

    origin_element = xml.find("origin")
    origin = Origin.from_xml(origin_element) if origin_element is not None else None

    return cls(mass=mass, inertia=inertia, origin=origin)

to_mjcf(root)

Convert the inertial properties to an MuJoCo compatible XML element.

Example XML:

<inertial pos="0 0 -0.0075" euler="0.5 0.5 -0.5" mass="0.624"
          diaginertia="0.073541512 0.07356916 0.073543931" />
Args: root: The root element to append the inertial properties to.

Source code in onshape_robotics_toolkit\models\link.py
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
def to_mjcf(self, root: ET.Element) -> None:
    """
    Convert the inertial properties to an MuJoCo compatible XML element.

    Example XML:
    ```xml
    <inertial pos="0 0 -0.0075" euler="0.5 0.5 -0.5" mass="0.624"
              diaginertia="0.073541512 0.07356916 0.073543931" />
    ```
    Args:
        root: The root element to append the inertial properties to.
    """
    inertial = root if root.tag == "inertial" else ET.SubElement(root, "inertial")
    inertial.set("mass", format_number(self.mass))
    self.origin.to_mjcf(inertial)
    self.inertia.to_mjcf(inertial)

to_xml(root=None)

Convert the inertial properties to an XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the inertial properties to.

None

Returns:

Type Description
Element

The XML element representing the inertial properties.

Examples:

>>> inertial = InertialLink(
...     mass=1.0,
...     inertia=Inertia(1.0, 1.0, 1.0, 0.0, 0.0, 0.0),
...     origin=Origin.zero_origin()
... )
>>> inertial.to_xml()
<Element 'inertial' at 0x...>
Source code in onshape_robotics_toolkit\models\link.py
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the inertial properties to an XML element.

    Args:
        root: The root element to append the inertial properties to.

    Returns:
        The XML element representing the inertial properties.

    Examples:
        >>> inertial = InertialLink(
        ...     mass=1.0,
        ...     inertia=Inertia(1.0, 1.0, 1.0, 0.0, 0.0, 0.0),
        ...     origin=Origin.zero_origin()
        ... )
        >>> inertial.to_xml()
        <Element 'inertial' at 0x...>
    """
    inertial = ET.Element("inertial") if root is None else ET.SubElement(root, "inertial")
    ET.SubElement(inertial, "mass", value=format_number(self.mass))
    self.inertia.to_xml(inertial)
    self.origin.to_xml(inertial)
    return inertial

transform(tf_matrix, inplace=False)

Apply a transformation matrix to the Inertial Properties of the a link.

Parameters:

Name Type Description Default
matrix

The transformation matrix to apply to the origin.

required
inplace bool

Whether to apply the transformation in place.

False

Returns:

Type Description
Union[InertialLink, None]

An updated Inertial Link with the transformation applied to both:

Union[InertialLink, None]
  • the inertia tensor (giving a transformed "inertia tensor prime" = [ixx', iyy', izz', ixy', ixz', iyz'])
Union[InertialLink, None]
  • AND to the origin too (via the Origin class's transform logic [~line 100])

Examples {@}: >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0)) >>> matrix = np.eye(4) >>> inertial.transform(matrix)

Analysis and References

The essence is to convert the Inertia tensor to a matrix and then transform the matrix via the equation I_prime = R·I·Transpose[R] + m(||d||^2·I - d·Transpose[d]) Then we put the components into the resultant Inertial Link An analysis (on 100k runs) suggests that this is 3× faster than a direct approach on the tensor elements likely because numpy's libraries are optimized for matrix operations. Ref: https://chatgpt.com/share/6781b6ac-772c-8006-b1a9-7f2dc3e3ef4d

Source code in onshape_robotics_toolkit\models\link.py
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
def transform(self, tf_matrix: np.matrix, inplace: bool = False) -> Union["InertialLink", None]:
    """
    Apply a transformation matrix to the Inertial Properties of the a link.

    Args:
        matrix: The transformation matrix to apply to the origin.
        inplace: Whether to apply the transformation in place.

    Returns:
        An updated Inertial Link with the transformation applied to both:
        * the inertia tensor (giving a transformed "inertia tensor prime" = [ixx', iyy', izz', ixy', ixz', iyz'])
        * AND to the origin too (via the Origin class's transform logic [~line 100])

    Examples {@}:
        >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
        >>> matrix = np.eye(4)
        >>> inertial.transform(matrix)

    Analysis and References:
        The essence is to convert the Inertia tensor to a matrix and then transform the matrix via the equation
        I_prime = R·I·Transpose[R] + m(||d||^2·I - d·Transpose[d]) 
        Then we put the components into the resultant Inertial Link
        An analysis (on 100k runs) suggests that this is 3× faster than a direct approach on the tensor elements likely because numpy's libraries are optimized for matrix operations.
        Ref: https://chatgpt.com/share/6781b6ac-772c-8006-b1a9-7f2dc3e3ef4d
    """

    R = tf_matrix[:3, :3]  # Top-left 3x3 block is the rotation matrix
    p = tf_matrix[:3, 3]   # Top-right 3x1 block is the translation vector

    inertia_matrix = self.inertia.to_matrix
    I_rot = R @ inertia_matrix @ R.T

    # Compute the parallel axis theorem adjustment
    parallel_axis_adjustment = self.mass * (np.dot(p, p) * np.eye(3) - np.outer(p, p))

    # Final transformed inertia matrix
    I_transformed = I_rot + parallel_axis_adjustment

    ixx_prime = I_transformed[0, 0]
    iyy_prime = I_transformed[1, 1]
    izz_prime = I_transformed[2, 2]
    ixy_prime = I_transformed[0, 1]
    ixz_prime = I_transformed[0, 2]
    iyz_prime = I_transformed[1, 2]

    # Transform the Origin (Don't replace the original in case the user keeps the inplace flag false)
    Origin_prime = self.origin.transform(tf_matrix)

    # Update values and (if requested) put the extracted values into a new_InertialLink
    if inplace:
        # mass stays the same :-) ==> self.mass = new_InertialLink.mass
        self.inertia.ixx = ixx_prime
        self.inertia.iyy = iyy_prime
        self.inertia.izz = izz_prime
        self.inertia.ixy = ixy_prime
        self.inertia.ixz = ixz_prime
        self.inertia.iyz = iyz_prime
        self.origin = Origin_prime
        return None
    else:
        new_InertialLink = InertialLink(mass=self.mass, inertia=Inertia(ixx_prime, iyy_prime, izz_prime, ixy_prime, ixz_prime, iyz_prime), origin=Origin_prime)
        return new_InertialLink

Represents a complete link in the robot model.

A link is a rigid body in the robot model that can contain visual, collision, and inertial properties. Links are connected by joints to form the complete robot structure.

Attributes:

Name Type Description
name str

The unique identifier for the link.

visual VisualLink | None

Optional visual properties for rendering.

collision CollisionLink | None

Optional collision properties for physics simulation.

inertial InertialLink | None

Optional inertial properties for dynamics.

Methods:

Name Description
to_xml

Converts the link to an XML element.

to_mjcf

Converts the link to a MuJoCo compatible XML element.

Class Methods

from_xml: Creates a Link from an XML element.

Examples:

>>> link = Link(
...     name="base_link",
...     visual=VisualLink(...),
...     collision=CollisionLink(...),
...     inertial=InertialLink(...)
... )
>>> link.to_xml()
<Element 'link' at 0x...>
>>> xml_str = '''
...     <link name="base_link">
...         <visual>...</visual>
...         <collision>...</collision>
...         <inertial>...</inertial>
...     </link>
... '''
>>> xml_element = ET.fromstring(xml_str)
>>> link = Link.from_xml(xml_element)
Source code in onshape_robotics_toolkit\models\link.py
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
@dataclass
class Link:
    """
    Represents a complete link in the robot model.

    A link is a rigid body in the robot model that can contain visual, collision,
    and inertial properties. Links are connected by joints to form the complete
    robot structure.

    Attributes:
        name (str): The unique identifier for the link.
        visual (VisualLink | None): Optional visual properties for rendering.
        collision (CollisionLink | None): Optional collision properties for physics simulation.
        inertial (InertialLink | None): Optional inertial properties for dynamics.

    Methods:
        to_xml: Converts the link to an XML element.
        to_mjcf: Converts the link to a MuJoCo compatible XML element.

    Class Methods:
        from_xml: Creates a Link from an XML element.

    Examples:
        >>> link = Link(
        ...     name="base_link",
        ...     visual=VisualLink(...),
        ...     collision=CollisionLink(...),
        ...     inertial=InertialLink(...)
        ... )
        >>> link.to_xml()
        <Element 'link' at 0x...>

        >>> xml_str = '''
        ...     <link name="base_link">
        ...         <visual>...</visual>
        ...         <collision>...</collision>
        ...         <inertial>...</inertial>
        ...     </link>
        ... '''
        >>> xml_element = ET.fromstring(xml_str)
        >>> link = Link.from_xml(xml_element)
    """

    name: str
    visual: VisualLink | None = None
    collision: CollisionLink | None = None
    inertial: InertialLink | None = None

    def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the link to an XML element.

        Args:
            root: The root element to append the link to.

        Returns:
            The XML element representing the link.

        Examples:
            >>> link = Link(
            ...     name="base_link",
            ...     visual=VisualLink(...),
            ...     collision=CollisionLink(...),
            ...     inertial=InertialLink(...)
            ... )
            >>> link.to_xml()
            <Element 'link' at 0x...>
        """
        link = ET.Element("link") if root is None else ET.SubElement(root, "link")
        link.set("name", self.name)
        if self.visual is not None:
            self.visual.to_xml(link)
        if self.collision is not None:
            self.collision.to_xml(link)
        if self.inertial is not None:
            self.inertial.to_xml(link)
        return link

    def to_mjcf(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the link to an MuJoCo compatible XML element.

        Args:
            root: The root element to append the link to.

        Returns:
            The XML element representing the link.

        Examples:
            >>> link = Link(
            ...     name="base_link",
            ...     visual=VisualLink(...),
            ...     collision=CollisionLink(...),
            ...     inertial=InertialLink(...)
            ... )
            >>> link.to_mjcf()
            <Element 'link' at 0x...>
        """
        link = ET.Element("body") if root is None else ET.SubElement(root, "body")
        link.set("name", self.name)

        if self.visual:
            link.set("pos", " ".join(map(str, self.visual.origin.xyz)))
            link.set("euler", " ".join(map(str, self.visual.origin.rpy)))

        if self.collision:
            self.collision.to_mjcf(link)

        if self.visual:
            self.visual.to_mjcf(link)

        if self.inertial:
            self.inertial.to_mjcf(link)

        return link

    @classmethod
    def from_xml(cls, xml: ET.Element) -> "Link":
        """
        Create a link from an XML element.

        Args:
            xml: The XML element to create the link from.

        Returns:
            The link created from the XML element.

        Examples:
            >>> xml = ET.Element('link')
            >>> Link.from_xml(xml)
            Link(name='link', visual=None, collision=None, inertial=None)
        """
        name = xml.get("name")

        visual_element = xml.find("visual")
        visual = VisualLink.from_xml(visual_element) if visual_element is not None else None

        collision_element = xml.find("collision")
        collision = CollisionLink.from_xml(collision_element) if collision_element is not None else None

        inertial_element = xml.find("inertial")
        inertial = InertialLink.from_xml(inertial_element) if inertial_element is not None else None

        return cls(name=name, visual=visual, collision=collision, inertial=inertial)

from_xml(xml) classmethod

Create a link from an XML element.

Parameters:

Name Type Description Default
xml Element

The XML element to create the link from.

required

Returns:

Type Description
Link

The link created from the XML element.

Examples:

>>> xml = ET.Element('link')
>>> Link.from_xml(xml)
Link(name='link', visual=None, collision=None, inertial=None)
Source code in onshape_robotics_toolkit\models\link.py
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
@classmethod
def from_xml(cls, xml: ET.Element) -> "Link":
    """
    Create a link from an XML element.

    Args:
        xml: The XML element to create the link from.

    Returns:
        The link created from the XML element.

    Examples:
        >>> xml = ET.Element('link')
        >>> Link.from_xml(xml)
        Link(name='link', visual=None, collision=None, inertial=None)
    """
    name = xml.get("name")

    visual_element = xml.find("visual")
    visual = VisualLink.from_xml(visual_element) if visual_element is not None else None

    collision_element = xml.find("collision")
    collision = CollisionLink.from_xml(collision_element) if collision_element is not None else None

    inertial_element = xml.find("inertial")
    inertial = InertialLink.from_xml(inertial_element) if inertial_element is not None else None

    return cls(name=name, visual=visual, collision=collision, inertial=inertial)

to_mjcf(root=None)

Convert the link to an MuJoCo compatible XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the link to.

None

Returns:

Type Description
Element

The XML element representing the link.

Examples:

>>> link = Link(
...     name="base_link",
...     visual=VisualLink(...),
...     collision=CollisionLink(...),
...     inertial=InertialLink(...)
... )
>>> link.to_mjcf()
<Element 'link' at 0x...>
Source code in onshape_robotics_toolkit\models\link.py
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
def to_mjcf(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the link to an MuJoCo compatible XML element.

    Args:
        root: The root element to append the link to.

    Returns:
        The XML element representing the link.

    Examples:
        >>> link = Link(
        ...     name="base_link",
        ...     visual=VisualLink(...),
        ...     collision=CollisionLink(...),
        ...     inertial=InertialLink(...)
        ... )
        >>> link.to_mjcf()
        <Element 'link' at 0x...>
    """
    link = ET.Element("body") if root is None else ET.SubElement(root, "body")
    link.set("name", self.name)

    if self.visual:
        link.set("pos", " ".join(map(str, self.visual.origin.xyz)))
        link.set("euler", " ".join(map(str, self.visual.origin.rpy)))

    if self.collision:
        self.collision.to_mjcf(link)

    if self.visual:
        self.visual.to_mjcf(link)

    if self.inertial:
        self.inertial.to_mjcf(link)

    return link

to_xml(root=None)

Convert the link to an XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the link to.

None

Returns:

Type Description
Element

The XML element representing the link.

Examples:

>>> link = Link(
...     name="base_link",
...     visual=VisualLink(...),
...     collision=CollisionLink(...),
...     inertial=InertialLink(...)
... )
>>> link.to_xml()
<Element 'link' at 0x...>
Source code in onshape_robotics_toolkit\models\link.py
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the link to an XML element.

    Args:
        root: The root element to append the link to.

    Returns:
        The XML element representing the link.

    Examples:
        >>> link = Link(
        ...     name="base_link",
        ...     visual=VisualLink(...),
        ...     collision=CollisionLink(...),
        ...     inertial=InertialLink(...)
        ... )
        >>> link.to_xml()
        <Element 'link' at 0x...>
    """
    link = ET.Element("link") if root is None else ET.SubElement(root, "link")
    link.set("name", self.name)
    if self.visual is not None:
        self.visual.to_xml(link)
    if self.collision is not None:
        self.collision.to_xml(link)
    if self.inertial is not None:
        self.inertial.to_xml(link)
    return link

Material dataclass

Represents the material properties of a link in the robot model.

Materials define the visual appearance of links in the robot model, primarily through their color properties.

Attributes:

Name Type Description
name str

The name identifier for the material.

color tuple[float, float, float, float]

The RGBA color values, each component in range [0.0, 1.0].

Methods:

Name Description
to_xml

Converts the material properties to an XML element.

to_mjcf

Converts the material to a MuJoCo compatible XML element.

Class Methods

from_xml: Creates a material from an XML element. from_color: Creates a material from a predefined Colors enum value.

Examples:

>>> material = Material(name="red_material", color=(1.0, 0.0, 0.0, 1.0))
>>> material.to_xml()
<Element 'material' at 0x...>
>>> material = Material.from_color("steel_material", Colors.BLUE)
>>> material.color
(0.0, 0.0, 1.0, 1.0)
Source code in onshape_robotics_toolkit\models\link.py
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
@dataclass
class Material:
    """
    Represents the material properties of a link in the robot model.

    Materials define the visual appearance of links in the robot model,
    primarily through their color properties.

    Attributes:
        name (str): The name identifier for the material.
        color (tuple[float, float, float, float]): The RGBA color values,
            each component in range [0.0, 1.0].

    Methods:
        to_xml: Converts the material properties to an XML element.
        to_mjcf: Converts the material to a MuJoCo compatible XML element.

    Class Methods:
        from_xml: Creates a material from an XML element.
        from_color: Creates a material from a predefined Colors enum value.

    Examples:
        >>> material = Material(name="red_material", color=(1.0, 0.0, 0.0, 1.0))
        >>> material.to_xml()
        <Element 'material' at 0x...>

        >>> material = Material.from_color("steel_material", Colors.BLUE)
        >>> material.color
        (0.0, 0.0, 1.0, 1.0)
    """

    name: str
    color: tuple[float, float, float, float]

    def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the material properties to an XML element.

        Args:
            root: The root element to append the material properties to.

        Returns:
            The XML element representing the material properties.

        Examples:
            >>> material = Material(name="material", color=(1.0, 0.0, 0.0, 1.0))
            >>> material.to_xml()
            <Element 'material' at 0x7f8b3c0b4c70>
        """

        material = ET.Element("material") if root is None else ET.SubElement(root, "material")
        material.set("name", self.name)
        ET.SubElement(material, "color", rgba=" ".join(format_number(v) for v in self.color))
        return material

    def to_mjcf(self, root: ET.Element) -> None:
        """
        Convert the material properties to an MuJoCo compatible XML element.

        Args:
            root: The root element to append the material properties to.

        Returns:
            The XML element representing the material properties.

        Examples:
            >>> material = Material(name="material", color=(1.0, 0.0, 0.0, 1.0))
            >>> material.to_mjcf()
            <Element 'material' at 0x7f8b3c0b4c70>
        """
        geom = root if root is not None and root.tag == "geom" else ET.SubElement(root, "geom")
        geom.set("rgba", " ".join(format_number(v) for v in self.color))

    @classmethod
    def from_xml(cls, xml: ET.Element) -> "Material":
        """
        Create a material from an XML element.

        Args:
            xml: The XML element to create the material from.

        Returns:
            The material created from the XML element.

        Examples:
            >>> xml = ET.Element('material')
            >>> Material.from_xml(xml)
            Material(name='material', color=(1.0, 0.0, 0.0, 1.0))
        """

        name = xml.get("name")
        color = tuple(map(float, xml.find("color").get("rgba").split()))
        return cls(name, color)

    @classmethod
    def from_color(cls, name: str, color: Colors) -> "Material":
        """
        Create a material from a color.

        Args:
            name: The name of the material.
            color: The color of the material.

        Returns:
            The material created from the color.

        Examples:
            >>> Material.from_color(name="material", color=Colors.RED)
            Material(name='material', color=(1.0, 0.0, 0.0, 1.0))
        """
        return cls(name, color)

from_color(name, color) classmethod

Create a material from a color.

Parameters:

Name Type Description Default
name str

The name of the material.

required
color Colors

The color of the material.

required

Returns:

Type Description
Material

The material created from the color.

Examples:

>>> Material.from_color(name="material", color=Colors.RED)
Material(name='material', color=(1.0, 0.0, 0.0, 1.0))
Source code in onshape_robotics_toolkit\models\link.py
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
@classmethod
def from_color(cls, name: str, color: Colors) -> "Material":
    """
    Create a material from a color.

    Args:
        name: The name of the material.
        color: The color of the material.

    Returns:
        The material created from the color.

    Examples:
        >>> Material.from_color(name="material", color=Colors.RED)
        Material(name='material', color=(1.0, 0.0, 0.0, 1.0))
    """
    return cls(name, color)

from_xml(xml) classmethod

Create a material from an XML element.

Parameters:

Name Type Description Default
xml Element

The XML element to create the material from.

required

Returns:

Type Description
Material

The material created from the XML element.

Examples:

>>> xml = ET.Element('material')
>>> Material.from_xml(xml)
Material(name='material', color=(1.0, 0.0, 0.0, 1.0))
Source code in onshape_robotics_toolkit\models\link.py
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
@classmethod
def from_xml(cls, xml: ET.Element) -> "Material":
    """
    Create a material from an XML element.

    Args:
        xml: The XML element to create the material from.

    Returns:
        The material created from the XML element.

    Examples:
        >>> xml = ET.Element('material')
        >>> Material.from_xml(xml)
        Material(name='material', color=(1.0, 0.0, 0.0, 1.0))
    """

    name = xml.get("name")
    color = tuple(map(float, xml.find("color").get("rgba").split()))
    return cls(name, color)

to_mjcf(root)

Convert the material properties to an MuJoCo compatible XML element.

Parameters:

Name Type Description Default
root Element

The root element to append the material properties to.

required

Returns:

Type Description
None

The XML element representing the material properties.

Examples:

>>> material = Material(name="material", color=(1.0, 0.0, 0.0, 1.0))
>>> material.to_mjcf()
<Element 'material' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
def to_mjcf(self, root: ET.Element) -> None:
    """
    Convert the material properties to an MuJoCo compatible XML element.

    Args:
        root: The root element to append the material properties to.

    Returns:
        The XML element representing the material properties.

    Examples:
        >>> material = Material(name="material", color=(1.0, 0.0, 0.0, 1.0))
        >>> material.to_mjcf()
        <Element 'material' at 0x7f8b3c0b4c70>
    """
    geom = root if root is not None and root.tag == "geom" else ET.SubElement(root, "geom")
    geom.set("rgba", " ".join(format_number(v) for v in self.color))

to_xml(root=None)

Convert the material properties to an XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the material properties to.

None

Returns:

Type Description
Element

The XML element representing the material properties.

Examples:

>>> material = Material(name="material", color=(1.0, 0.0, 0.0, 1.0))
>>> material.to_xml()
<Element 'material' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the material properties to an XML element.

    Args:
        root: The root element to append the material properties to.

    Returns:
        The XML element representing the material properties.

    Examples:
        >>> material = Material(name="material", color=(1.0, 0.0, 0.0, 1.0))
        >>> material.to_xml()
        <Element 'material' at 0x7f8b3c0b4c70>
    """

    material = ET.Element("material") if root is None else ET.SubElement(root, "material")
    material.set("name", self.name)
    ET.SubElement(material, "color", rgba=" ".join(format_number(v) for v in self.color))
    return material

Origin dataclass

Represents the origin of a link in the robot model.

Attributes:

Name Type Description
xyz tuple[float, float, float]

The x, y, z coordinates of the origin.

rpy tuple[float, float, float]

The roll, pitch, yaw angles of the origin.

Methods:

Name Description
transform

Applies a transformation matrix to the origin.

to_xml

Converts the origin to an XML element.

to_mjcf

Converts the origin to a MuJoCo compatible XML element.

quat

Converts the origin's rotation to a quaternion.

Class Methods

from_xml: Creates an origin from an XML element. from_matrix: Creates an origin from a transformation matrix. zero_origin: Creates an origin at (0, 0, 0) with no rotation.

Examples:

>>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
>>> origin.to_xml()
<Element 'origin' at 0x7f8b3c0b4c70>
>>> matrix = np.matrix([
...     [1, 0, 0, 0],
...     [0, 1, 0, 0],
...     [0, 0, 1, 0],
...     [0, 0, 0, 1],
... ])
>>> Origin.from_matrix(matrix)
Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
Source code in onshape_robotics_toolkit\models\link.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
@dataclass
class Origin:
    """
    Represents the origin of a link in the robot model.

    Attributes:
        xyz (tuple[float, float, float]): The x, y, z coordinates of the origin.
        rpy (tuple[float, float, float]): The roll, pitch, yaw angles of the origin.

    Methods:
        transform: Applies a transformation matrix to the origin.
        to_xml: Converts the origin to an XML element.
        to_mjcf: Converts the origin to a MuJoCo compatible XML element.
        quat: Converts the origin's rotation to a quaternion.

    Class Methods:
        from_xml: Creates an origin from an XML element.
        from_matrix: Creates an origin from a transformation matrix.
        zero_origin: Creates an origin at (0, 0, 0) with no rotation.

    Examples:
        >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
        >>> origin.to_xml()
        <Element 'origin' at 0x7f8b3c0b4c70>

        >>> matrix = np.matrix([
        ...     [1, 0, 0, 0],
        ...     [0, 1, 0, 0],
        ...     [0, 0, 1, 0],
        ...     [0, 0, 0, 1],
        ... ])
        >>> Origin.from_matrix(matrix)
        Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
    """

    xyz: tuple[float, float, float]
    rpy: tuple[float, float, float]

    def transform(self, matrix: np.matrix, inplace: bool = False) -> Union["Origin", None]:
        """
        Apply a transformation matrix to the origin.

        Args:
            matrix (np.matrix): The 4x4 transformation matrix to apply.
            inplace (bool): If True, modifies the current origin. If False, returns a new Origin.

        Returns:
            Union[Origin, None]: If inplace is False, returns a new transformed Origin. 
                               If inplace is True, returns None and modifies current Origin.

        Examples:
            >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
            >>> matrix = np.eye(4)
            >>> new_origin = origin.transform(matrix)  # Returns new Origin
            >>> origin.transform(matrix, inplace=True)  # Modifies origin in place
        """
        new_xyz = np.dot(matrix[:3, :3], np.array(self.xyz)) + matrix[:3, 3]
        current_rotation_matrix = Rotation.from_euler("xyz", self.rpy).as_matrix()

        new_rotation_matrix = np.dot(matrix[:3, :3], current_rotation_matrix)
        new_rpy = Rotation.from_matrix(new_rotation_matrix).as_euler("xyz")
        if inplace:
            self.xyz = tuple(new_xyz)
            self.rpy = tuple(new_rpy)
            return None

        return Origin(new_xyz, new_rpy)

    def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the origin to an XML element.

        Args:
            root: The root element to append the origin to.

        Returns:
            The XML element representing the origin.

        Examples:
            >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
            >>> origin.to_xml()
            <Element 'origin' at 0x7f8b3c0b4c70>
        """

        origin = ET.Element("origin") if root is None else ET.SubElement(root, "origin")
        origin.set("xyz", " ".join(format_number(v) for v in self.xyz))
        origin.set("rpy", " ".join(format_number(v) for v in self.rpy))
        return origin

    def to_mjcf(self, root: ET.Element) -> None:
        """
        Convert the origin to a MuJoCo compatible XML element.

        Args:
            root (ET.Element): The root element to add the origin attributes to.
                             Adds 'pos' and 'euler' attributes to this element.

        Examples:
            >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
            >>> element = ET.Element('body')
            >>> origin.to_mjcf(element)
            >>> element.get('pos')
            '1.0 2.0 3.0'
            >>> element.get('euler')
            '0.0 0.0 0.0'
        """
        root.set("pos", " ".join(format_number(v) for v in self.xyz))
        root.set("euler", " ".join(format_number(v) for v in self.rpy))

    @classmethod
    def from_xml(cls, xml: ET.Element) -> "Origin":
        """
        Create an origin from an XML element.

        Args:
            xml: The XML element to create the origin from.

        Returns:
            The origin created from the XML element.

        Examples:
            >>> xml = ET.Element('origin')
            >>> Origin.from_xml(xml)
            Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
        """

        xyz = tuple(map(float, xml.get("xyz").split()))
        rpy = tuple(map(float, xml.get("rpy").split()))
        return cls(xyz, rpy)

    def quat(self, sequence: str = "xyz") -> np.ndarray:
        """
        Convert the origin's rotation to a quaternion.

        Args:
            sequence (str): The sequence of rotations used for Euler angles. Defaults to "xyz".

        Returns:
            np.ndarray: A quaternion [x, y, z, w] representing the rotation.

        Examples:
            >>> origin = Origin(xyz=(0.0, 0.0, 0.0), rpy=(np.pi/2, 0.0, 0.0))
            >>> origin.quat()
            array([0.70710678, 0.        , 0.        , 0.70710678])
        """
        return Rotation.from_euler(sequence, self.rpy).as_quat()

    @classmethod
    def from_matrix(cls, matrix: np.matrix) -> "Origin":
        """
        Create an origin from a transformation matrix.

        Args:
            matrix: The transformation matrix.

        Returns:
            The origin created from the transformation matrix.

        Examples:
            >>> matrix = np.matrix([
            ...     [1, 0, 0, 0],
            ...     [0, 1, 0, 0],
            ...     [0, 0, 1, 0],
            ...     [0, 0, 0, 1],
            ... ])
            >>> Origin.from_matrix(matrix)
            Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
        """

        x = float(matrix[0, 3])
        y = float(matrix[1, 3])
        z = float(matrix[2, 3])
        roll, pitch, yaw = Rotation.from_matrix(matrix[:3, :3]).as_euler("xyz")
        return cls((x, y, z), (roll, pitch, yaw))

    @classmethod
    def zero_origin(cls) -> "Origin":
        """
        Create an origin at the origin (0, 0, 0) with no rotation.

        Returns:
            The origin at the origin (0, 0, 0) with no rotation.

        Examples:
            >>> Origin.zero_origin()
            Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
        """

        return cls((0.0, 0.0, 0.0), (0.0, 0.0, 0.0))

from_matrix(matrix) classmethod

Create an origin from a transformation matrix.

Parameters:

Name Type Description Default
matrix matrix

The transformation matrix.

required

Returns:

Type Description
Origin

The origin created from the transformation matrix.

Examples:

>>> matrix = np.matrix([
...     [1, 0, 0, 0],
...     [0, 1, 0, 0],
...     [0, 0, 1, 0],
...     [0, 0, 0, 1],
... ])
>>> Origin.from_matrix(matrix)
Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
Source code in onshape_robotics_toolkit\models\link.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
@classmethod
def from_matrix(cls, matrix: np.matrix) -> "Origin":
    """
    Create an origin from a transformation matrix.

    Args:
        matrix: The transformation matrix.

    Returns:
        The origin created from the transformation matrix.

    Examples:
        >>> matrix = np.matrix([
        ...     [1, 0, 0, 0],
        ...     [0, 1, 0, 0],
        ...     [0, 0, 1, 0],
        ...     [0, 0, 0, 1],
        ... ])
        >>> Origin.from_matrix(matrix)
        Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
    """

    x = float(matrix[0, 3])
    y = float(matrix[1, 3])
    z = float(matrix[2, 3])
    roll, pitch, yaw = Rotation.from_matrix(matrix[:3, :3]).as_euler("xyz")
    return cls((x, y, z), (roll, pitch, yaw))

from_xml(xml) classmethod

Create an origin from an XML element.

Parameters:

Name Type Description Default
xml Element

The XML element to create the origin from.

required

Returns:

Type Description
Origin

The origin created from the XML element.

Examples:

>>> xml = ET.Element('origin')
>>> Origin.from_xml(xml)
Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
Source code in onshape_robotics_toolkit\models\link.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
@classmethod
def from_xml(cls, xml: ET.Element) -> "Origin":
    """
    Create an origin from an XML element.

    Args:
        xml: The XML element to create the origin from.

    Returns:
        The origin created from the XML element.

    Examples:
        >>> xml = ET.Element('origin')
        >>> Origin.from_xml(xml)
        Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
    """

    xyz = tuple(map(float, xml.get("xyz").split()))
    rpy = tuple(map(float, xml.get("rpy").split()))
    return cls(xyz, rpy)

quat(sequence='xyz')

Convert the origin's rotation to a quaternion.

Parameters:

Name Type Description Default
sequence str

The sequence of rotations used for Euler angles. Defaults to "xyz".

'xyz'

Returns:

Type Description
ndarray

np.ndarray: A quaternion [x, y, z, w] representing the rotation.

Examples:

>>> origin = Origin(xyz=(0.0, 0.0, 0.0), rpy=(np.pi/2, 0.0, 0.0))
>>> origin.quat()
array([0.70710678, 0.        , 0.        , 0.70710678])
Source code in onshape_robotics_toolkit\models\link.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
def quat(self, sequence: str = "xyz") -> np.ndarray:
    """
    Convert the origin's rotation to a quaternion.

    Args:
        sequence (str): The sequence of rotations used for Euler angles. Defaults to "xyz".

    Returns:
        np.ndarray: A quaternion [x, y, z, w] representing the rotation.

    Examples:
        >>> origin = Origin(xyz=(0.0, 0.0, 0.0), rpy=(np.pi/2, 0.0, 0.0))
        >>> origin.quat()
        array([0.70710678, 0.        , 0.        , 0.70710678])
    """
    return Rotation.from_euler(sequence, self.rpy).as_quat()

to_mjcf(root)

Convert the origin to a MuJoCo compatible XML element.

Parameters:

Name Type Description Default
root Element

The root element to add the origin attributes to. Adds 'pos' and 'euler' attributes to this element.

required

Examples:

>>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
>>> element = ET.Element('body')
>>> origin.to_mjcf(element)
>>> element.get('pos')
'1.0 2.0 3.0'
>>> element.get('euler')
'0.0 0.0 0.0'
Source code in onshape_robotics_toolkit\models\link.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
def to_mjcf(self, root: ET.Element) -> None:
    """
    Convert the origin to a MuJoCo compatible XML element.

    Args:
        root (ET.Element): The root element to add the origin attributes to.
                         Adds 'pos' and 'euler' attributes to this element.

    Examples:
        >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
        >>> element = ET.Element('body')
        >>> origin.to_mjcf(element)
        >>> element.get('pos')
        '1.0 2.0 3.0'
        >>> element.get('euler')
        '0.0 0.0 0.0'
    """
    root.set("pos", " ".join(format_number(v) for v in self.xyz))
    root.set("euler", " ".join(format_number(v) for v in self.rpy))

to_xml(root=None)

Convert the origin to an XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the origin to.

None

Returns:

Type Description
Element

The XML element representing the origin.

Examples:

>>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
>>> origin.to_xml()
<Element 'origin' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the origin to an XML element.

    Args:
        root: The root element to append the origin to.

    Returns:
        The XML element representing the origin.

    Examples:
        >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
        >>> origin.to_xml()
        <Element 'origin' at 0x7f8b3c0b4c70>
    """

    origin = ET.Element("origin") if root is None else ET.SubElement(root, "origin")
    origin.set("xyz", " ".join(format_number(v) for v in self.xyz))
    origin.set("rpy", " ".join(format_number(v) for v in self.rpy))
    return origin

transform(matrix, inplace=False)

Apply a transformation matrix to the origin.

Parameters:

Name Type Description Default
matrix matrix

The 4x4 transformation matrix to apply.

required
inplace bool

If True, modifies the current origin. If False, returns a new Origin.

False

Returns:

Type Description
Union[Origin, None]

Union[Origin, None]: If inplace is False, returns a new transformed Origin. If inplace is True, returns None and modifies current Origin.

Examples:

>>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
>>> matrix = np.eye(4)
>>> new_origin = origin.transform(matrix)  # Returns new Origin
>>> origin.transform(matrix, inplace=True)  # Modifies origin in place
Source code in onshape_robotics_toolkit\models\link.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def transform(self, matrix: np.matrix, inplace: bool = False) -> Union["Origin", None]:
    """
    Apply a transformation matrix to the origin.

    Args:
        matrix (np.matrix): The 4x4 transformation matrix to apply.
        inplace (bool): If True, modifies the current origin. If False, returns a new Origin.

    Returns:
        Union[Origin, None]: If inplace is False, returns a new transformed Origin. 
                           If inplace is True, returns None and modifies current Origin.

    Examples:
        >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
        >>> matrix = np.eye(4)
        >>> new_origin = origin.transform(matrix)  # Returns new Origin
        >>> origin.transform(matrix, inplace=True)  # Modifies origin in place
    """
    new_xyz = np.dot(matrix[:3, :3], np.array(self.xyz)) + matrix[:3, 3]
    current_rotation_matrix = Rotation.from_euler("xyz", self.rpy).as_matrix()

    new_rotation_matrix = np.dot(matrix[:3, :3], current_rotation_matrix)
    new_rpy = Rotation.from_matrix(new_rotation_matrix).as_euler("xyz")
    if inplace:
        self.xyz = tuple(new_xyz)
        self.rpy = tuple(new_rpy)
        return None

    return Origin(new_xyz, new_rpy)

zero_origin() classmethod

Create an origin at the origin (0, 0, 0) with no rotation.

Returns:

Type Description
Origin

The origin at the origin (0, 0, 0) with no rotation.

Examples:

>>> Origin.zero_origin()
Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
Source code in onshape_robotics_toolkit\models\link.py
250
251
252
253
254
255
256
257
258
259
260
261
262
263
@classmethod
def zero_origin(cls) -> "Origin":
    """
    Create an origin at the origin (0, 0, 0) with no rotation.

    Returns:
        The origin at the origin (0, 0, 0) with no rotation.

    Examples:
        >>> Origin.zero_origin()
        Origin(xyz=(0.0, 0.0, 0.0), rpy=(0.0, 0.0, 0.0))
    """

    return cls((0.0, 0.0, 0.0), (0.0, 0.0, 0.0))

Represents the visual properties of a link in the robot model.

This class defines how a link appears in visualization tools and simulators, including its position, geometry, and material properties.

Attributes:

Name Type Description
name Union[str, None]

Optional name identifier for the visual element.

origin Origin

The position and orientation of the visual geometry.

geometry BaseGeometry

The shape of the visual element (box, cylinder, mesh, etc.).

material Material

The material properties (color, texture) of the visual element.

Methods:

Name Description
to_xml

Converts the visual properties to an XML element.

to_mjcf

Converts the visual properties to a MuJoCo compatible XML element.

transform

Applies a transformation matrix to the visual geometry's origin.

Class Methods

from_xml: Creates a VisualLink from an XML element.

Examples:

>>> visual = VisualLink(
...     name="link_visual",
...     origin=Origin.zero_origin(),
...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0)),
...     material=Material.from_color("red", Colors.RED)
... )
>>> visual.to_xml()
<Element 'visual' at 0x...>
Source code in onshape_robotics_toolkit\models\link.py
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
@dataclass
class VisualLink:
    """
    Represents the visual properties of a link in the robot model.

    This class defines how a link appears in visualization tools and simulators,
    including its position, geometry, and material properties.

    Attributes:
        name (Union[str, None]): Optional name identifier for the visual element.
        origin (Origin): The position and orientation of the visual geometry.
        geometry (BaseGeometry): The shape of the visual element (box, cylinder, mesh, etc.).
        material (Material): The material properties (color, texture) of the visual element.

    Methods:
        to_xml: Converts the visual properties to an XML element.
        to_mjcf: Converts the visual properties to a MuJoCo compatible XML element.
        transform: Applies a transformation matrix to the visual geometry's origin.

    Class Methods:
        from_xml: Creates a VisualLink from an XML element.

    Examples:
        >>> visual = VisualLink(
        ...     name="link_visual",
        ...     origin=Origin.zero_origin(),
        ...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0)),
        ...     material=Material.from_color("red", Colors.RED)
        ... )
        >>> visual.to_xml()
        <Element 'visual' at 0x...>
    """

    name: Union[str, None]
    origin: Origin
    geometry: BaseGeometry
    material: Material

    def transform(self, transformation_matrix: np.ndarray) -> None:
        """
        Apply a transformation to the visual link's origin.

        Args:
            transformation_matrix (np.ndarray): A 4x4 transformation matrix (homogeneous).
        """
        # Apply translation and rotation to the origin position
        pos = np.array([self.origin.xyz[0], self.origin.xyz[1], self.origin.xyz[2], 1])
        new_pos = transformation_matrix @ pos
        self.origin.xyz = tuple(new_pos[:3])  # Update position

        # Extract the rotation from the transformation matrix
        rotation_matrix = transformation_matrix[:3, :3]
        current_rotation = R.from_euler("xyz", self.origin.rpy)
        new_rotation = R.from_matrix(rotation_matrix @ current_rotation.as_matrix())
        self.origin.rpy = new_rotation.as_euler("xyz").tolist()

    def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
        """
        Convert the visual properties to an XML element.

        Args:
            root: The root element to append the visual properties to.

        Returns:
            The XML element representing the visual properties.

        Examples:
            >>> visual = VisualLink(
            ...     name="link_visual",
            ...     origin=Origin.zero_origin(),
            ...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0)),
            ...     material=Material.from_color("red", Colors.RED)
            ... )
            >>> visual.to_xml()
            <Element 'visual' at 0x...>
        """
        visual = ET.Element("visual") if root is None else ET.SubElement(root, "visual")
        if self.name:
            visual.set("name", self.name)
        self.origin.to_xml(visual)
        self.geometry.to_xml(visual)
        self.material.to_xml(visual)
        return visual

    def to_mjcf(self, root: ET.Element) -> None:
        """
        Convert the visual properties to an MuJoCo compatible XML element.

        Args:
            root: The root element to append the visual properties to.

        Returns:
            The XML element representing the visual properties.

        Examples:
            >>> visual = VisualLink(
            ...     name="link_visual",
            ...     origin=Origin.zero_origin(),
            ...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0)),
            ...     material=Material.from_color("red", Colors.RED)
            ... )
            >>> visual.to_mjcf()
            <Element 'visual' at 0x...>
        """
        visual = root if root.tag == "geom" else ET.SubElement(root, "geom")
        if self.name:
            visual.set("name", self.name)
        # TODO: Parent body uses visual origin, these share the same?
        self.origin.to_mjcf(visual)

        if self.geometry:
            self.geometry.to_mjcf(visual)

        self.material.to_mjcf(visual)

        visual.set("conaffinity", "0")
        visual.set("condim", "1")
        visual.set("contype", "0")
        visual.set("density", "0")
        visual.set("group", "1")

    @classmethod
    def from_xml(cls, xml: ET.Element) -> "VisualLink":
        """
        Create a visual link from an XML element.

        Args:
            xml: The XML element to create the visual link from.

        Returns:
            The visual link created from the XML element.

        Examples:
            >>> xml = ET.Element('visual')
            >>> VisualLink.from_xml(xml)
            VisualLink(name='visual', origin=None, geometry=None, material=None)
        """
        name = xml.get("name")
        origin_element = xml.find("origin")
        origin = Origin.from_xml(origin_element) if origin_element is not None else None

        geometry_element = xml.find("geometry")
        geometry = set_geometry_from_xml(geometry_element) if geometry_element is not None else None

        material_element = xml.find("material")
        material = Material.from_xml(material_element) if material_element is not None else None
        return cls(name=name, origin=origin, geometry=geometry, material=material)

from_xml(xml) classmethod

Create a visual link from an XML element.

Parameters:

Name Type Description Default
xml Element

The XML element to create the visual link from.

required

Returns:

Type Description
VisualLink

The visual link created from the XML element.

Examples:

>>> xml = ET.Element('visual')
>>> VisualLink.from_xml(xml)
VisualLink(name='visual', origin=None, geometry=None, material=None)
Source code in onshape_robotics_toolkit\models\link.py
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
@classmethod
def from_xml(cls, xml: ET.Element) -> "VisualLink":
    """
    Create a visual link from an XML element.

    Args:
        xml: The XML element to create the visual link from.

    Returns:
        The visual link created from the XML element.

    Examples:
        >>> xml = ET.Element('visual')
        >>> VisualLink.from_xml(xml)
        VisualLink(name='visual', origin=None, geometry=None, material=None)
    """
    name = xml.get("name")
    origin_element = xml.find("origin")
    origin = Origin.from_xml(origin_element) if origin_element is not None else None

    geometry_element = xml.find("geometry")
    geometry = set_geometry_from_xml(geometry_element) if geometry_element is not None else None

    material_element = xml.find("material")
    material = Material.from_xml(material_element) if material_element is not None else None
    return cls(name=name, origin=origin, geometry=geometry, material=material)

to_mjcf(root)

Convert the visual properties to an MuJoCo compatible XML element.

Parameters:

Name Type Description Default
root Element

The root element to append the visual properties to.

required

Returns:

Type Description
None

The XML element representing the visual properties.

Examples:

>>> visual = VisualLink(
...     name="link_visual",
...     origin=Origin.zero_origin(),
...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0)),
...     material=Material.from_color("red", Colors.RED)
... )
>>> visual.to_mjcf()
<Element 'visual' at 0x...>
Source code in onshape_robotics_toolkit\models\link.py
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
def to_mjcf(self, root: ET.Element) -> None:
    """
    Convert the visual properties to an MuJoCo compatible XML element.

    Args:
        root: The root element to append the visual properties to.

    Returns:
        The XML element representing the visual properties.

    Examples:
        >>> visual = VisualLink(
        ...     name="link_visual",
        ...     origin=Origin.zero_origin(),
        ...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0)),
        ...     material=Material.from_color("red", Colors.RED)
        ... )
        >>> visual.to_mjcf()
        <Element 'visual' at 0x...>
    """
    visual = root if root.tag == "geom" else ET.SubElement(root, "geom")
    if self.name:
        visual.set("name", self.name)
    # TODO: Parent body uses visual origin, these share the same?
    self.origin.to_mjcf(visual)

    if self.geometry:
        self.geometry.to_mjcf(visual)

    self.material.to_mjcf(visual)

    visual.set("conaffinity", "0")
    visual.set("condim", "1")
    visual.set("contype", "0")
    visual.set("density", "0")
    visual.set("group", "1")

to_xml(root=None)

Convert the visual properties to an XML element.

Parameters:

Name Type Description Default
root Optional[Element]

The root element to append the visual properties to.

None

Returns:

Type Description
Element

The XML element representing the visual properties.

Examples:

>>> visual = VisualLink(
...     name="link_visual",
...     origin=Origin.zero_origin(),
...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0)),
...     material=Material.from_color("red", Colors.RED)
... )
>>> visual.to_xml()
<Element 'visual' at 0x...>
Source code in onshape_robotics_toolkit\models\link.py
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
def to_xml(self, root: Optional[ET.Element] = None) -> ET.Element:
    """
    Convert the visual properties to an XML element.

    Args:
        root: The root element to append the visual properties to.

    Returns:
        The XML element representing the visual properties.

    Examples:
        >>> visual = VisualLink(
        ...     name="link_visual",
        ...     origin=Origin.zero_origin(),
        ...     geometry=BoxGeometry(size=(1.0, 1.0, 1.0)),
        ...     material=Material.from_color("red", Colors.RED)
        ... )
        >>> visual.to_xml()
        <Element 'visual' at 0x...>
    """
    visual = ET.Element("visual") if root is None else ET.SubElement(root, "visual")
    if self.name:
        visual.set("name", self.name)
    self.origin.to_xml(visual)
    self.geometry.to_xml(visual)
    self.material.to_xml(visual)
    return visual

transform(transformation_matrix)

Apply a transformation to the visual link's origin.

Parameters:

Name Type Description Default
transformation_matrix ndarray

A 4x4 transformation matrix (homogeneous).

required
Source code in onshape_robotics_toolkit\models\link.py
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
def transform(self, transformation_matrix: np.ndarray) -> None:
    """
    Apply a transformation to the visual link's origin.

    Args:
        transformation_matrix (np.ndarray): A 4x4 transformation matrix (homogeneous).
    """
    # Apply translation and rotation to the origin position
    pos = np.array([self.origin.xyz[0], self.origin.xyz[1], self.origin.xyz[2], 1])
    new_pos = transformation_matrix @ pos
    self.origin.xyz = tuple(new_pos[:3])  # Update position

    # Extract the rotation from the transformation matrix
    rotation_matrix = transformation_matrix[:3, :3]
    current_rotation = R.from_euler("xyz", self.origin.rpy)
    new_rotation = R.from_matrix(rotation_matrix @ current_rotation.as_matrix())
    self.origin.rpy = new_rotation.as_euler("xyz").tolist()

set_geometry_from_xml(geometry)

Set the geometry from an XML element.

Parameters:

Name Type Description Default
geometry Element

The XML element to create the geometry from.

required

Returns:

Type Description
BaseGeometry | None

The geometry created from the XML element.

Source code in onshape_robotics_toolkit\models\link.py
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
def set_geometry_from_xml(geometry: ET.Element) -> BaseGeometry | None:
    """
    Set the geometry from an XML element.

    Args:
        geometry: The XML element to create the geometry from.

    Returns:
        The geometry created from the XML element.
    """
    if geometry.find("mesh") is not None:
        return MeshGeometry.from_xml(geometry)
    elif geometry.find("box"):
        return BoxGeometry.from_xml(geometry)
    elif geometry.find("length") and geometry.find("radius"):
        return CylinderGeometry.from_xml(geometry)
    elif geometry.find("radius"):
        return SphereGeometry.from_xml(geometry)

    return None