Add material with python script


I have modified an example found online to add a new PBR material to the scene. If I run this script again, it creates a new material labled …(1), …(2), etc. How can I keep track of the current number lable added? Can I find and delete / use materials added with previous runs of the script? (2.3 KB)

Also I have found this example python - How to replace all pixels of a certain RGB value with another RGB value in OpenCV - Stack Overflow which would help a bit with the preparation (since the metallic part of my .jpgs is always the same color). Can I inbuild this into my script so that I only need to prepare the “main_texture” and have the rhino python script do the rest for me?

Thank you in advance

Hello- do you mean, if you run the script prepeatedly? I guess you can add a counter to the the scriptcontext.sticky dictionary.


Or you could use a naming convention for your material, say “Kral PBR” and each time before you create the material instead first look through the RenderMaterials table to see if already such a material exist. If it does only change the texture on that material instance.

(And it looks like you found my PBR material creation example :slight_smile: )

1 Like

Yes, as mentioned, your example is the core of the script and I don’t think I could write it alone. In actual fact I don’t know how to script as can be seen by my futile attempts to utilise this website MaterialTable.Find Method (String, Boolean) ( From what I see the Find() accepts a string and a boolean but when I use “rd.Tables.MaterialTable.Find(“MyPBRTexture*”, True)” I get an error: “Message: Find() takes exactly 3 arguments (2 given)”. I guess anyone using it understands it all to well but moving from rhinoscript to rhinocommon I can’t properly understand what’s going on. Here Guides ( I found a lot information but it’s like “Use the shovel to dig a hole”. What I need is a guide on how to use the showel before I can get to digging the hole.

My goal is to to make a script that Can be used multiple times applying materials on single/multiple objects (surfaces) while overwritting the material already applied to the object.

If the script could create the metalness mask itself based on the provided texture that would be a great enhancement. I’d like to conquer this part alone as part of my learning but I need to know if it can be done inside Rhino python script or if I need to make it a standalone program?

I’ve been gethering all kinds of guides and I’ll be grateful for links to more.

Don’t use the Materials table, instead iterate over RenderMaterials table. I like to use list comprehension for this:

import scriptcontext as sc

myrm = [rm for rm in sc.doc.RenderMaterials if rm.Name=="MyPBRMaterial"]

Then make changes to your render material as you see fit and assign to each object you want it on.

import scriptcontext as sc

for o in sc.doc.Objects:
    o.RenderMaterial = myrm

If you only ever have one material and textures you created and named yourself then you could maybe just iterate over the render textures instead?

myrts = [rt for rt in sc.doc.RenderTextures if rt.Name=='MyPBRTexture']

From what I found around the forums it seems I’m getting objects mixed up with guids or vice versa. Both with materials and objects I got the lists I wanted but wasn’t able to work with them in any way. Now I’m getting a " getset_descriptor is not callable" (I coudn’t find what that means).

Anyway I’ve added the testing textures which I realize I’ve left out. (2.6 KB)

Lines 64-68 I’d replace with:

obs = [sc.doc.Objects.FindId(obid) for obid in objs_to_texture] # get all the RhinoObjects from the GUIDs
for ob in obs:
    ob.RenderMaterial = new_pbr

(One of) The mistake(s) you made was using AddMaterialToObject as a property, but it is a method. Anyway, the above snipped will ensure objects get the RenderMaterial new_pbr assigned.

1 Like

I would guess “(One of) The mistake(s)” I made was assuming that throwing random stuff into the script and checking on the variables comming out is a valid scripting process. :smiley: But what do I know I’m a beginner. With this piece fallen in I’ve managet to make the script do what I need. Thank you a lot for all the hints. Have a nice day.

Congrats! I am happy the hints helped you learning new stuff :slight_smile: Have fun scripting and using Rhino.

1 Like