robotology / idyntree

Multibody Dynamics Library designed for Free Floating Robots
BSD 3-Clause "New" or "Revised" License
168 stars 67 forks source link

Wrong type returned when calling FromPython function #999

Open isorrentino opened 2 years ago

isorrentino commented 2 years ago

Here an example. If I run this code:

x_py = numpy.zeros(6)
x_idyn = idyn.JointPosDoubleArray(6)
x_idyn = x_idyn.FromPython(x_py)

x_idyn becomes a VectorDynSize object. Maybe, this happens because the FromPython function that is actually called is the one defined in https://github.com/robotology/idyntree/blob/163cff7899f7a660ed78fe550e4774553a4ced60/bindings/python/python.i#L184.

traversaro commented 2 years ago

Good point. This is a nasty effect of the fact that .FromPython method is a function that returns a new instance of the type, instead of populating the instance on which it is called (as .fromMatlab does). I am not sure why this happened back in time, but the quickest solution for your use case is to add a FromMatlab overload for JointPosDoubleArray, as done for example in https://github.com/robotology/idyntree/pull/959 for iDynTree::Position.

traversaro commented 2 years ago

An alternative may be to define a .FromPythonInPlace method for iDynTree::VectorDynSize (better name suggestions are welcome) that populate the existing vector instead of returning a new one (a bit like .fromMatlab do), and then use this new method in JointPosDoubleArray.