how to get unique ID from a workitem index?

   2601   4   1
User Avatar
Member
900 posts
Joined: Feb. 2016
Offline
dict = {} ## pdg_index and unique id     
for w in pdg_node.workItems:
   dict[w.index] = w.id
   

I'm using this code to reconstruct an ordered dictionary of workitems of a pdg_node and their corresponding unique ID.
Problem: I just need to know a single specific unique ID, from a workitem index of a pdg.node. I don't want to iterate over all the workitems to reconstruct a dictionary.

Naively, I thought I could just access a workitem like this:
pdg_node.workItems[3]
and then asking for its unique workItem.id, but by doing so I'm not getting the workitem with index 3.

Indeed the following code produce a unordered list of workitems, and an ordered dictionary of workitems. Might be a python thing? I didn't know that dictionaries get auto-sorted. My question remain, anyway..
wlist = [] 
dict = {}
for w in pdg_node.workItems:
    wlist.append(w.index)
 
for w in pdg_node.workItems:  
    dict[w.index] = w.id
Edited by Andr - Oct. 1, 2019 04:57:17
User Avatar
Member
209 posts
Joined: Nov. 2010
Offline
Hi,

But what is wrong with 'pdg_node.workItems.index'? if you set index 3 for workitem 3 then it returns 3.


If I understand it properly, ID is just your scene workitem unique id.
User Avatar
Member
900 posts
Joined: Feb. 2016
Offline
Hello,
my aim is to know the unique ID, from a workitem index of a top node, without doing any loop.
I just found out that a Python Script node in your topnet (even blank) mixes things up.
And you can't use a simple
pdg_node.workItems[52]
to get the workitem index 52 of that Top Node.
The pdg_node.workItems list you get from the Python Script node is probably sorted raw based on the order of the processed workitems by the node. I guess. (on each new cook it will change indeed)
You have have to create a new list of workitems and resort it based on their index, and that's what the dictionary approach seems to do (even if I don't understand why it does this autosorting)

Try the attached example:
topnode = hou.pwd().inputs()[0].node('./OUT')
pdgnode = topnode.getPDGNode()
check_index = hou.pwd().parm('checkworkitem').eval()

wlist = [] 
for w in pdgnode.workItems:
    wlist.append(w)
    #print w.index

wdict = {}
for w in pdgnode.workItems:  
    wdict[w.index] = w.id
    
print wdict[check_index], wlist[check_index].id
## the two numbers should be equal, but they won't if you have a python script node in your topnet
## bypass it to see matching pairs of numbers displayed
Edited by Andr - Oct. 1, 2019 05:34:48

Attachments:
uniqueidfromindex.hiplc (128.2 KB)
workitemsindexJPG.JPG (160.4 KB)

User Avatar
Member
603 posts
Joined: Sept. 2016
Offline
The work item index is just an arbitrary number, it doesn't have to be unique although nodes usually follow the convention of assigning indexes so that they form a continuous range.

Also, dictionaries don't get auto-sorted, it's because it's implemented using a hash table so it just happens that the .values() are in increasing order of their keys because hash(i) = i for integers.
Edited by chrisgreb - Oct. 3, 2019 21:11:53
User Avatar
Member
209 posts
Joined: Nov. 2010
Offline
Task ID and Index are different things (They should not be equal).
As Chris mentioned that index is just an arbitrary number (you can assign whatever you want).
  • Quick Links