ScriptSpot home

Forum topic · General Scripting

De-Instance materials

By David_Lee · 2013-06-30

Description

I need to rename all the SubMaterials in scene adding a "Sub_" prefix.
To doing this, I wrote a simple script:


for o in sceneMaterials do
(
        if (classof o == Multimaterial) then 
	(
		for i in o.materialIdList do
		(
			try
			(
				subo = o[i]
				subo.name = "Sub_"+subo.name
			)
			catch()
		)
	)
)

But the problem is that sometimes, a submaterial is an instance of another material in scene and if I rename the submaterial, the master material is renamed too.
So, how can I check if the submaterial is an instance and, if it is, make it unique and then rename it?

Someone could help me, please?

Thanks,
David

Comments (5)
miauu

To find all instances of a material check this

To make material unique see what is written in maxscript help file:

assignNewName <material> 

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 it’s unique.

Reply to miauu
David_Lee

Tried using assignNewName, but it doesn't make the submaterial unique. It rename both submaterial and master material as "Material #n".

Reply to David_Lee
miauu

Same behaviour here. Maybe we don't know how to use it. I started a thread in cgtalk. Hope someoane to gives us the answer. :)

Reply to miauu
David_Lee

It works!!

Thanks a lot miauu!!
I know how to use it, I have the answer! :)


for o in sceneMaterials do
(
        if (classof o == Multimaterial) then 
	(
		for i in o.materialIdList do
		(
			try
			(
				subo = o[i]
				tmp = copy subo
				o[i] = tmp
				o[i].name = "Sub_"+subo.name
			)
			catch()
		)
	)
)
Reply to David_Lee
miauu

I think I found a sloution:

The first slot in Material Editor is a multisub. All submaterials are instances of subMat[1]


-- the first slot is multisub mterial. All submat-s are instances of submat[1]
msm = meditmaterials[1]
-- get the second submat
-- create a copy
tmp = copy msm[2]
-- assign the copy back 
msm[2] = tmp