Convert materials in scene to VrayOverrideMtl

Hi,

I'm trying to simplify a long winded process, and think it may be achievable in MaxScript. Apologies my knowledge of MaxScript (and any programming) is extremely basic.

What I would like to do is convert every material in a scene to a VrayOverrideMtl, keeping the existing material in the base slot. Then apply a vrayMtl to the Reflect mtl slot, and change the diffuse color of this vrayMtl to black. I'd need the script to ignore certain objects, which could be defined as a selection, layer, etc. However thinking about this I could "cut and paste" the objects I don't want to be affected, but if it could be done through scripting then that would be great.

My main problem is that I can't see how to change the material type through scripting. I can see how it works using the listener, and manually assigning the new material type, but that's not really how I think it should work. Ideally I think I should be using something like this -

for mat in scenematerials do
(
(
if iskindof o.mat VrayOverrideMtl do
(
)
else
(
SCRIPT TO CHANGE MATERIAL TYPE, ETC
)
)

Any help would be really appreciated.

Thanks,
Dean

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
barigazy's picture

...

Try this function. If fn argument"selectedObjs" is set to "on" then fn works on selected objects only or if is set to "off" then on scene materijals. Also this fn respect instanced materials.

fn convertToOverideMtl selectedObjs: =
(
	local mtls = #()
	fn checkmat mtl arr = (mtl != undefined and not iskindOf mtl VrayOverrideMtl and findItem arr mtl == 0)
	if selectedObjs then 
	(
		if selection.count > 0 do (for o in selection where (checkmat o.mat mtls) do append mtls o.mat)
	)
	else 
	(
		for m in scenematerials where (checkmat m mtls) do append mtls o.mat
	)
	if mtls.count > 0 do for oldMtl in mtls do
	(
		newMtl = VrayOverrideMtl name:(oldMtl.name + "_Override") baseMtl:(copy oldMtl) reflectMtl:(vraymtl diffuse:black)
		for n in refs.dependents oldMtl do case of
		(
			(isvalidnode n and n.mat == oldMtl): n.mat = newMtl
			(iskindof n material): for i = 1 to (getNumSubMtls n) where (getSubMtl n i) == oldMtl do setSubMtl n i newMtl
		)
	)
)

After you run fn for the first time use belowe code

convertToOverideMtl selectedObjs:on -- if you want to consider only selected objects

or this line

convertToOverideMtl selectedObjs:off -- this will works on scenematerials

barigazy

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.