Accessing objects programmatically

Related API functions

A simulation in V-REP supports virtually an infinite number of embedded scripts. Moreover, all scripts associated with objects (i.e. child scripts) will be duplicated if their associated object is duplicated. Which brings the question: how can a duplicated script access the correct objects? Normally, one would have to adjust each script (i.e. redirect commands to new objects, change object names, etc.), but this is not necessary in V-REP. Copied scripts will know they are a copy themselves and will adjust their behavior accordingly. Imagine a robot made-up by 6 objects and 2 child scripts. Following figure illustrates this example:

[Example of a robot model composed by 6 objects and 2 child scripts]


In above example, each child script will have to access some of the robot's objects (e.g. the robot's sensors/motors, etc.). Accessing scene objects and general-type objects in V-REP works by providing an object name to a function that will return a handle to that object. The handles can then be used to interact with the object (e.g. simGetObjectPosition(objectHandle,...)).

If the robot is now duplicated in the scene (through a single and same copy/paste operation), then the duplicated robot will be directly operational and behave like the original robot. Following figure illustrates the duplicated robot:

[Example of a duplicated robot model]


Notice that all duplicated objects have a "#0" appended to their original name. This is because there cannot be two identical names for objects in V-REP. Notice also that the duplicated child scripts have the same program content as the original child scripts. How come that the duplicated child scripts are not accessing the original objects? This is because V-REP uses an automatic name adjustment mechanism for child scripts. Following figure illustrates this concept:

[Automatic name adjustment mechanism for child scripts]


When a child script retrieves the handle of a scene object or a general-type object, then the provided object name will be augmented with a name suffix identical to the name suffix of the child script. This mechanism can be altered with the simGetNameSuffix and the simSetNameSuffix commands. By default the mechanism is adjusted to the name suffix of the current script, and it is recommended to alter this behavior only for very special cases and then immediately reset it again to its previous state. You can also tell V-REP not to automatically adjust the name, by using the '#' char when accessing the object: simGetObjectHandle("myObject#"), simGetObjectHandle("myObject#0") or simGetObjectHandle("myObject#42") will retrieve the handles of "myObject", "myObject#0" and "myObject#42" respectively, irrespective of the name suffix of the child script. The name adjustment mechanism is individually handled by each script (i.e. altering the mechanism in one script will not alter it in other scripts).

When retrieving object handles from the C/C++ side of the regular API, the automatic name adjustment mechanism will not work directly. Instead, you will have to adjust the name suffix manually to what you want, to obtain a similar behavior.


Recommended topics

  • Child scripts