mmatl / urdfpy

Python parser for URDFs
http://urdfpy.readthedocs.io/
MIT License
224 stars 81 forks source link

Generating cylinder meshes #11

Closed unigamer closed 4 years ago

unigamer commented 4 years ago

https://github.com/mmatl/urdfpy/blob/08ff7bb8311e7a23bb93c75446d908af62844375/urdfpy/urdf.py#L402

The cylinder mesh creation is faulty in the current release version. Some of the bits reference self._meshes as being None but now they should be empty lists. Also, on line 402 it tries to return self._mesh instead of self._meshes.

Apologies for not making a proper pull request, it's not easy for me to do that in my current situation. Great library by the way, much appreciated.

Below is what I have changed the cylinder stuff to and it appears to work.

`class Cylinder(URDFType): """A cylinder whose center is at the local origin.

Parameters
----------
radius : float
    The radius of the cylinder in meters.
length : float
    The length of the cylinder in meters.
"""

_ATTRIBS = {
    'radius': (float, True),
    'length': (float, True),
}
_TAG = 'cylinder'

def __init__(self, radius, length):
    self.radius = radius
    self.length = length
    self._meshes = []

@property
def radius(self):
    """float : The radius of the cylinder in meters.
    """
    return self._radius

@radius.setter
def radius(self, value):
    self._radius = float(value)
    self._meshes = []

@property
def length(self):
    """float : The length of the cylinder in meters.
    """
    return self._length

@length.setter
def length(self, value):
    self._length = float(value)
    self._meshes = []

@property
def meshes(self):
    """list of :class:`~trimesh.base.Trimesh` : The triangular meshes
    that represent this object.
    """
    if len(self._meshes) == 0:
        self._meshes = [trimesh.creation.cylinder(
            radius=self.radius, height=self.length
        )]
    return self._meshes

def copy(self, prefix='', scale=None):
    """Create a deep copy with the prefix applied to all names.

    Parameters
    ----------
    prefix : str
        A prefix to apply to all names.

    Returns
    -------
    :class:`.Cylinder`
        A deep copy.
    """
    if scale is None:
        scale = 1.0
    if isinstance(scale, (list, np.ndarray)):
        if scale[0] != scale[1]:
            raise ValueError('Cannot rescale cylinder geometry with asymmetry in x/y')
        c = Cylinder(
            radius=self.radius * scale[0],
            length=self.length * scale[2],
        )
    else:
        c = Cylinder(
            radius=self.radius * scale,
            length=self.length * scale,
        )
    return c`
unigamer commented 4 years ago

https://github.com/mmatl/urdfpy/pull/6

Already a pull request that solves this!