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 a link in the robot model.

Attributes:

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

The x, y, z coordinates of the axis.

Methods:

Name Description
to_xml

Converts the axis to an XML element.

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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
@dataclass
class Axis:
    """
    Represents the axis of a link in the robot model.

    Attributes:
        xyz (tuple[float, float, float]): The x, y, z coordinates of the axis.

    Methods:
        to_xml: Converts the axis to an XML element.

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

    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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
@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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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.

Attributes:

Name Type Description
origin Origin

The origin of the link.

geometry BaseGeometry

The geometry of the link.

Methods:

Name Description
to_xml

Converts the collision properties to an XML element.

Examples:

>>> collision = CollisionLink(origin=Origin(...), geometry=BoxGeometry(...))
>>> collision.to_xml()
<Element 'collision' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
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
@dataclass
class CollisionLink:
    """
    Represents the collision properties of a link in the robot model.

    Attributes:
        origin (Origin): The origin of the link.
        geometry (BaseGeometry): The geometry of the link.

    Methods:
        to_xml: Converts the collision properties to an XML element.

    Examples:
        >>> collision = CollisionLink(origin=Origin(...), geometry=BoxGeometry(...))
        >>> collision.to_xml()
        <Element 'collision' at 0x7f8b3c0b4c70>
    """

    name: str
    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")
        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")
        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
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
@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
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
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")
    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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
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")
    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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
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.

Attributes:

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

Color red.

GREEN tuple[float, float, float]

Color green.

BLUE tuple[float, float, float]

Color blue.

YELLOW tuple[float, float, float]

Color yellow.

CYAN tuple[float, float, float]

Color cyan.

MAGENTA tuple[float, float, float]

Color magenta.

WHITE tuple[float, float, float]

Color white.

BLACK tuple[float, float, float]

Color black.

ORANGE tuple[float, float, float]

Color orange.

PINK tuple[float, float, float]

Color pink.

Examples:

>>> Colors.RED
<Colors.RED: (1.0, 0.0, 0.0)>
Source code in onshape_robotics_toolkit\models\link.py
31
32
33
34
35
36
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
class Colors(tuple[float, float, float], Enum):
    """
    Enumerates the possible colors in RGBA format for a link in the robot model.

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

    Examples:
        >>> Colors.RED
        <Colors.RED: (1.0, 0.0, 0.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.

Attributes:

Name Type Description
ixx float

The inertia tensor element Ixx.

iyy float

The inertia tensor element Iyy.

izz float

The inertia tensor element Izz.

ixy float

The inertia tensor element Ixy.

ixz float

The inertia tensor element Ixz.

iyz float

The inertia tensor element Iyz.

Methods:

Name Description
to_xml

Converts the inertia tensor to an XML element.

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
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
351
352
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
@dataclass
class Inertia:
    """
    Represents the inertia tensor of a link in the robot model.

    Attributes:
        ixx (float): The inertia tensor element Ixx.
        iyy (float): The inertia tensor element Iyy.
        izz (float): The inertia tensor element Izz.
        ixy (float): The inertia tensor element Ixy.
        ixz (float): The inertia tensor element Ixz.
        iyz (float): The inertia tensor element Iyz.

    Methods:
        to_xml: Converts the inertia tensor to an XML element.

    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>
    """

    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)

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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
@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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
417
418
419
420
421
422
423
424
425
426
427
428
429
@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.

Attributes:

Name Type Description
mass float

The mass of the link.

inertia Inertia

The inertia properties of the link.

origin Origin

The origin of the link.

Methods:

Name Description
to_xml

Converts the inertial properties to an XML element.

Examples:

>>> inertial = InertialLink(mass=1.0, inertia=Inertia(...), origin=Origin(...))
>>> inertial.to_xml()
<Element 'inertial' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
@dataclass
class InertialLink:
    """
    Represents the inertial properties of a link in the robot model.

    Attributes:
        mass (float): The mass of the link.
        inertia (Inertia): The inertia properties of the link.
        origin (Origin): The origin of the link.

    Methods:
        to_xml: Converts the inertial properties to an XML element.

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

    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(...), origin=Origin(...))
            >>> inertial.to_xml()
            <Element 'inertial' at 0x7f8b3c0b4c70>
        """
        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)

    @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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
