pythonwebots

How do you add a PROTO node programmatically in Webots?


Using a Supervisor controller, is it possible to programmatically add Robot nodes to a running simulation which are defined in .proto files?

I tried using importMFNode, but it looks like it only supports .wbo or .wrl files.


Solution

  • You should use importMFNodeFromString like this:

    from controller import Supervisor
    
    supervisor = Supervisor()
    children = supervisor.getRoot().getField('children')
    children.importMFNodeFromString(-1, 'Nao { translation 0.1 0.334 -0.2 }')
    

    That will add a Nao robot with the specified translation at the bottom of the scene tree. You may also specify the rotation and other parameters of the Nao PROTO.