
Lua vs Python scripts
The way Lua and Python scripts are handled in CoppeliaSim (next to differences related to the language itself) displays small differences, which are discussed here:
Python scripts require a #python header tag as the very first instruction/comment:
1 2 3 4 | def sysCall_init():
print ( "Hello world!" )
|
Lua code always executes in the same thread as CoppeliaSim and is thus quite fast. Python code on the other hand is launched in a new process, which connects to CoppeliaSim via socket communication. This implies that Python code will usually start and run slower than Lua code.
Python scripts are handled via Lua wrapper code, which handles the launch and communication of each Python script. Auxiliary Lua code can be passed via the luaExec command, within a Python comment section. That code must come immediately after the #python header tag:
1 2 3 4 5 6 7 8 9 10 11 12 |
def sysCall_init():
print ( "Hello world!" )
|
The import directive does not work when importing the primary Python file, and following should be used instead:
Threaded Lua code, since implemented via coroutines, will always block while executing (i.e. CoppeliaSim won't handle any other task during that time). This is most of the time not noticeable, except when calling blocking functions that are foreign to CoppeliaSim. Threaded Python code on the other hand, will never block when executing, also with functions that are foreign to CoppeliaSim.
Python threaded scripts do not have an initialization nor clean-up callback. They should thus handle initialization and clean-up within their sysCall_thread function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def sysCall_thread():
sim.setThreadAutomaticSwitch( True )
h = sim.getObject( '/myCube' )
originalColor = sim.getObjectColor(h, 0 ,sim.colorcomponent_ambient_diffuse)
sim.setObjectColor(h, 0 ,sim.colorcomponent_ambient_diffuse,[ 1 , 0 , 0 ])
while sim.getThreadExistRequest() = = False :
print (sim.getObjectPosition(h, - 1 ))
sim.setObjectColor(h, 0 ,sim.colorcomponent_ambient_diffuse,originalColor)
|
Python threaded scripts should explicitly enable thread automatic switching, otherwise CoppeliaSim will block (or wait until sim.switchThread is called):
1 2 3 4 5 6 | def sysCall_thread():
sim.setThreadAutomaticSwitch( True )
while sim.getThreadExistRequest() = = False :
pass
|
Python threaded scripts will need to call a message pump (via sim.handleExtCalls) in order to have user callback functions to be serviced:
1 2 3 4 5 6 7 8 9 10 11 12 | def sysCall_thread():
sim.setThreadAutomaticSwitch( True )
xml = '<ui title="Custom UI"> <button text="Click me!" on-click="click_callback"/> </ui>'
ui = simUI.create(xml)
while sim.getThreadExistRequest() = = False :
sim.handleExtCalls()
simUI.destroy(ui)
def click_callback(ui,button):
print ( "button was clicked!" )
|
Lua callback functions are always reentrant, while Python callback functions are usually non-reentrant. A workaround to this is to have Lua assist Python like in following example, where Python would call a Lua function:
1 2 3 4 5 6 7 8 9 10 11 |
def sysCall_init():
object = sim.getScriptInt32Param(sim.handle_self,sim.scriptintparam_objecthandle)
path = sim.getObjectAlias( object , 1 )
sim.callScriptFunction( 'myLuaFunction@' + path,sim.scripttype_childscript)
|
Some rare API functions will only be availabe in Lua, while other only in Python.
The word table in the documentation refers to array-like or map-like items in Lua, while in Python, it refers to either lists or dicts.
Finally remember that Python lists have a zero-based numbering, while Lua array-like tables have a 1-based numbering.
|