GHPython Make Material Component makes infinite materials

I wrote a ‘make material’ and a ‘bake material’ component, based in the work of many discussion at the forum. It creates the material very well but it creates infinite versions of the same material if I press ‘create’ and bake. Attached you will find the definition and the scripts. For example, if I do a material called ‘r1’ and assign it to a layer, it will create ‘r1(1)’ if I create the material again with my component. What I need is to reset the original material instead of creating so many.

"""Creates a new Material.

Returns a new Material, a Render Material and a GH Preview Material using the parameters set as inputs.
To bake into Rhino you need to use the 'Bake with Attributes' LocalCode component. The material will be baked into a layer.
Based on the work of Chris Hanley.

    Typical usage:
        Input a name string and a diffuse color and create a material.
        The rest of the inputs are optional.
        Connect the 'Material' output to assign the material to a geometry.
        Connect the 'GHMaterial' to a 'Preview' component to visualize your material with GH objects.

    Inputs:
        Name: name string
        diffuseColor: diffuse color
        specularColor: specular color
        emissioColor: emission color
        transparency: transparency from 0 to 1
        reflectionRange: reflection range from 0 to 1
        glossiness: glossiness from 0 to 1

    Outputs:
        Material: Render material for Rhino geometry
        GHMaterial: Material for GH preview """

__author__ = "palomagr"
__version__ = "2020.07.09"

#ghenv.Component.Name = "Make Material"
#ghenv.Component.NickName = "Make Material"

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs
import Grasshopper as gh
import scriptcontext as sc


class MyComponent(component):

    def RunScript(self, Name, diffuseColor, specularColor, emissionColor, transparency,
                  reflectionColor, glossiness, create):
        GHMaterial = None
        Material = None
        newMat = ''

        def IsMaterial(mat_index):
            "Verifies if a material exists in a given index"
            if mat_index >= 0 and mat_index < sc.doc.Materials.Count:
                mat = sc.doc.Materials[mat_index]
                a = not mat.IsDeleted
                b = mat.IsDeleted
                print(b, "sdfsdfsfs")
                return not mat.IsDeleted
            return False

        if create:
            sc.doc = Rhino.RhinoDoc.ActiveDoc
            num = sc.doc.RenderMaterials.Count

            for i in range(num):
                mat_from_table = rs.MaterialName(i)
                # print(Name, 'inputname',mat_from_table, 'this is the name')

                if IsMaterial(i):
                    print
                    'yes'
                if Name == mat_from_table:
                    print('yes yes')
                    rs.ResetMaterial(i)
                    newMat = sc.doc.Materials[i]
                else:
                    newMat = Rhino.DocObjects.Material()



            Reflection = min(max(reflectionColor, 0), 1);
            Smoothness = min(max(glossiness, 0), 1);
    
            newMat = Rhino.DocObjects.Material()
    
            newMat.DiffuseColor = diffuseColor
            newMat.SpecularColor = specularColor
            newMat.AmbientColor = diffuseColor
            newMat.EmissionColor = emissionColor
            newMat.Transparency = transparency
            newMat.ReflectionColor = specularColor
            newMat.Reflectivity = Reflection
            newMat.ReflectionGlossiness = Smoothness
            newMat.Name = Name
            material_gh = Rhino.Render.RenderMaterial.CreateBasicMaterial(newMat)
            GHMaterial = gh.Kernel.Types.GH_Material(material_gh)  # GH preview material
            Material = material_gh  # Rhino Material

        # return outputs if you have them; here I try it for you:
        return (Material, GHMaterial)

"""Bake Material"""

import Rhino
import scriptcontext as sc

if B:
    sc.doc.RenderMaterials.Add(M)

make material.gh (23.9 KB)

@andy is this something you can help with?

The code fairly clearly creates a new RenderMaterial every time. You’re going to have to write some code that looks through the RenderMaterials and checks for a similar name…or some other method of deciding which one to update. Then I would just delete the old.one and create a new one when you bake.

Use unique naming style to ensure that your old materials are correctly removed.

Yes, thanks, that is exactly what I thought I should do. found that the work of Chanley actually had the answer on how to write the ‘remove’ piece.