@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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
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(...), origin=Origin(...))
>>> inertial.to_xml()
<Element 'inertial' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
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(...), origin=Origin(...))
        >>> inertial.to_xml()
        <Element 'inertial' at 0x7f8b3c0b4c70>
    """
    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

Represents a link in the robot model.

Attributes:

Name Type Description
name str

The name of the link.

visual VisualLink

The visual properties of the link.

collision CollisionLink

The collision properties of the link.

inertial InertialLink

The inertial properties of the link.

Methods:

Name Description
to_xml

Converts the link to an XML element.

Class Methods

from_xml: Creates a link from an XML element.

Examples:

>>> link = Link(name="link", visual=VisualLink(...), collision=CollisionLink(...), inertial=InertialLink(...))
>>> link.to_xml()
<Element 'link' at 0x7f8b3c0b4c70>
>>> part = Part(...)
>>> Link.from_part(part)
Link(name='partId', visual=None, collision=None, inertial=None)
Source code in onshape_robotics_toolkit\models\link.py
 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
 938
 939
 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
@dataclass
class Link:
    """
    Represents a link in the robot model.

    Attributes:
        name (str): The name of the link.
        visual (VisualLink): The visual properties of the link.
        collision (CollisionLink): The collision properties of the link.
        inertial (InertialLink): The inertial properties of the link.

    Methods:
        to_xml: Converts the link to an XML element.

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

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

        >>> part = Part(...)
        >>> Link.from_part(part)
        Link(name='partId', visual=None, collision=None, inertial=None)
    """

    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="link",
            ...     visual=VisualLink(...),
            ...     collision=CollisionLink(...),
            ...     inertial=InertialLink(...),
            ... )
            >>> link.to_xml()
            <Element 'link' at 0x7f8b3c0b4c70>
        """
        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="link",
            ...     visual=VisualLink(...),
            ...     collision=CollisionLink(...),
            ...     inertial=InertialLink(...),
            ... )
            >>> link.to_mjcf()
            <Element 'link' at 0x7f8b3c0b4c70>
        """
        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
 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
@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="link",
...     visual=VisualLink(...),
...     collision=CollisionLink(...),
...     inertial=InertialLink(...),
... )
>>> link.to_mjcf()
<Element 'link' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
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
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="link",
        ...     visual=VisualLink(...),
        ...     collision=CollisionLink(...),
        ...     inertial=InertialLink(...),
        ... )
        >>> link.to_mjcf()
        <Element 'link' at 0x7f8b3c0b4c70>
    """
    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="link",
...     visual=VisualLink(...),
...     collision=CollisionLink(...),
...     inertial=InertialLink(...),
... )
>>> link.to_xml()
<Element 'link' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
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="link",
        ...     visual=VisualLink(...),
        ...     collision=CollisionLink(...),
        ...     inertial=InertialLink(...),
        ... )
        >>> link.to_xml()
        <Element 'link' at 0x7f8b3c0b4c70>
    """
    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.

Attributes:

Name Type Description
name str

The name of the material.

color tuple[float, float, float, float]

The color of the material in RGBA format.

Methods:

Name Description
to_xml

Converts the material properties to an XML element.

Class Methods

from_color: Creates a material from a color.

Examples:

>>> material = Material(name="material", color=(1.0, 0.0, 0.0, 1.0))
>>> material.to_xml()
<Element 'material' at 0x7f8b3c0b4c70>
>>> 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
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
487
488
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
@dataclass
class Material:
    """
    Represents the material properties of a link in the robot model.

    Attributes:
        name (str): The name of the material.
        color (tuple[float, float, float, float]): The color of the material in RGBA format.

    Methods:
        to_xml: Converts the material properties to an XML element.

    Class Methods:
        from_color: Creates a material from a color.

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

        >>> Material.from_color(name="material", color=Colors.RED)
        Material(name='material', color=(1.0, 0.0, 0.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
@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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
@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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
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
to_xml

Converts the origin to an XML element.

Class Methods

from_matrix: Creates an origin from a transformation matrix. zero_origin: Creates an origin at the origin (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))
>>> 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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 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
@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:
        to_xml: Converts the origin to an XML element.

    Class Methods:
        from_matrix: Creates an origin from a transformation matrix.
        zero_origin: Creates an origin at the origin (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))

        >>> Origin.zero_origin()
        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: The transformation matrix to apply to the origin.
            inplace: Whether to apply the transformation in place.

        Returns:
            The origin with the transformation applied.

        Examples:
            >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
            >>> matrix = np.eye(4)
            >>> origin.transform(matrix)
        """
        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 an MuJoCo compatible 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_mjcf()
            <Element 'origin' at 0x7f8b3c0b4c70>
        """
        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") -> str:
        """
        Convert the origin to a quaternion.

        Args:
            sequence: The sequence of the Euler angles.

        Returns:
            The quaternion representing the origin.
        """
        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
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
@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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
@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 to a quaternion.

