# Count Program Nodes

**URL:** https://forum.universal-robots.com/t/count-program-nodes/6722
**Category:** URCaps SDK
**Created:** [March 19, 2020, 7:47am UTC](https://forum.universal-robots.com/t/count-program-nodes/6722 "2020-03-19T07:47:26Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![anon83100310](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@anon83100310](https://forum.universal-robots.com/u/anon83100310)
#### Post date: [March 19, 2020, 7:47am UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/1 "2020-03-19T07:47:26Z")

</div>

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.

Regards

---

<div class="post-metadata">

### Author: ![sam.verity-hilton](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/sam.verity-hilton/32/6046_2.png) [@sam.verity-hilton](https://forum.universal-robots.com/u/sam.verity-hilton)
#### Post date: [March 19, 2020, 3:23pm UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/2 "2020-03-19T15:23:00Z")

</div>

HI @anon83100310

I’m afraid that URCaps don’t intuitively have any methods that are triggered when they are deleted. Unlike`openView()` 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.

---

<div class="post-metadata">

### Author: ![anon83100310](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@anon83100310](https://forum.universal-robots.com/u/anon83100310)
#### Post date: [March 20, 2020, 7:26am UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/3 "2020-03-20T07:26:06Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![sam.verity-hilton](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/sam.verity-hilton/32/6046_2.png) [@sam.verity-hilton](https://forum.universal-robots.com/u/sam.verity-hilton)
#### Post date: [March 20, 2020, 8:42am UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/4 "2020-03-20T08:42:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![anon83100310](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@anon83100310](https://forum.universal-robots.com/u/anon83100310)
#### Post date: [March 20, 2020, 9:22am UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/5 "2020-03-20T09:22:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![sam.verity-hilton](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/sam.verity-hilton/32/6046_2.png) [@sam.verity-hilton](https://forum.universal-robots.com/u/sam.verity-hilton)
#### Post date: [March 20, 2020, 10:46am UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/6 "2020-03-20T10:46:13Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![fujikit](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/fujikit/32/6042_2.png) [@fujikit](https://forum.universal-robots.com/u/fujikit)
#### Post date: [March 20, 2020, 10:55am UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/7 "2020-03-20T10:55:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![fujikit](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/fujikit/32/6042_2.png) [@fujikit](https://forum.universal-robots.com/u/fujikit)
#### Post date: [March 21, 2020, 1:52pm UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/8 "2020-03-21T13:52:36Z")

</div>

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,

---

<div class="post-metadata">

### Author: ![anon32693620](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@anon32693620](https://forum.universal-robots.com/u/anon32693620)
#### Post date: [March 25, 2021, 9:04pm UTC](https://forum.universal-robots.com/t/count-program-nodes/6722/9 "2021-03-25T21:04:34Z")

</div>

You could look into using global variables as stated in “working\_with\_variables” pdf from the SDK. Look at Section 4.1 on how it handles naming convention when creating global variables. This could be used at the creation of a ProgramNode and saved only within that Nodes DataModel. Doing this would also remove the global variable when the node has been deleted and the next Node would increment to its place. That way you could also use this name within your script.
