Uh oh!
There was an error while loading. Please reload this page.
Add binding for Controller::draw() and drawTools() - #532
Conversation
So it become possible to draw from python.
So we can print all the dof of a mechanical object without copy
THE EXAMPLE IN THIS COMMENT IS DEPRECATED importSofaimportSofaTypesclassDrawingExamples(Sofa.Core.Controller):
def__init__(self, *args, **kwargs):
# These are needed (and the normal way to override from a python class)Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.target=kwargs.get("target", None)
self.mo=kwargs.get("mo", None)
defdraw(self, visual_context):
dt=visual_context.getDrawTool()
dt.drawPoints([SofaTypes.Vec3d(-1.5,0,-1)], 5.0)
dt.drawPoints([SofaTypes.Vec3d(-1.3,0,-1), SofaTypes.Vec3d(1.3,0,-1)], 5.0)
dt.drawLines([SofaTypes.Vec3d(-1.3,0,-1), SofaTypes.Vec3d(1.3,0,-1)], 1.0)
dt.drawFrames([SofaTypes.Vec3d(-1.5,0.1,-1)], [SofaTypes.Quat(0.0,0,0,1.0)], SofaTypes.Vec3d(0.1,0.1,0.1))
ifself.targetisnotNone:
dt.drawPoints(self.target.position, 2.0)
dt.draw3DText(SofaTypes.Vec3d(-2.0,0.0,0.0), 0.5, "This is not a raptor")
dt.drawFrames(self.mo.position, SofaTypes.Vec3d(0.1,0.1,0.1))
defcreateScene(root):
root.dt=0.01root.bbox= [[-1,-1,-1],[1,1,1]]
root.addObject('DefaultVisualManagerLoop')
root.addObject('DefaultAnimationLoop')
root.addObject("MeshOBJLoader", name="loader", filename="mesh/raptor_35kp.obj")
root.addObject("MechanicalObject", name="mo", template="Rigid3", position=[[float(i)/10.0, 0.0,0.0, 0.0, 0.0, 0.0, 1.0] foriinrange(0,10)])
# Add our python controller in the sceneroot.addObject( DrawingExamples(name="DrawingController2", target=root.loader,
mo=root.mo) )![]() |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
e916e24 to
d9b84f8CompareHi @hugtalbot, @bakpaul I have added the support for numpy, removed the support for Vec3 and added support for RGBAColor, drawCircle, drawSpheres, drawTriangles, drawQuads. I would dream of having more implicit conversion (eg: to avoid the need for RGBAColor)... but I didn't manage so far using the custom pybind11::type_caster mechanism. I will not investigate further this aspect for the moment, in addition improving the drawing API by removing the RGBA is something that can be done in the future without breaking things. |
damienmarchal
commented
Sep 11, 2025
Happy to see it is ready... |
Uh oh!
There was an error while loading. Please reload this page.
* Add binding for Controller::draw() and drawTools() So it become possible to draw from python. * Add drawFrames, drawLines, drawPoints and an example * Add drawFrame(Data<std::vector<Rigid3Coord>>) So we can print all the dof of a mechanical object without copy * Cosmetic improvement of Binding_VisualParams * Add binding for DrawTool::drawText * [Sofa.Types] Add RGBAColor binding & tests * FIXUP * Separate DrawTool from VisualParam * Add support for non Vec3d arguments. * Update example and add BaseData support for drawLines * Add enable/disableLighting, drawDisk, drawCircle, drawTriangles, drawQuads * improve example. * add variation on drawTriangle/drawQuads * Reset python version 3.7 * Add documentation

So it become possible to draw from python.
I think we should discuss the API of this. Do we mimick the C++ one or do we make one that avoid data conversion.
A small example:
With the corresponding picture:
