Universal Robots+

Count Program Nodes

I’m working on my first URCap, and, for the most part, it’s going smoothly, but I have stumbled upon something I don’t quite know how to solve.

I would like to add a bunch of my Program Nodes to the Program Tree in PolyScope. When I add a new node (all of them are the same URCap, just diferent instances) I would like to add a number at the end of the node title displayed in the Program Tree. Something similar to how the Thread node works - when you add the first Thread, its title is “Thread_1”, then the next one will get “Thread_2”, and so on.

Now, I have tried to achieve this functionality by creating an integer variable in the Program Node View, which I would increment every time a new Contribution is created, and then use that variable in the getTitle() function of the contribution. That works, but I don’t know how to decrease my counter when I delete a node from the Program Tree. So, if I add 10 of my nodes, they do have numbers 1-10 in their titles, but when I delete any one of them, I don’t have a way of changing that number in the title of each node.

I have a feeling my approach is not even the right way to go about this, I came up with that partial solution, since I wasn’t able to find anything that would help in the API reference or the, otherwise very helpful, samples.

Let me also mention, that I would like to use the number of the particular node in my Data Model for generating variable names in my generateScript() method.

Any help is much appreciated. If I manage to find a solution to this on my own, I’ll make sure to post it in this thread as well.


HI @pawel.achtelik

I’m afraid that URCaps don’t intuitively have any methods that are triggered when they are deleted. UnlikeopenView() and closeView() which are called whenever a URCap node is selected/deselected. Deleting a URCap program node from teh program tree has no such method.

You could suggest something like this is added by creating a new post in the “Feature Requests” section of the forum.

Hi Sam,

thanks for your reply. I will post it in the Feature Requests section, since adding such event to the API would be a quality-of-life improvement.

However, do you, or anyone else for that matter, know how this functionality is achieved in something like the “Thread” node?

Threads might not be what you’re after, as they only run when a program is running, and because I am not sure of any script functions that can evaluate the program tree. However there is a process called a “Daemon”. This is a process that a URCap can contribute to polyscope. A Daemon process is similar to a thread in that it is a parallel process that runs in the background, but a daemon runs in polyscope all the time regardless of what the robotr is doing, not just when a program is running. This may be something you can use as a work around. I’m not an expert in them, and haven’t really explored them. You should be able to find more help on the forum, and there is an example daemon URCap incluided in the starter package too.

I think I didn’t make my question clear enough, so let me rephrase that:

How does PolyScope know what number to append at the end of the node title displayed in the program tree, when you’re inserting a new Thread node? I assume each node provided with PolyScope works in a similar fashion to a URCap node created by any of us, therefore I assume there is a mechanism in there that allows the node to change the title of the particular contribution, depending on the number of such nodes inserted into the program tree.

I don’t really need to use Threads or a Daemon at all, since I’m quite certain everything I’m trying to achieve is doable in a ProgramNode.

I suspect that the naming functionality that you see with threads uses mechanics stored elsewhere in polyscope. So when new threads are created or removed, some other class somewhere will update all the numbers in the names. I think you might be able to acheive something similar with a daemon, as it is a constantly running background prpocess, so have the daemon be contantly checking how many of your program nodes are in the robot program, and update them if there is ever any change. Again I’ve no experience using a daemon, but this might be acehivable with one.

I cannot get idea that number is decrease with deleting nodes
because the program contribution cannot refer parameters of other same nodes even if you use daemon service.

I am sorry if I don’t have enough knowledge.

Maybe, It is not possible that number of your urcap is decrease, because instances of programnode contribution are remained after closing old urp file and opening new urp file.

If the instances can be disposed after closing urp, your request may be realized every time you reload urp.

As an alternative, there is the feature that the title of program node can be named by user when I create urcap,