VEX: Working with groups

A few useful sample snippets to manipulate and read geometry groups within a Wrangle SOP. (inpointgroup, setpointgroup, npointsgroup, expandpointgroup, detailintrinsic)

Testing groups membership and assignment, creating groups

int ingroup = inpointgroup(0, "group1", @ptnum);

This tests group assignment and returns true/false. For primitives and vertices use inprimgroup and invertexgroup.
Also works with wildcards:

i@pieces = inpointgroup(0, "piece*", @ptnum); 

Members of any group matching name pattern “piece*” will be evaluated 1.

Next example assigns a material by primitive group names. Useful for batch wrangling materials within a single node (best performance method for many material assignments):

if(inprimgroup(0, "*glass*", @primnum)==1) s@shop_materialpath = "/mat/glass/"; 

Alternative method to test group assignment:

if(@group_mygroup==1) i@myattribute = 1;

This code also writes an attribute based on the group membership. In this case I’m using a special virtual attribute @group that is only accessible in VEX (and you can see it in Geometry Spreadsheet).

It will work the opposite way too – testing attribute value to write group assignment:

if(@density<0.5) @group_mygroup = 1;

If the group does not exist beforehand it will be created.
Removing group membership can be done by assigning a value of 0:

if(@density<0.5) @group_mygroup = 0;

Removing a group altogether is currently not possible within VEX or VOP context.

More sophisticated way to manipulate groups is setpointgroup, setprimgroup, setedgegroup and setvertexgroup:

if(@density<0.5) setpointgroup(0, "mygroup", @ptnum, 1);

Like this it’s only a longer way to perform the same action but it’s convenient if you need more control. For instance:

if(@density<1) setpointgroup(1, "mygroup", @ptnum, 1, "toggle");

This snippet will test an attribute @density at the first input geometry and toggles the “mygroup” membership at the second input geometry on corresponding point numbers. Something like this can make sense, for example, if you want to transfer data between two matching topologies while one of them is deformed or something.

Listing group elements

Another thing you can do with groups is reading how many elements they contain and listing them:

i@count = npointsgroup(0, 'mygroup');

Returns number of points contained in “mygroup” on first geometry input. This can be done with primitive and vertex groups as well (nprimsgroup, nverticesgroup)

i[]@list = expandpointgroup(0, 'mygroup');

This will list the contained point numbers as an integer array.

Listing existing groups

s[]@list = detailintrinsic(0, 'pointgroups');

Returns a string array of existing point groups. Works for other group types with respective arguments:

s[]@list = detailintrinsic(0, 'primitivegroups');
s[]@list = detailintrinsic(0, 'vertexgroups'); 
s[]@list = detailintrinsic(0, 'edgegroups');  


2 thoughts on “VEX: Working with groups

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s