Using the area of a piece to create name attribute

   2246   6   2
User Avatar
Member
32 posts
Joined: May 2019
Offline
Hi there!
I have a seletion of debris items in various sizes. I would like to give them a sorted name attribute based on their surface area.
Getting the area per piece in a loop is easy enough but how can I use this float value to name the pieces in rising order from small to large? So piece01 would always be the smallest and piece0X will be the largest.
Also the float values for area are huge cumbersome numbers, idealy I would want to remap those into a range matching the number of the pieces.
Thanks!
User Avatar
Member
5100 posts
Joined: Feb. 2012
Offline
Hi,

Here is one way:



Sort by area


Attribute Promote (name) -> from Primitive to Detail -> Array of All


unique_names_array (Detail Wrangle):

string allnames [ ] = detail ( 0, "allnames" );
string names [ ] = { };

foreach ( string name; allnames )
    if ( find ( names, name ) < 0 )
        append ( names, name );
        
s[]@allnames = names;


assign_unique_indices_per_piece (Primitive Wrangle):

string allnames [ ] = detail ( 0, "allnames" );
i@index = find ( allnames, @name );
s@newname = sprintf ( "piece%s", i@index );
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
32 posts
Joined: May 2019
Offline
Thanks a lot for your help!
It's not quite working yet on my end. All indices return a -1 value and the "newname" pieces as well in accordance.
I attached a screenshot and a tiny hip file.
Thanks again!

Attachments:
sort_by_area.hip (143.6 KB)
sort_by_area.JPG (323.5 KB)

User Avatar
Member
5100 posts
Joined: Feb. 2012
Offline
Oh I see, I forgot to mention the attribute has to be renamed to allnames in Attrib Promote:

Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
32 posts
Joined: May 2019
Offline
Thanks, it's working perfectly now!
User Avatar
Member
32 posts
Joined: May 2019
Offline
Sorry to bring this up again, but how would you go about using the longest edge of the BBOX instead of the area to name the pieces?
User Avatar
Member
152 posts
Joined: Aug. 2012
Offline
Instead of sorting by area, sort by an attribute (e.g. called maxbboxlen) using the VEX snippet:

f@maxbboxlen = max(getbbox_size(0));
  • Quick Links