Add material with python script

Hello,

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?

AssignPBR.py (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.

-Pascal

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 https://jesterking.github.io/rhipy/create_pbr_material_with_textures.html :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) (rhino3d.com). 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 (rhino3d.com) 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
    o.CommitChanges()

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.
AssignPBR.py (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
    ob.CommitChanges()

(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