Please help:Adding RenderMaterial won't Undo

@pascal or @nathan can you help with this?

I am making a script to make some glass panels and I have made a definition to add a material if it doesn’t exist, and then assign it to an object. It works (but was a pain to figure out, I wish it was a simple rs.IsRenderMaterial(name) ,rs.AddRenderMaterial() option…)
But the problem is that if I undo the script then the material is still there.

And if I run the script a few times and delete the material a few times and undo a few times more after that then MORE identical materials pops up in the material list… with the same name. Isn’t that strange?
Do I have to add some stuff to make the creation of the material undoable?
Is this a bug, or are there better ways to do what I want?

def addMaterialToObject(obj_id):
    render_material = False
    matTable = sc.doc.RenderMaterials
    for mat in matTable:
        if mat.Name == "hh_GlassPanel":
            render_material = mat
    if not render_material:
        rhino_material = Rhino.DocObjects.Material();
        rhino_material.Name = "hh_GlassPanel";
        rhino_material.DiffuseColor = rs.coercecolor((200,240,235))
        rhino_material.Reflectivity = 0.9
        rhino_material.Transparency = 0.9
        print dir(rhino_material)
        # Use the Rhino material to create a Render material.
        render_material = Rhino.Render.RenderMaterial.CreateBasicMaterial(rhino_material, sc.doc)
        sc.doc.RenderMaterials.Add(render_material)
    rs.ObjectMaterialSource(obj_id, 1)
    obj = sc.doc.Objects.FindId(obj_id)
    obj.RenderMaterial = render_material
    obj.CommitChanges();
    sc.doc.Views.Redraw()

Here I have undone every test I did and then all of these pops back up, even if they were created after I undid and deleted the material:
image

oof. You’re out of my league here… I’ll wait on Nathan…

-Pascal

1 Like

So am I… :wink:

1 Like

I’m not sure if @nathan is going to help you, but @nathanletwory can by linking his literate example https://jesterking.github.io/rhipy/create_specific_rendermaterial.html .Through it you’ll find RenderMaterial.GlassMaterialGuid Property and generally be more happy that you learn a bit more about RenderContent, more specifically RenderMaterial. You’ll see the light and stop using Rhino.DocObjects.Material and rhinoscriptsyntax (He’s never really used it, so can’t help with that - it does too much weird magic inside that is detrimental to writing good scripts, IHHO)

1 Like

You are right, @nathan has only visited the forum once, read one post in one topic, so I doubt he’s deep into RenderMaterials, but we never know! Maybe he is so good that he only needed to look up one single thing since 2014! But he will get a surprise when he logs back on and find that he has been asked lots of questions though :smiley:

I’m not so sure… simplicity and ease of use usually makes me the happiest :wink:
Thanks for pointing me in the right direction. I found a workaround where I assign a material to a layer, so I doubt I’ll dive deep into the core of this caos though.

But PLEASE add my wish for a simple “rs.addRendermaterial()” command and python GetMaterailByName thingo. I wastly prefer simple stuff like this, it makes it so much easier to keep scripts small and to tweak 4 year old scripts too with out refill my work memory with complex stuff I don’t really understand.