Modify a Solaris MatLib using Python ...

   214   0   1
User Avatar
Member
278 posts
Joined: Sept. 2012
Offline
Hello guys!😀
Im trying to build a python script, to read a SOP detail name attribute on my geo in LOP , and in my library to modify the name of my material using that extracted string. I tried with a Python LOP node or a python panel but i can't make it...
Additionnally i was also planning to interractively been able to Create additionnal materialX network if needed by my asset but so far the first step was this one.

The general idea is to bring by FBX material to USD Mat ready material assigned to the correct asset... I can't make sop import to import any material so far so i have to find another approach.

The LOP python Script look like this :

import hou
from pxr import Usd, UsdGeom

# 1. Grab the current LOP and its upstream stage
lop = hou.pwd()
upstream = lop.inputs()[0]
stage = upstream.stage()  # Read‐only; safe from recursion

if not stage:
    # No stage upstream? bail out.
    raise hou.NodeError("No USD stage found on first input!")

# 2. Build a map from the SOP prim paths to their masterasset string
master_map = {}
for prim in stage.Traverse():
    # Look for your attribute on each prim
    attr = prim.GetAttribute("masterasset")
    if attr:
        master_map[prim.GetPath()] = attr.Get()

# 3. Locate your material library and loop its child prims
mat_lib_path = "/stage/Asset_Materiallibrary1/"
mat_lib = stage.GetPrimAtPath(mat_lib_path)
if not mat_lib:
    raise hou.NodeError(f"Material library not found at {mat_lib_path}")

for child in mat_lib.GetChildren():
    old_name = child.GetName()
    # Split by last '_' to isolate suffix (e.g. 'Leaf_02', 'Trunk_01')
    if "_" not in old_name:
        continue  # nothing to do
    prefix, suffix = old_name.rsplit("_", 1)
    # Try to find matching masterasset: 
    # assume your SOP import created a prim at the same path as the material
    # (or adjust this lookup logic to suit your stage layout)
    matching_master = master_map.get(child.GetPath(), None)
    if not matching_master:
        # fallback: maybe match by prefix
        matching_master = master_map.get(prefix, None)
    if not matching_master:
        continue  # no masterasset for this material

    new_name = f"{matching_master}_{suffix}"
    if new_name != old_name:
        child.SetName(new_name, True)  # forceUnique=True

# 4. Done

While the python shelf script looks like this ....

import hou

def rename_materials_from_master():
    sel = hou.selectedNodes()
    if len(sel) != 2:
        hou.ui.displayMessage("Select exactly two LOPs:\n"
                              "1) Your ImportAttributes SOP LOP\n"
                              "2) Your Material Library LOP")
        return

    sop_lop, mat_lop = sel
    stage1 = sop_lop.stage()
    if not stage1:
        hou.ui.displayMessage("1st LOP has no stage!")
        return

    # gather masterasset attrs
    master_map = {}
    for prim in stage1.Traverse():
        a = prim.GetAttribute("masterasset")
        if a: master_map[prim.GetPath()] = a.Get()

    stage2 = mat_lop.stage()
    if not stage2:
        hou.ui.displayMessage("2nd LOP has no stage!")
        return

    # assume root prim name == LOP node name
    root_name = mat_lop.name()
    root_prim = stage2.GetPrimAtPath("/" + root_name)
    if not root_prim:
        hou.ui.displayMessage(f"No prim at /{root_name}")
        return

    renamed = 0
    for child in root_prim.GetChildren():
        old = child.GetName()
        if "_" not in old:
            continue
        prefix, suffix = old.rsplit("_", 1)
        new_prefix = master_map.get(child.GetPath(),
                                   master_map.get(prefix))
        if not new_prefix:
            continue
        new_name = f"{new_prefix}_{suffix}"
        if new_name != old:
            child.SetName(new_name, True)
            renamed += 1

    hou.ui.displayMessage(f"Renamed {renamed} materials under /{root_name}")

rename_materials_from_master()

Have you already face that need? Thanks you!

vincent*
Vincent Thomas   (VFX and Art since 1998)
Senior Env and Lighting  artist & Houdini generalist & Creative Concepts
http://fr.linkedin.com/in/vincentthomas [fr.linkedin.com]
  • Quick Links