Filter by Expression/Range to delete wedge nodes at Random

   2206   3   0
User Avatar
Member
10 posts
Joined: April 2019
Offline
Can you filter random values within a range using an expression?

"Please look at image attached"

i.e I have 495 Wedge Index's generated.

I would like to randomly delete 200 index's at random and not in a sequential order that is set in the range. So a total of 200 index's will be deleted but selected at random.

Would this be done in using a filter by range or filter by expression.

If so. what expression is best to do this.

I have tried this python script but it doesn't seem to work:

("`@pdg_frame`" if "`@pdg_frame`" in random.choices(range(0,10), k=10) else None)

Attachments:
2022-05-16_12h28_05.png (41.2 KB)

User Avatar
Staff
585 posts
Joined: May 2014
Offline
You can't mix HScript expression functions/variables like @pdg_frame and Python in the same expression. If you're using a Python expression, you'll need to use pdg.workItem() to access the work item that the parameter is being evaluated against. For example, pdg.workItem().frame in a Python expression is the same as @pdg_frame in an HScript expression.

pdg.workItem() will return a pdg.WorkItem instance that you can use to access attributes and intrinsic data: https://www.sidefx.com/docs/houdini/tops/pdg/WorkItem.html [www.sidefx.com]
Edited by tpetrick - May 17, 2022 12:10:09
User Avatar
Member
10 posts
Joined: April 2019
Offline
Thanks for that, so but changing my @pdg_frame to pdg.workItem().frame. Will that solve my main problem or are there other definitive issues wrong in my code, because it is still not working.

Even after changing the Language to python in my expression settings.
User Avatar
Staff
585 posts
Joined: May 2014
Offline
It's pretty hard to say what the issue is without actually seeing the .hip file.

If you want to delete exactly 200 work items, then that approach probably won't work. The filter expression is evaluated independently for each work item, which means there's no shared state and no way for you to ensure that an exact number are deleted. You're better of using a Python Processor TOP, which has access to the full list of input work items. Something like:

import random

choices = random.sample(range(0, len(upstream_items)), k=200)

for index, upstream_item in enumerate(upstream_items):
    if index not in choices:
        item_holder.addWorkItem(parent=upstream_item)
  • Quick Links