vex/python: How to Get the Name of a Material {[SOLVED]}

   688   4   1
User Avatar
Member
1010 posts
Joined: April 2017
Offline
Hi!

I'm working in sops but I need to fetch the name of multiple materials and use it in VEX. If I already know the path to the materials (like: /mat/Wood_Material), how can I get the name of the material connected to the material ouputs (always named OUT_material)?

I'm guessing it can't be done in VEX. If so, is there a way I could use a python sop that would get the info so I could then get it from a wrangle and continue in VEX?

-Olivier
Edited by olivierth - Nov. 15, 2023 13:15:36

Attachments:
Houdini_get_Material_Name_Python.JPG (51.9 KB)

User Avatar
Member
47 posts
Joined: July 2017
Offline
This is a bit more hard-coded than I'd usually prefer, but it works as long as your material output is always name "OUT_material" and the material name you need is in the first input. Paste in in a Python SOP and connect it to your object. Running Python on each prim can be slow on bigger objects though.
It'll store the material node name in the prim attribute 'mat_name' which you can then access in VEX.

node = hou.pwd()

def GetMaterialName(node):
    geom = node.geometry()
    # Create the attribute to store name at first
    geom.addAttrib(hou.attribType.Prim, 'mat_name', 'None')
    
    for prim in geom.prims():
        matnode = hou.node(prim.attribValue('shop_materialpath'))
        mat_childs = matnode.children()
        for c in mat_childs:
            if c.name() == 'OUT_material':
                mat_name = c.input(0).name()
                prim.setAttribValue('mat_name', mat_name)
                
# Run the script
GetMaterialName(node)
Edited by Tronotrond - Nov. 15, 2023 12:57:03
User Avatar
Member
1010 posts
Joined: April 2017
Offline
OHHHH, Fantastic! You just opened a world of possibilities.

I'm running it on packed prims so I don't think this will be slow for any of my assets.

Thanks a LOT!

-Olivier
User Avatar
Member
208 posts
Joined: Jan. 2013
Online
This is a bit more hard-coded than I'd usually prefer, but it works as long as your material output is always name "OUT_material" and the material name you need is in the first input. Paste in in a Python SOP and connect it to your object. Running Python on each prim can be slow on bigger objects though.
It'll store the material node name in the prim attribute 'mat_name' which you can then access in VEX.

With your permission, I would improve this code a bit and just return the output of the subnetwork itself, so that the inner loop is avoided and it doesn't refer to a hard-coded name.

for prim in geom.prims():
    matnode = hou.node(prim.attribValue('shop_materialpath'))
    mat_name = matnode.subnetOutputs()[0].input(0).name()
    prim.setAttribValue('mat_name', mat_name)
User Avatar
Member
47 posts
Joined: July 2017
Offline
alexwheezy
This is a bit more hard-coded than I'd usually prefer, but it works as long as your material output is always name "OUT_material" and the material name you need is in the first input. Paste in in a Python SOP and connect it to your object. Running Python on each prim can be slow on bigger objects though.
It'll store the material node name in the prim attribute 'mat_name' which you can then access in VEX.

With your permission, I would improve this code a bit and just return the output of the subnetwork itself, so that the inner loop is avoided and it doesn't refer to a hard-coded name.

for prim in geom.prims():
    matnode = hou.node(prim.attribValue('shop_materialpath'))
    mat_name = matnode.subnetOutputs()[0].input(0).name()
    prim.setAttribValue('mat_name', mat_name)

Great appreciate that! A very good improvement
Edited by Tronotrond - Nov. 15, 2023 15:24:28
  • Quick Links