Jump to content

Get List of All Prim Groups in Python


Recommended Posts

I'm often bringing objects into Houdini that have 20+ primitive groups, and I'd like to figure out a way to easily split them up to put separate materials on each group, then merging them back together. Its rather tedious to do by hand - creating node after node and manually inputting each group name - so a scripting solution sounds like it would be much more appropriate. 

 

The psuedo-code would go something like:

 

sn = GetSelectedNode()
groups = sn.GetPrimitiveGroups()
 
for x in groups:
    Create Blast node
    Connect Blast to sn
    BlastGroup = x
 
    CreateMaterialNode
    Connect Material to BlastGroup
    MaterialGroup = x

 

I know how to do this sort of thing in C4D with Python, but I'm not sure of where to start with this sort of thing in Houdini; I've looked through the help guide, but couldn't find exactly what I needed. Thoughts?

    

Link to comment
Share on other sites

You could do that by script, but also Houdini have Material node which gives you ability to assign bunch of materials to single object where each material could be assigned to specific group. This way you do not need to blast out separate groups and merge them back. Simply, connect Material node at SOP level and use "+" button to add as many materials you need and select which group(s) material should be attached to.

Link to comment
Share on other sites

import hou

node = hou.selectedNodes()[0]
mat = node.createOutputNode('material')
mat.parm('num_materials').set(0)

groups = [g.name() for g in node.geometry().primGroups()]
for i, name in enumerate(groups, 1):
    # Add instance to the multiparm on material node.
    mat.parm('num_materials').insertMultiParmInstance(i-1)

    # Set instance's Group and Material parameters.
    group = mat.parm('group%d' %i)
    shop = mat.parm('shop_materialpath%d' %i)

    group.set(name)
    shop.set('/shop/' + name)

# Select material node.
mat.setCurrent(on=True, clear_all_selected=True)
mat.setDisplayFlag(True)
mat.setRenderFlag(True)

Open Windows / Python Source Editor (code already there), then select node and press Apply. You can do much more than that. Such as create materials.

automat.hipnc

  • Like 1
Link to comment
Share on other sites

you can also do this using VEX, using:

 

//

string groups[] = detailintrinsic(0,"primitivegroups");

int i,numGroups;

numGroups = len(groups);

 

for(i=0;i<numGroups;i++)

{

// code to assign your materials per group index

}

 

//

 

unfortunately this does not let you add instances to the multi parm automatically

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...