Change gamma from python

Hello!
How can I change material’s gamma from python script?
image
Thank you!

@andy, @nathanletwory - is this something you can help with?

You can change parameters on RenderContent using the GetParameter() and SetParameter() API. This works for RenderMaterial, RenderTexture and RenderEnvironment.

To find out what the parameter name is you do that easiest by saving your (top) render content as a file. Simplest is by dragging the material or environment to the Windows file explorer. Then open the resulting .rtml file in your favourite text editor and search for the parameter name. If you have a hard time guessing what the name could be just set a special value you can search for, say 99.99. Do the search and take note of the parameter name.

variant = rc.GetParameter(yourparameternamehere)
print(variant.ToDouble(None)) # or ToBoolean, ToSingle, ToUInt64 etc. use dir(variant) to find out what the possibilities are

To change:

rc.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
rc.SetParameter(parametername, 4.4)
rc.EndChange()

Here a short video if the text alone wasn’t clear enough

2 Likes

Thank you!

Hello,

If you want to change the gamma value of bump/normal map for all PBR materials, the script compiler will give an error prompt:
Message: ‘PhysicallyBasedMaterial’ object has no attribute ‘GetTextyreFromUsage’

what shall I do?

Thanks

Hi @Alen_Russ, this should be spelled GetTextureFromUsage i guess.

_
c.

hi,

this is the python code from nathanletwory, i dont know how to do for this

import Rhino.Render as rr
import scriptcontext as sc

for rm in sc.doc.RenderMaterials:
    #rt = rm.GetTextyreFromUsage(rr.RenderMaterial.StandardChildSlots.Diffuse)
    rt = rm.GetTextyreFromUsage(rr.PhysicallyBasedMaterial.ParametersNames)
    variant = rt.GetParameter("rdk-texture-adjust-gamma")
    print(variant.ToDouble(None))
    rt.BeginChange(rr.RenderContent.ChangeContexts.Program)
    rt.SetParameter("rdk-texture-adjust-gamma", 2.2)
    rt.EndChane()
    

It’s RenderMaterial.GetTextureFromUsage.

– Dale

Hi @Alen_Russ, there are some more typographic errors, please try below:

import Rhino.Render as rr
import scriptcontext as sc

for rm in sc.doc.RenderMaterials:
    rt = rm.GetTextureFromUsage(rr.PhysicallyBasedMaterial.ParametersNames)
    variant = rt.GetParameter("rdk-texture-adjust-gamma")
    print(variant.ToDouble(None))
    rt.BeginChange(rr.RenderContent.ChangeContexts.Program)
    rt.SetParameter("rdk-texture-adjust-gamma", 2.2)
    rt.EndChange()

_
c.

1 Like

hi clement,

i have tested your code, it show this

Message: expected StandardChildSlots, got type

Traceback:
  line 5, in <module>,

Hi @Alen_Russ, the original code is not by be, below seems to work and changes gamma to 2.2 for diffuse channel texture.

import Rhino.Render as rr
import scriptcontext as sc

for rm in sc.doc.RenderMaterials:
    slot = rr.RenderMaterial.StandardChildSlots.Diffuse
    rt = rm.GetTextureFromUsage(slot)
    if not rt: continue
    
    variant = rt.GetParameter("rdk-texture-adjust-gamma")
    print(variant.ToDouble(None))
    rt.BeginChange(rr.RenderContent.ChangeContexts.Program)
    rt.SetParameter("rdk-texture-adjust-gamma", 2.2)
    rt.EndChange()

_
c.

thank you. it works well