How to use For Loop SOP to iterate over a dict?

   869   3   1
User Avatar
Member
539 posts
Joined: 8月 2019
Offline
For example, my geometry has this detail attribute :

{ "group1": 1, "group2": 1, "group3": 0}

And I'd like to use Blast SOP to delete groups whose values are 1 (in this case, group1 and group2).

A For Loop seems to be the reasonable solution:



... then I got stuck.

How to make it loops over len(my_attribute)times and how to access the dict's values from Blast SOP's parameters?
Edited by raincole - 2024年3月7日 11:26:57

Attachments:
Screenshot 2024-03-08 002415.png (54.7 KB)

User Avatar
Member
11 posts
Joined: 3月 2017
Offline
use "name" node and set "Name frome Group" and "Group Mask" to get name attribute,then go to for loop
User Avatar
Member
297 posts
Joined: 1月 2013
Offline
I don't think For Each and Blast SOP are very suitable for this. The reason is that in a dictionary elements are accessed by key, not by index, so getting the length of the dictionary will not help much in the end. To make this possible For Each SOP needs to be able to iterate over keys and not over indexes, but this feature is not available here.

If you really need to iterate through the dictionary and look up or delete something, then use AttribWrangle.

dict dictionary = detail(0, "groups");
foreach(string grp; keys(dictionary)){
    // ...
}
User Avatar
Member
13 posts
Joined: 1月 2011
Offline
Using python in blast should meet your requirements
geo = hou.pwd().input(0).geometry()
dic = geo.attribValue('detail_attrib')
lis = [k for k,v in dic.items() if v==1]
return(' '.join(lis))

Image Not Found
Edited by yeatshai - 2024年3月8日 02:18:35

Attachments:
blast_group_thats_value_is_1.hipnc (46.0 KB)

  • Quick Links