Customization scripts

Customization scripts are embedded scripts that can be used to customize a simulation scene to a great extent. They are attached to (or associated with) scene objects, and they can be easily recognized from their dark script icon in the scene hierarchy:

[A customization script associated with object ResizableFloor_5_25]

Double-clicking the script icon allows opening the script editor. You can change properties of a given script, or associate it with another object via the script dialog. You can attach a new customization script to an object by selecting the object, then navigating to [menu bar --> Add --> Associated customization script].

Following are customization script's main properties:

  • they run non-threaded.
  • they are executed all the time (within a same scene): when simulation is running, as well as when simulation is not running.
  • they are attached to (or associated with) scene objects (i.e. they are associated scripts). Associated scripts form the basis of CoppeliaSim's distributed control architecture, and share the convenient property to be automatically duplicated if their associated object is duplicated.
  • Above properties allow customization scripts to share some of the best features of add-ons and child scripts. Customization scripts allow the creation of customizable models for instance: imagine a model that was dropped into a scene, and that is able to configure or adapt itself, even when simulation is not running. This could be a robot where the user can adjust the various link lengths with a single slider repositioning.

    Customization scripts should contain a collection of system callback functions. Those should not be blocking. This means that every time they are called, they should perform some task and then return control. If control is not returned, then the whole application halts. Customization script functions are called by the system very often, but also by the main script, by following a precise execution order. Callback functions are also supported by customization scripts.

    A customization script should be segmented in several system callback functions, as following skeleton script illustrates:

    -- This is a customization script. It is intended to be used to customize a scene in
    -- various ways, mainly when simulation is not running. When simulation is running,
    -- do not use customization scripts, but rather child scripts if possible
    
    function sysCall_init() -- not optional!
        -- do some initialization here
    end
    
    function sysCall_nonSimulation()
        -- is executed when simulation is not running
    end
    
    function sysCall_cleanup()
        -- do some clean-up here
    end
    
    -- You can define additional system calls here:
    function sysCall_beforeSimulation()
        -- Simulation is about to start
    end
    
    function sysCall_actuation()
        -- put some actuation code here.
    end
    
    function sysCall_sensing()
        -- put some sensing code here.
    end
    
    function sysCall_suspend()
        -- Simulation is about to be suspended
    end
    
    function sysCall_suspended()
        -- Simulation is suspended
    end
    
    function sysCall_resume()
        -- Simulation is about to resume
    end
    
    function sysCall_afterSimulation()
        -- Simulation has just ended
    end
    
    function sysCall_beforeInstanceSwitch()
        -- About to switch to another scene
    end
    
    function sysCall_afterInstanceSwitch()
        -- Switched to another scene
    end
    
    function sysCall_userConfig()
    end
    
    function sysCall_dynCallback(inData)
        -- See the dynamics callback function section in the user manual for details about the input argument
    end
    
    function sysCall_jointCallback(inData)
        -- See the joint callback function section in the user manual for details about input/output arguments
        return outData
    end
    
    function sysCall_contactCallback(inData)
        -- See the contact callback function section in the user manual for details about input/output arguments
        return outData
    end
    
    function sysCall_vision(inData)
        -- See the vision callback function section in the user manual for details about input/output arguments
        return outData
    end
    
    function sysCall_trigger(inData)
        -- See the trigger callback function section in the user manual for details about input/output arguments
        return outData
    end
    
    function sysCall_userConfig()
        -- See the user config callback function section in the user manual for details
    end
    
    function sysCall_beforeCopy(inData)
        for key,value in pairs(inData.objectHandles) do
            print("Object with handle "..key.." will be copied")
        end
    end
    
    function sysCall_afterCopy(inData)
        for key,value in pairs(inData.objectHandles) do
            print("Object with handle "..key.." was copied")
        end
    end
    
    function sysCall_afterCreate(inData)
        for key,value in pairs(inData.objectHandles) do
            print("Object with handle "..value.." was created")
        end
    end
    
    function sysCall_beforeDelete(inData)
        for key,value in pairs(inData.objectHandles) do
            print("Object with handle "..key.." will be deleted")
        end
        -- inData.allObjects indicates if all objects in the scene will be deleted
    end
    
    function sysCall_afterDelete(inData)
        for key,value in pairs(inData.objectHandles) do
            print("Object with handle "..key.." was deleted")
        end
        -- inData.allObjects indicates if all objects in the scene were deleted
    end
    
    function sysCall_beforeMainScript()
        -- Can be used to step a simulation in a custom manner.
        local outData={doNotRunMainScript=false} -- when true, then the main script won't be executed
        return outData
    end

    If possible, do not use customization scripts to run simulation code, which is anyway best handled via child scripts.


    Recommended topics

  • Main script
  • Child scripts
  • Add-ons
  • Script execution order