Hello,
I'm hoping for some help to create a handy little script to replace a few hundred materials on my imported geometry.
The materials of my imported geometry are named like this:
Wood
Wood #01
Wood #02
Wood #03
and so forth...
This incremental naming also occurs for dozens of other materials (e.g. metal, bark, brick, plastics, etc)
If it's not too much to ask, can i request some help with my maxscript code?
The script should recognize scene materials starting with 'Wood', and replace them with a material from my material library.
I do have some knowledge of maxscript, but this one is eluding me unfortunately ๐
This is what I have now:
It works, but will only replace 'Wood' with my library material called 'Cypress'. I just can't figure out how to also make it work for Wood #01, Wood #02, Wood #03, etc
tmpMtlLib = loadTempMaterialLibrary @"\\<location of my material library>.mat" setAppData tmpMtlLib["Cypress"] 1 "Wood" saveTempMaterialLibrary tmpMtlLib @"\\<location of my material library>.mat" importselection = objects as array for i in objects do ( if ( classof i.material == standard ) then ( importedmat = i.material matnameasstring = importedmat.name as string tmpMtlLib = loadTempMaterialLibrary @"\\<location of my material library>.mat" for mtl in tmpMtlLib where (getAppData mtl 1) == matnameasstring do ( replaceInstances importedmat mtl ) ) )
Is this an easy problem to solve?
Can i use * (wildcard) to make it pick up the incremental naming?
I appreciate any help at all
Thanks!
Have a look at the matchpattern() function in this topic:
So you could do a compare like this:
matnameasstring = importedmat.name as string patname = matnameasstring + "*" tmpMtlLib = loadTempMaterialLibrary @"\\<location of my material library>.mat" for mtl in tmpMtlLib where (matchpattern (getAppData mtl 1) pattern:patname) do ( ...
Hope that helps,
Drew
Thanks very much Drew,
Unfortunately it didn't work. I'm getting this error:
I really have no idea what's wrong here, i'm a real rookie with maxscript ๐
#materialLibrary() OK true #($Dummy:Default @ [-254.031021,-185.199951,0.000000], $Dummy:Cube @ [-254.031021,-185.199966,50.000000], $Dummy:Cube @ [151.904984,-185.199982,50.000000], $Editable_Mesh:Cube @ [151.904984,-185.199982,50.000000], $Dummy:Cube #1 @ [-254.031021,-30.270645,50.000027], $Dummy:Cube @ [-254.031021,-30.270645,50.000027], $Editable_Mesh:Cube @ [-254.031021,-30.270645,50.000027], $Dummy:Cube #2 @ [-254.031021,134.675537,50.000050], $Dummy:Cube @ [-254.031021,134.675537,50.000050], $Editable_Mesh:Cube @ [-254.031021,134.675537,50.000050], $Dummy:Cube #3 @ [-48.765884,-185.199966,50.000000], $Dummy:Cube @ [-53.360161,-185.199966,50.000000], $Editable_Mesh:Cube @ [-53.360161,-185.199966,50.000000], $Dummy:Cube #4 @ [-48.765884,-30.270645,50.000027], $Dummy:Cube @ [-48.765884,-30.270645,50.000027], $Editable_Mesh:Cube @ [-48.765884,-30.270645,50.000027], $Dummy:Cube #5 @ [-48.765884,134.675537,50.000050], $Dummy:Cube @ [-48.765884,134.675537,50.000050], $Editable_Mesh:Cube @ [-48.765884,134.675537,50.000050], $Dummy:Cube #6 @ [151.904984,-185.199966,50.000000], ...) -- Error occurred in mtl loop; filename: C:\scripts\Test4.ms; position: 525; line: 14 -- Frame: -- mtl: DP_White:CoronaMtl -- called in i loop; filename: C:\scripts\Test4.ms; position: 581; line: 16 -- Frame: -- i: $Cube -- patname: "Wood #2*" -- importedmat: Wood #2:Standard -- matnameasstring: "Wood #2" -- Unable to convert: undefined to type: String
Here's the current script with your suggestion:
Is there anything wrong here? Thanks again
tmpMtlLib = loadTempMaterialLibrary @"C:\scripts\testlibrary.mat" setAppData tmpMtlLib["Cypress"] 1 "Wood" saveTempMaterialLibrary tmpMtlLib @"C:\scripts\testlibrary.mat" importselection = objects as array for i in objects do ( if ( classof i.material == standard ) then ( importedmat = i.material matnameasstring = importedmat.name as string patname = matnameasstring + "*" tmpMtlLib = loadTempMaterialLibrary @"C:\scripts\testlibrary.mat" for mtl in tmpMtlLib where (matchpattern (getAppData mtl 1) pattern:patname) do ( replaceInstances importedmat mtl ) ) )
Can't find what you're looking for? Ask the community or share your knowledge.