Updating scene materials from library.

I am trying to write what I thought was a simple script, but it's not doing what I'd expect. I'm trying to open a material library, and apply the materials in it to a scene (as you would manually with the put to scene button), to update the materials in the scene. For one thing, the "put to scene" button no longer works in 2011 as I found out here: http://usa.autodesk.com/adsk/servlet/ps/dl/item?siteID=123112&id=1499331...
So taking their advice, I tried to automate making a box, and applying my matlib materials to it one at a time to replace the existing materials. I can watch the box colors change, but it doesn't replace the scene materials, it just makes a new scene material of the same name, that then disappears when the next material is applied.

My test scene has 4 box's each with a different VRaymat applied. My matlib has the same 4 materials with different diffuse values.
This is the script I'm using to test with:

loadmateriallibrary "X:\\Dummy\\Custom Maps\\dummy.mat"
tb=box()
select tb
cml=currentmateriallibrary
cmlC=cml.count

Messagebox "pause" --just for feedback

for n=0 to cmlC do -- when I set it n=1 here it didn't seem to apply
-- the first material
(
tb.material = cml[n]
show n
n=n+1
messagebox "Pause" --so I have time to see the colors change
)
delete tb

Does anyone know a way to accomplish this that is compatible with 2010 and 2011, and does not involve applying the new material to each object in the scene individually?

Comments

Comment viewing options

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

I know this thread is old,

I know this thread is old, but as it helped me start a new script I'd still post this. what you're missing in your script is a update to the viewport after you apply the material with the script.

my working version of your script...

loadmateriallibrary "L:/STORES/Base Models/test.mat"
tb=box()
cml=currentmateriallibrary

for n=0 to cml.count do
(
tb.material = cml[n]
redrawViews()
messagebox "Pause"
)
delete tb

Anubis's picture

replaceInstances()

It seems replaceInstances() function could do the trick, just need to see how to identify which lib's mat to which scene mat to replace. For example, you can use their names, if they are the same.

my recent MAXScripts RSS (archive here)

xXDambielXx's picture

replaceinstances for scene materials

Thanks Anubis, that worked out great.. Took me quite a while to figure it out.. I'm very new to Maxscript, but I got it to compare my new library materials names to the ones in the scene and replace them if they matched..

Comment viewing options

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