Notifications
Clear all

[Closed] Changing specular and ambient color

 D4z

Hi everyone,

i’m wanting to change the specular and ambient color of the all the materials in my scene. Having a huge number of objects, it’s going to take forever changing them one by one though, which is why i’ve been looking into Maxscript to get the work done. Thing is, I’m not at all effecient with MaxScript :shrug:.

Any idea how it could be done ?

Thx

8 Replies

We had this question over at http://area.autodesk.com/forum/t44111

I have a little script that I usually keep open  for just such a thing, but this is for the current material editor  selection.  I just add new lines to it that I use often,                       comment out the ones I don’t need, and then  evaluate.                                                                 

Working on video game environments I will often have large multi-subs that I need to make batch changes to so that is what this is geared to, but maybe you can get some direction off this.


    currMat = medit.getCurMtl()
  
  if classof(currMat) == Multimaterial do
  (
 	 for i = 1 to (currMat.materialList.count) do
 	 (
 		 if classof(currMat.materialList[i]) == StandardMaterial do
 		  (
 			 --currMat.materialList[i].specularLevelMap = undefined
 			  --currMat.materialList[i].specularLevel = 0
 			  --currMat.materialList[i].glossiness = 10
 			  --currMat.materialList[i].diffuseMapEnable = off
 			  currMat.materialList[i].diffuseMap = undefined
 			 currMat.materialList[i].Diffuse = color 128 128 128
 			  --currMat.materialList[i].diffuseMap = currMat.materialList[i].selfillumMap
 			 --currMat.materialList[i].selfillumMap = undefined
 			  --currMat.materialList[i].selfIllumAmount = 0
 			  --currMat.materialList[i].Diffuse = color 0 0 0
 			 --currMat.materialList[i].filterMap = undefined
 		 )
 	 )
 )
    

Just add some lines in there for specular and ambient.

 D4z

Thx for the reply Andrew, most appreciated.

I’ve been trying out your script, and i’m having a few problems getting it to run.
When I evaluate it, I get the following message in the listener:
“test_tex:Standard
undefined
OK”
but nothing happens.

If I try to evaluate just one of the lines that modify a property in the material, I get the following error message:
“– Unknown property: “materialList” in test_tex:Standard”

Am I doing something wrong ?

Thx

The sample is using a multi-sub material, and works on the currently selected material slot.

Here is something a bit more versatile:

currMat = medit.getCurMtl()
 
 --material change function (can be shared no matter the material)
 fn changeMat theMat = 
 (
 	theMat.adLock = off --Unlink Ambient/Diffuse from one another
 	theMat.specular = color 0 255 255
 	theMat.ambient = color 255 255 0
 )
 
 if classof(currMat) == Multimaterial then
 (
 	for i = 1 to (currMat.materialList.count) do
 	(
 		if classof(currMat.materialList[i]) == StandardMaterial do
 		(
 			--send material to material change function
 			changeMat currMat.materialList[i]
 		)
 	)
 )
 else if classof(currMat) == StandardMaterial do
 (
 	--send material to material change function
 	changeMat currMat
 )
 D4z

Hi Andrew,

Sorry for the late reply and thx for the new code which works great.
One last question regarding the function that is created (I’m just trying to understand how it works) “fn changeMat theMat = “.

Correct me if I’m wrong, but from what I managed to gather from the MaxScript help, “fn” is the syntax used to call a function, “changeMat” is the name of the function, and finally “theMat” is the function’s parameters. Is that correct ?

Thx again for your help.

Adding more comments for you to clarify:


   --COMMENT: This sets a variable "currMat" based on the current material editor slot.
  currMat = medit.getCurMtl()
  
  --COMMENT: This is the function that we share when changing each material we encounter. It requires that a material be passed into it.
  fn changeMat theMat =
  (
  	 --COMMENT: Within this function, "theMat" is whatever material was passed into this function
  	 --COMMENT: Below you can change your colors to whatever you need or even add new lines for batch changing the material in other ways
  	 theMat.adLock = off --Unlink Ambient/Diffuse from one another
  	 theMat.specular = color 0 255 255
  	 theMat.ambient = color 255 255 0
  )
  --COMMENT: This IF statement will run if the material editor slot is on a Multi-sub.
  if classof(currMat) == Multimaterial then
  (
  	 --COMMENT: It will then process each material within the multi-sub
  	 for i = 1 to (currMat.materialList.count) do
  	 (
  		 if classof(currMat.materialList[i]) == StandardMaterial do
  		 (
  			 --COMMENT: If this ID of the multi-sub was a standard material then pass this material on to the changeMat function  to set its colors
  			 changeMat currMat.materialList[i]
  		 )
  	 )
  )
  --COMMENT: If the current material editor slot was not a multi-sub, but was a standard material
  else if classof(currMat) == StandardMaterial do
  (
  	 --COMMENT: We then pass this material on to the changeMat function to set its colors
  	 changeMat currMat
  )

That is a little more difficult than you think. Most mats have different parameters for ambient, specular, etc. When I need to mass modify I typically use getClassInstance which returns an array of all objects of that class in the scene and then modify the parameters as needed in the array. For example:

-- Get all instances of the material as an array
-- In this case the Standard Material class
theMats = getClassInstances Standard
-- Loop through all materials in the array and change the settings
for modMats in theMats do (
	-- Set ambient to Black for all Standard Materials
	modMats.ambient = (color 0.0 0.0 0.0)
	-- Set Specular to White for all Standard Materials
	modMats.specular = (color 255.0 255.0 255.0)
)

-Eric

 D4z

Thanks to the both of you for giving me a little bit more insight into all of this. Definitely a step in the right direction to understanding MaxScript.

 D4z

Thanks to the both of you for giving me a little bit more insight into all of this. Definitely a step in the right direction to understanding MaxScript a little bit more.