How to add a simple sphere in the world? + Bug

Hi,

It is the first time that I use dartpy (and dart in general). Is there a simple example on how to load a simple sphere in the world?

I tried to follow the C++ example available here: sphere_visualized. Currently, I have the following Python code but I can not see the sphere (and there are few errors / segmentation fault):

import dartpy as dart

# create world
world = dart.simulation.World()
world.setGravity([0., 0., -9.81])

# add ground
urdfParser = dart.utils.DartLoader()
ground = urdfParser.parseSkeleton("dart://sample/urdf/KR5/ground.urdf")
world.addSkeleton(ground)

# create skeleton
sphere = dart.dynamics.Skeleton(name="sphere")

# create shape
shape = dart.dynamics.SphereShape(radius=1.)
visual_aspect = dart.dynamics.VisualAspect()
# visual_aspect.setColor([1, 0, 0])  # SEGMENTATION FAULT
# Q: How to set this visual aspect to the shape?

# create body node and free joint
joint, body = sphere.createWeldJointAndBodyNodePair()

body.setMass(mass=1)
# body.setMomentOfInertia(shape.computeInertia(mass=1))  # ERROR: doesn't accept this input
body.createShapeNode(shape)

# add skeleton to the world
world.addSkeleton(sphere)

node = dart.gui.osg.RealTimeWorldNode(world)
viewer = dart.gui.osg.Viewer()
viewer.addWorldNode(node)

viewer.setUpViewInWindow(0, 0, 640, 480)
viewer.setCameraHomePosition([0.8, 0.0, 0.8], [0, -0.25, 0], [0, 0.5, 0])
viewer.run()

I’m not sure how the Aspect system is supposed to be use through the python bindings, but I can say for certain that it’s not supposed to be possible to construct an Aspect (in this case VisualAspect) outside of an object, like what you’re doing here. Part of the problem may be that python doesn’t have the same idea of public/protected/private constructors that C++ has.

In the C++ API, you would call a function on a ShapeNode in order to create the VisualAspect. My best guess is it would look something like this in python:

# ... [REDACTED] ...

# create shape
shape = dart.dynamics.SphereShape(radius=1.)

# create body node and free joint
joint, body = sphere.createWeldJointAndBodyNodePair()

body.setMass(mass=1)

# create shape node
shape_node = body.createShapeNode(shape)
visual_aspect = shape_node.createVisualAspect()
visual_aspect.setColor([1, 0, 0])

# add skeleton to the world
world.addSkeleton(sphere)

# ... [REDACTED] ...

It’s not immediately obvious to me what the issue with body.setMomentOfInertia(~) would be. I guess you’d have to inspect what data type the function expects to receive versus what data type is being produced by Shape::computeInertia(~).

Thanks for your reply! It now works. For the setMomentOfInertia, it was because it didn’t accept a numpy array as an input, but we have to provide instead Ixx, Iyy, etc.

Sorry, does anyone of you knows how to make the sphere move under gravity? Right now, I can see the sphere but it is fixed in the world…

Ok, I found the sphere was not moving because the visualizer was paused (pressing the space key moved the sphere under gravity), however now it doesn’t collide with the ground; it goes through it…

I tried:

body.setCollidable(True)

but this doesn’t seem to have any effects… Does someone knows how to enable collisions?

You need to add a collision aspect to the shape as well, like:

shape_node.createCollisionAspect()

Otherwise DART will think that it’s a shape which is meant to be displayed but not physically interacted with.

Oh okay, thanks! :slight_smile: It is working now; I had a bunch of warnings but I had to also create a dynamics aspect to remove them.

This is just to let know that there is an error with the capsule shape when enabling collisions:
Error [FCLCollisionDetector.cpp:976] [FCLCollisionDetector::createFCLCollisionGeometry] Attempting to create an unsupported shape type [CapsuleShape]. Creating a sphere with 0.1 radius instead.

Also, in order to load a mesh, I am a bit lost with the aiScene argument. What does this represent? I could not find the corresponding Python class… Here are the Python signatures of dart.dynamics.MeshShape:

1. dartpy.dynamics.MeshShape(scale: numpy.ndarray[float64[3, 1]], mesh: aiScene)
2. dartpy.dynamics.MeshShape(scale: numpy.ndarray[float64[3, 1]], mesh: aiScene, uri: dartpy.common.Uri)
3. dartpy.dynamics.MeshShape(scale: numpy.ndarray[float64[3, 1]], mesh: aiScene, uri: dartpy.common.Uri, resourceRetriever: dart::common::ResourceRetriever)