Community
3ds Max Programming
Welcome to Autodeskโ€™s 3ds Max Forums. Share your knowledge, ask questions, and explore popular 3ds Max SDK, Maxscript and Python topics.
cancel
Showing results forย 
Showย ย onlyย  | Search instead forย 
Did you mean:ย 

MAXSCRIPT HELP REQUEST- Replace many scene materials with a library material

2 REPLIES 2
Reply
Message 1 of 3
willchocks
1809 Views, 2 Replies

MAXSCRIPT HELP REQUEST- Replace many scene materials with a library material

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!

 

2 REPLIES 2
Message 2 of 3
drew.avis
in reply to: willchocks

Have a look at the matchpattern() function in this topic:

http://help.autodesk.com/view/3DSMAX/2019/ENU/?guid=__files_GUID_A6A60FC7_6206_4FFC_80E2_0EF8544BE2C...

 

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

 



Drew Avis
Content Experience Designer
Message 3 of 3
willchocks
in reply to: drew.avis

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.

Post to forums  

Autodesk Design & Make Report