Python Control of Picture (Srf) Transparency Settings

I am trying to automate the control of the transparency within a PNG and am unable to this with the implements that I’m aware of. I’ve built tools to automate the production of transparent PNGs where the perfs come in on that layer of the image (this way I don’t have to create a massive file size by perfing a surface) - works great when I do this manually. When I bring it in via python, the mask is not acknowledged - shows up white. I found a radio button that turns it on, but all this seems clunky.

I don’t want to render anything, but I’m trying to get a visualization from the shaded view that can be placed on the drawing - so I can print the linework with the perfs/material.

Need help, thx!

Like this?

#! python 3
import rhinoscriptsyntax as rs
picture = rs.GetObject(preselect=True)
mat = rs.ObjectMaterialIndex(picture)
value = 0.5
rs.MaterialTransparency(mat, value)

or do you mean the individual pixel transparency values?

Thx for the response, unfortunately that’s not pushing the masking for transparency, but instead just editing the entire material.

In my quest for this, this doesn’t appear to be exposed… but there is a radio button (what I circled initially) to toggle it on in the environment - but I’m needing to run this automatically and don’t want to interrupt the processing to hit that toggle, especially since it should just be exposed bool.

Thx!

Okay, yeah. The Rhino.DocObjects.Material class has a boolean AlphaTransparency property, but changing it seems to have no effect.

#! python 3
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

picture = rs.GetObject(preselect=True)
mat = rs.ObjectMaterialIndex(picture)
material = sc.doc.Materials[mat]
material.AlphaTransparency = True
material.CommitChanges()

yeah this is essentially where I got to, bug?

Try this:

#! python 3
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

picture = rs.GetObject(preselect=True)
mat = rs.ObjectMaterialIndex(picture)
material = sc.doc.Materials[mat]
render_material = material.RenderMaterial
child_name = "bitmap-texture"
texture = render_material.FindChild(child_name)
field_name = "use-alpha-channel"
texture.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Script)
texture.Fields.Set(field_name, True)
texture.EndChange()

No luck. Same result. Thx though.

Huh. It’s working for me.

What version of Rhino? I’m on 8 SR7. Running via Rhinocode.

You’re able to run the code and it goes from this:

to this:

I’m on R8 SR19, running from the script editor.

Here some tutorials on how to use RenderContent and how to investigate what a specific material or texture type contains: https://jesterking.github.io/rhipy/

It looks like you’re using the transparent color mask rather than the actual alpha channel. In that case, try this one:

#! python 3
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
import System

picture = rs.GetObject(preselect=True)
mat = rs.ObjectMaterialIndex(picture)
material = sc.doc.Materials[mat]
render_material = material.RenderMaterial
child_name = "bitmap-texture"
texture = render_material.FindChild(child_name)
texture.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Script)
texture.Fields.Set("has-transparent-color", True)
texture.Fields.Set("transparent-color", System.Drawing.Color.Black)
texture.Fields.Set("transparent-color-sensitivity", 0.5) # adjust as needed
texture.EndChange()
1 Like

Dude. You’re amazing. This completely worked! Appreciate the assist!

1 Like