Ben Traje
← Back to maya

Batch Assign Arnold Hair Shaders to NURBS Splines

25 Feb 26 (1mo ago)

If it's a native hair object, like nHair or xGen, you easily assign a hair shader like a regular geometry. Not the same case for raw splines. You have to do it under the attribute editor manually (like the same way you would fille up a texture in a fileTexture node).

But the actual kicker is: you have to do it for every spline shape. So even if you have one hair transform but it contains several hair shapes, you have do it again and again.

Hence, the script below. Basic, but it did saved me tedious tasks every now and then.

P.S. Ideally hair should be from native object from its host DCC but sometimes using an existing hair splines saves time specially if its just a one-off render illustration.

import maya.cmds as cmds

# 1. Name of the shader you want to assign
shader_name = "hair_mat" 

# 2. Get ALL NURBS curve shapes in the scene
# 'long=True' ensures we get the full path to avoid naming conflicts
all_curves = cmds.ls(type="nurbsCurve", long=True)

for shape in all_curves:
    # 3. Filter out "Intermediate Objects" 
    # (These are invisible curves used for rig history/calculation that you shouldn't render)
    if cmds.getAttr(f"{shape}.intermediateObject"):
        continue



    # A. Enable 'Render Curve'
    cmds.setAttr(f"{shape}.aiRenderCurve", 1)
    cmds.setAttr(f"{shape}.aiMode", 0) #0 ribbon #1 tubes
    cmds.setAttr(f"{shape}.aiCurveWidth", 0.05)
    cmds.setAttr(f"{shape}.aiMinPixelWidth", 0.4)
    
    # Get the short name (the part after the last '|') to check the prefix. For selective paramater assignment
    short_name = shape.split('|')[-1]

    if short_name.startswith("hair_mainSpline"):
        curve_width = 0.02    
                
    # B. Connect the Shader
    try:
        cmds.connectAttr(f"{shader_name}.outColor", f"{shape}.aiCurveShader", force=True)
        print(f"Connected {shader_name} -> {shape}")
    except Exception as e:
        print(f"Skipped {shape}: {e}")

print(f"Batch assignment complete for {len(all_curves)} curves.")