Universal Robots+

Generate URScript functions from program node or installation node?

Is it a good practice to define functions inside a program node generateScript() method (using writer.defineFunction("myFun()") or writer.appendLine("def myFun(): etc...."))?

For example this is done by the integrated Palletizing node (by looking at the the resulting .script file); however by doing this the function is re-defined for each program cycle, isn’t this practice inefficient? Wouldn’t it be better to define functions in the installation node and call them in the program nodes?

Hi, @a.mora

When I create urcap, I define urscript functions in installation node.
I don’t define urscript functions in program nodes.

But it is a little hard to write code to define function.
So I create script file in resource folder and I use the following code in my samples.

public String[] readScriptFile(String filename) {
	try {

		BufferedReader br = new BufferedReader(
				new InputStreamReader(this.getClass().getResourceAsStream(filename)));

		ArrayList<String> list = new ArrayList<String>();

		String addstr;
		while ((addstr = br.readLine()) != null) {

		String[] res = list.toArray(new String[0]);
		return res;

	} catch (IOException e) {
		return null;

public void generateScript(ScriptWriter writer) {

	String[] scripts = readScriptFile("/com/ur/urcap/safetyhome/impl/safetyhome.script");

	for (String script : scripts) {
		script = script.replace("{inputLowerX}", model.get("inputLowerX", "-1000"));
		script = script.replace("{inputUpperX}", model.get("inputUpperX", "1000"));
		script = script.replace("{inputLowerY}", model.get("inputLowerY", "-1000"));
		script = script.replace("{inputUpperY}", model.get("inputUpperY", "1000"));
		script = script.replace("{inputLowerZ}", model.get("inputLowerZ", "-1000"));
		script = script.replace("{inputUpperZ}", model.get("inputUpperZ", "1000"));

		script = script.replace("{inputMoveX}", model.get("inputMoveX", "0"));
		script = script.replace("{inputMoveY}", model.get("inputMoveY", "0"));
		script = script.replace("{inputMoveZ}", model.get("inputMoveZ", "0"));

		script = script.replace("{baseAxis}", model.get("inputBase", true) ? "True" : "False");


1 Like

Thanks for the code, this can be very useful.

Only one question: why do you use writer.writeChildren() in an installation node?

I am sorry that uploaded sample is mistaken.
Please see my sample here, instead of.


1 Like