ZeroMQ-based remote API

The ZeroMQ-based remote API is one of several ways an application can connect with CoppeliaSim.

The ZeroMQ-based remote API allows to control a simulation (or the simulator itself) from an external application or a remote hardware (e.g. real robot, remote computer, etc.). It offers all API functions also available via a CoppeliaSim script: this includes all regular API functions (i.e. sim.* -type functions), but also all API functions provided by plugins (e.g. simOMPL.*, simUI.*, simIK.*, etc.). The ZeroMQ-based remoteAPI represents a very thin Python wrapper around mentioned API functions, and can be used in the exact same way as from within a CoppeliaSim script.

The ZeroMQ-based remote API functions are interacting with CoppeliaSim via ZeroMQ and its interface plugin to CoppeliaSim and the ZMQ remote API add-on. All this happens in a hidden fashion to the user. The remote API can let one or several external applications interact with CoppeliaSim in a synchronous or asynchronous* way (asynchronous by default), and even remote control of the simulator is supported (e.g. remotely loading a scene, starting, pausing or stopping a simulation for instance).

Note: the ZeroMQ-based remote API will be supported by default in CoppeliaSim V4.3.0 and later. For CoppeliaSim V4.2.0, follow this procedure: clone the ZeroMQ-based remote API repository into your CoppeliaSim/programming folder. Then use this compatibility add-on, this script and that script, and place them into your CoppeliaSim/ folder (for the add-on) and into the CoppeliaSim/lua folder (for the two last files).

See programming/zmqRemoteApi folder or its related repository for examples. Here a simple ZeroMQ-based remote API client:

# Make sure to have the add-on "Connectivity/ZMQ remote API server"
# running in CoppeliaSim
#
# Following are required and should be installed:
# pip install pyzmq
# pip install cbor

import time
from zmqRemoteApi import RemoteAPIClient

client = RemoteAPIClient('localhost',23000)
sim = client.getobject('sim')

# Create a few dummies and set their positions:
handles = [sim.createDummy(0.01, 12 * [0]) for _ in range(50)]
for i, h in enumerate(handles):
    sim.setObjectPosition(h, -1, [0.01 * i, 0.01 * i, 0.01 * i])

# Run a simulation in asynchronous mode:
sim.startSimulation()
while (t := sim.getSimulationTime()) < 3:
    s=f'Simulation time: {t:.2f} [s] (simulation running asynchronously to client)'
    print(s)
    sim.addLog(sim.verbosity_scriptinfos,s)
sim.stopSimulation()
# If you need to make sure we really stopped:
while sim.getSimulationState()!=sim.simulation_stopped:
    time.sleep(0.1)

# Run a simulation in synchronous mode:
client.setsynchronous(True)
sim.startSimulation()
while (t := sim.getSimulationTime()) < 3:
    print(f'Simulation time: {t:.2f} [s] (synchronous simulation)')
    sim.addLog(sim.verbosity_scriptinfos,f'Simulation time: {t:.2f} [s] (synchronous simulation)')
    client.step() # triggers next simulation step
sim.stopSimulation()

# Remove the dummies created earlier:
for h in handles:
    sim.removeObject(h)