Notifications
Clear all

[Closed] material change by name

weirdest thing. This scripts works perfectly fine with the exception of 1 thing. Here is what it is supposed to do. Change Material properties in scene only if it has a name. If it doesn’t have a name, i.e. .name = undefined, then leave it alone. However it seems to change everything named or not.

for i = 1 to meditmaterials.count do
(
	local changemat = meditmaterials[i].name
	if changemat != undefined then
	(	
	meditMaterials[changemat].opacity = 0 --100% transparent
	meditMaterials[changemat].specularLevel = 0 --no specular
	meditMaterials[changemat].specularMapEnable = off --no specular map
	meditMaterials[changemat].specularLevelMapEnable = off --no specularmap level
	meditMaterials[changemat].reflectionLevel - 0 --advanced reflection level
	meditMaterials[changemat].reflectionMapEnable = off --get rid of reflextions.
	meditMaterials[changemat].refractionMapEnable = off --no refraction
	)
)
renderers.production = Default_Scanline_Renderer()
renderers.production.AntiAliasing = true
renderers.production.objectMotionBlur = off
renderers.production.imageMotionBlur = off
renderers.production.shadows = on
--renderers.production.filterMaps = off --enable/disable

where am I going wrong here?

22 Replies

Maybe test for .name != “” as well. Could be that a null name is stored as an empty string…

hrmm… tried your suggestion, didn’t work. now (ok this is strange), example. I named material 1,2,3 something1, something2 something3. I run the script, it changes something1 and everything else except something2 and something3. It is supposed to change anything named, so why did it ignore something 2 and something 3. and why did it change the undefined ones???

as an experiment I changed this line

local changemat = meditmaterials[i].name
if changemat != undefined and changemat != "" then

to this

 local changemat = meditmaterials[i].name
if changemat == undefined and changemat == "" then

and it did nothing?? I am just flabbergasted. That should have changed everythign but it does the opposite.

2 Replies
(@focomoso)
Joined: 11 months ago

Posts: 0

I think you’re misunderstanding boolean operations. The second if statement will ever execute because changemat can never be both undefined and empty at the same time. Boolean ands are limiting (always make the set smaller) So you read it as, “give me everything that is both x and y” not “give me everything that is x and also give me everything that is y”. That’s why nothing’s getting changed. Switch the and to an or.

(@rustyknight)
Joined: 11 months ago

Posts: 0

Nice spot! Well done!

I took you code and said:


	if changemat == "" then (
		format "[%]
" changemat
	)

And this worked fine…

I don’t think your materials are named with empty strings…try the above and see what you get.

You could also play with the trimleft and trimright functions…so long as you don’t have an “undefined” value – but I can’t see the name been undefined

Shane

well thanks guys for helping me out. I didnt know that “” meant empty, I thought it mean any string entered/ I also didn’t know proper boolean arguments. So thanx for that as well.

both recommendations worked – sort of. focomoso and RustyKnight’s code changes made everything change. the only thing I need to change is materials with a name on it and not the default ones like 01 – Default.

I wil have to do some more meddling, if I get it to work right I will post the code.

The question I have is, why are you applying it to the materials in the editor? The reason I ask is because meditmaterials will be a material in the scene only if it is in the Material Editor. If you want to change the materials applied to something in the scene use scenematerials[i] which is an array of materials used in the scene and not just those currently in the materialeditor. I would reccomend reading up on “MaterialLibrary Values” in the maxscript reference.

-Eric

Glad to help.

I’ll add to PiXeL_MoNKeY’s concern that using empty names as a way differentiate materials is probably not good design to start with. You’re better off putting a unique marker into the names that you may want to change like: “skin_mat_changeable” “green_grass_ changeable” and then use findString to test for you marker:

if findString changemat “changeable” != undefined then …

This way you’re being proactive about the objects your code works on: just things you’ve explicitly marked will get through.

focomoso, PiXeL_MoNKeY: Awesome! Those are so good points you raised. the findString approach is the right way to go. Also I will use scenematerials[i] instead since I am writing a script for several projects with different artist they could be merging in data into the scene as well. So I will update the code. if it works I will post it as an example.

ok, I got alot of stuff running really well. I have to write several scripts for different kinds of reasons. But with your help I was able to get most of them working. Now I have to change materials into composite, and assign a texture and an ADD color. The only problem now is I am getting this error

--unable to convert: unidentified to type: Point 3
@ this location >>> [i]meditMaterials[sName].diffuse = noMaterial() --get rid of textures 1st[/i]
here's the sample code
if (findstring meditMaterials[i].name "input") == 1 then
(
	-- Get the material name for easier use...
	local sName = meditMaterials[i].name -- as integer
	-- Convert the sufix to an integer
	local iSufix = (substring sName 6 sName.count) as integer
	-- Check how big the integer is
	if iSufix != undefined and iSufix < 999 then
	(
	meditMaterials[sName].diffuse = noMaterial() --get rid of textures 1st
	meditMaterials[sName] = compositematerial () --change to composite materials
	----get uvtexture and put in main slot of composite materials
	meditMaterials[sName][#materialList].diffuseMap = Bitmaptexture fileName:"E:\3dsmax9\maps\uvtex.png"
	meditMaterials[sName][#materialList][#Maps][#Diffuse_Color__Map__1__uvtex_png].alphaSource = 2
	----------------------------------------------------------------
	meditMaterials[sName].materialList[2] = Standardmaterial () --setting up the added layer
	--added layer is blue
	meditMaterials[sName][#materialList][#Shader_Basic_Parameters].ambient = color 0 0 255
	meditMaterials[sName][#materialList][#Shader_Basic_Parameters].diffuse = color 0 0 255
	)
)
    
I tried changing sName to integer but this didnt help

meditMaterials[sName].diffuse is the color of the diffuse slot (a point3). You can’t assign a material (or no material) to a property that expects a color. You could change it to

meditMaterials[sName] = noMaterial()

but you don’t need that because in the next line you assign it to a compositematerial (just kill the line).

Also, a bit of coding practice: you start by referring to the material by it’s index [i] and then switch to using the material’s name [sName]. It won’t brake the code, but it makes it difficult to follow (it took me a sec to figure out what you were doing). Your best bet is, after the findstring test, to assign the material to a variable (m works well) and do all your work on that. Much easier to read.

Page 1 / 2