Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5938

Modify Rhino PBR Material With GH Python Component

$
0
0

Hello,

I’m using the new Rhino 8 Material nodes to create custom materials from within GH but the features missing are being able to control the Roughness, Opacity/Transparency of said materials.

I am attempting to create a python node that modifies a PBR material from within GH but stuck with the method to actually find the referenced material.

You can see below I’m taking a new model material, getting its ID, and giving that to python to then find that material and set Material Attributes from my python inputs (color, sliders, etc). (Or at least this is what I am trying to create.

Here’s a snip of the graph:

Here’s the baked result (Roughness always gets set to 100):

Here’s the intended result (Roughness gets set to user defined value from GH):

The “Create Material” component can influence the opacity from GH but cannot influence Roughness

Here is the code thus far:

import random
import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
import Rhino.Render as rr
import System.Drawing as sd

def set_material_parameters(material_name, diffuse, roughness, opacity):
    rhino_doc = sc.doc
    render_materials = rhino_doc.RenderMaterials
    
    material_index = None
    for i in range(render_materials.Count):
        material = render_materials[i]
        if material.Name == material_name:
            material_index = i
            break
    
    if material_index is not None:
        # Get the current material name
        current_name = rs.MaterialName(material_index)
        
        # Set the new material name
        previous_name = rs.MaterialName(material_index, "Your New Material Name")
        
        material = render_materials[material_index]
        
        material.BeginChange(rr.RenderContent.ChangeContexts.Program)
        
        # Set diffuse color
        material.SetParameter("diffuse", sd.Color.FromArgb(diffuse[0], diffuse[1], diffuse[2]))
        
        # Set roughness
        material.SetParameter("roughness", roughness)
        
        # Set opacity
        material.SetParameter("opacity", opacity)
        
        material.EndChange()
    else:
        print("Material not found!")

# Input parameters
material_name = "Your Material Name"  # Input material name as a string
diffuse = Diffuse
roughness = Roughness
opacity = Opacity

#print diffuse
#print roughness
#print opacity

set_material_parameters(material_name, diffuse, roughness, opacity)

Thank you for your help and any leads! Always appreciated!

8 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 5938

Trending Articles