[Closed] Navigating materials with maxscript
I’m writing a script for navigating materials in max, it feels hacky. What is the best way to add & remove maps?
for example:
-
I want to add a colorCorrect node in the diffuse slot to every vray material in my selection,with a unique name(cc_*) cool no problem.
-
Now I want to delete any colorCorrect created with the name (cc_*) leaving any children intact.
So originally my node hierarchy might be something like:
multimat>vrayMtl>mix>vrayHDRI…
After applying new map(in this case a cc):
multimat>vrayMtl>CC_*>mix>vrayHDRI…
upon removal it would again look like:
multimat>vrayMtl>mix>vrayHDRI…
So anyway I have a script working but it’s CC map specific and I’d rather get something to work for any map that you’d want to apply. Is there a “get child nodes” or “remove” material built in function or some classy way of adding and removing any map leaving any hierarchy intact?
hope this makes sense… :rolleyes:
Something along these lines will probably help you…
StandardMaterial diffuse:Mix()
for o in scenematerials where o.diffuse_texmap != undefined do
(
tempMat = o.diffuse_texmp
o.diffuse_texmap = colorCorrection map:tempMat
)
for o in getclassinstances colorCorrection where matchpattern o.name pattern:"CC_*" do
(
replaceinstances o o.map
)
cool, nice one!
I’ve created some functions:
--Apply.
fn addMap m =
(
for o in selection do
(
tempMat = o.material.texmap_diffuse
o.material.texmap_diffuse = m map:tempMat
)
)
addMap colorCorrection
--Remove.
fn removeMap m p =
(
for o in selection do
(
for o in getclassinstances m where matchpattern o.name pattern:p do
(
replaceinstances o o.map
)
)
)
removeMap colorCorrection "Color_*"
Ok,
I’ve updated the code and this is working great for the “colorCorrection” map, however if I change it to a “colorCorrect” map it breaks on the removeMap function: replaceinstances m m.map as it can’t find the “.map” attribute for a colorCorrect as it’s looking for a “.src_tex”.
I think a potential solution may be to get the first map slot of any map and assume that in this case it might always be the one we want?
--Array of unique maps so that maps only get processed once.
uniqueMapArray = #()
i = 1
--Apply.
fn addMap mapType mapName =
(
for o in selection do
(
--If VrayMat:
if isKindOf o.material VRayMtl then
(
if ((finditem uniqueMapArray o.material.name) == 0) do
(
append uniqueMapArray o.material.name
tempMat = o.material.texmap_diffuse
o.material.texmap_diffuse = mapType name:( mapName + "_0" + i as string ) map:tempMat
)
i = i + 1
)
--If VrayMat is a submaterial:
else if (isKindOf o.material Multimaterial or isKindOf o.material compositematerial) then
(
for subMap in o.material.materialList where isKindOf subMap material do
(
--Go back and do "addMap" :
addMap subMap mapName
)
)
)
)
addMap colorCorrection "mapWrangler"
--Remove.
fn removeMap mapType namePattern =
(
for o in selection do
(
for m in getclassinstances mapType where matchpattern m.name pattern:namePattern do
(
replaceinstances m m.map
)
)
)
removeMap colorCorrection "mapWrangler_*
“