Parameters:

Name Type Description Default
sequence str

The sequence of the Euler angles.

'xyz'

Returns:

Type Description
str

The quaternion representing the origin.

Source code in onshape_robotics_toolkit\models\link.py
189
190
191
192
193
194
195
196
197
198
199
def quat(self, sequence: str = "xyz") -> str:
    """
    Convert the origin to a quaternion.

    Args:
        sequence: The sequence of the Euler angles.

    Returns:
        The quaternion representing the origin.
    """
    return Rotation.from_euler(sequence, self.rpy).as_quat()

to_mjcf(root)

Convert the origin to an MuJoCo compatible XML element.

Parameters:

Name Type Description Default
root Element

The root element to append the origin to.

required

Returns:

Type Description
None

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_mjcf()
<Element 'origin' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def to_mjcf(self, root: ET.Element) -> None:
    """
    Convert the origin to an MuJoCo compatible 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_mjcf()
        <Element 'origin' at 0x7f8b3c0b4c70>
    """
    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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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 transformation matrix to apply to the origin.

required
inplace bool

Whether to apply the transformation in place.

False

Returns:

Type Description
Union[Origin, None]

The origin with the transformation applied.

Examples:

>>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
>>> matrix = np.eye(4)
>>> origin.transform(matrix)
Source code in onshape_robotics_toolkit\models\link.py
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
def transform(self, matrix: np.matrix, inplace: bool = False) -> Union["Origin", None]:
    """
    Apply a transformation matrix to the origin.

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

    Returns:
        The origin with the transformation applied.

    Examples:
        >>> origin = Origin(xyz=(1.0, 2.0, 3.0), rpy=(0.0, 0.0, 0.0))
        >>> matrix = np.eye(4)
        >>> origin.transform(matrix)
    """
    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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
@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.

Attributes:

Name Type Description
origin Origin

The origin of the link.

geometry BaseGeometry

The geometry of the link.

material Material

The material properties of the link.

Methods:

Name Description
to_xml

Converts the visual properties to an XML element.

Examples:

>>> visual = VisualLink(origin=Origin(...), geometry=BoxGeometry(...), material=Material(...))
>>> visual.to_xml()
<Element 'visual' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
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
@dataclass
class VisualLink:
    """
    Represents the visual properties of a link in the robot model.

    Attributes:
        origin (Origin): The origin of the link.
        geometry (BaseGeometry): The geometry of the link.
        material (Material): The material properties of the link.

    Methods:
        to_xml: Converts the visual properties to an XML element.

    Examples:
        >>> visual = VisualLink(origin=Origin(...), geometry=BoxGeometry(...), material=Material(...))
        >>> visual.to_xml()
        <Element 'visual' at 0x7f8b3c0b4c70>
    """

    name: str
    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(origin=Origin(...), geometry=BoxGeometry(...), material=Material(...))
            >>> visual.to_xml()
            <Element 'visual' at 0x7f8b3c0b4c70>
        """
        visual = ET.Element("visual") if root is None else ET.SubElement(root, "visual")
        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(origin=Origin(...), geometry=BoxGeometry(...), material=Material(...))
            >>> visual.to_mjcf()
            <Element 'visual' at 0x7f8b3c0b4c70>
        """
        visual = root if root.tag == "geom" else ET.SubElement(root, "geom")
        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
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
@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(origin=Origin(...), geometry=BoxGeometry(...), material=Material(...))
>>> visual.to_mjcf()
<Element 'visual' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
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
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(origin=Origin(...), geometry=BoxGeometry(...), material=Material(...))
        >>> visual.to_mjcf()
        <Element 'visual' at 0x7f8b3c0b4c70>
    """
    visual = root if root.tag == "geom" else ET.SubElement(root, "geom")
    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(origin=Origin(...), geometry=BoxGeometry(...), material=Material(...))
>>> visual.to_xml()
<Element 'visual' at 0x7f8b3c0b4c70>
Source code in onshape_robotics_toolkit\models\link.py
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
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(origin=Origin(...), geometry=BoxGeometry(...), material=Material(...))
        >>> visual.to_xml()
        <Element 'visual' at 0x7f8b3c0b4c70>
    """
    visual = ET.Element("visual") if root is None else ET.SubElement(root, "visual")
    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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
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()