[Closed] assignNewName gives not always unique name to material
So here what the max docu says about assignNewName:
Modifies the name of the specified material to make it unique. The name is of the form “Material #1” where the number is incremented as required to make ensure its
unique.But it´s not always worcking, here some example:
mat=standard()
assignNewName mat
let say mat.name is “Material #1”
now I do the folowing
mat2=standard name:“Material #2”
mat3=standard()
assignNewName mat3
now mat2 and mat3 have exactly the same name. Any chance to get really unique name for material?
It does work for me:
mat2 = standard name:"Material #2"
mat3 = standard()
assignNewName mat3
print mat2.name
-- outputs "Material #2"
print mat3.name
-- outputs "Material #25"
Sorry my bad, or to be more precise my bad english. try to change your code to:
mat2 = standard name:"Material #25"
mat3 = standard()
assignNewName mat3
print mat2.name
print mat3.name
Oh I see the problem now. assignNewName doesn’t seem to work as the help file says, maybe is a bug?
You could use something like this to get a material name that is not used in the scene:
fn getNewMaterialName = (
count = 0
do (
count += 1
materialName = ("Material #" + count as string)
) while sceneMaterials[materialName] != undefined
return materialName
)
Not shure if I understood you right, but if yes there could be even a bit easier solution. What is that sceneMaterials array? Is it some kind of dictionary with all scene materials?
I guess you need to run assignNewName function each time new material is created
(
resetMaxFile #noPrompt
mat2 = standard name:"Material #25"
assignNewName mat2
mat3 = standard()
assignNewName mat3
format "%
" mat2.name
format "%
" mat3.name
)
In this case you could try to get a list of all the material names by class, this way you get all the materials regardless if they are inside a submaterial or not. And then search for a new name using this list:
fn getAllMaterialNames = (
names = #()
for materialClass in material.classes do (
for m in (getClassInstances materialClass) do (
append names m.name
)
)
return names
)
fn getNewMaterialName = (
count = 0
materialNames = getAllMaterialNames()
do (
count += 1
materialName = ("Material #" + count as string)
) while (findItem materialNames materialName != 0)
return materialName
)
newName = getNewMaterialName()
Alternatively, you can do just what Serejah said: execute assignNewName after the creation of every material, this might work too and it’s more straightforward.
OK, I try it. But cant I change getNewMaterialName to setNewMaterialName that looks something like this?
fn setNewMaterialName &mat = (
materialNames = getAllMaterialNames()
while (findItem materialNames mat.name != 0) do assignNewName mat
)