[Closed] [Solved] Assign material script problem
Since the cgtalk search is not working i’ll try asking here. I have a simple maxscript that loads a material library with one button and with other buttons assigns materials to selected objects. However it only works the first time. If I want to use a different material (different button) the material will remain the first material . Any idea what could be wrong?
(
rollout MATLIB "Mat Library" width:500 height:500
(
button mat01 "mat01" pos:[70,20] width:200 height:21
button mat02 "mat02" pos:[70,50] width:200 height:21
button mat03 "mat03" pos:[70,80] width:200 height:21
button mat04 "mat04" pos:[70,110] width:200 height:21
button mat05 "mat05" pos:[70,140] width:200 height:21
button loadmat01 "load material A - N" pos:[20,470] width:200 height:21
button loadmat02 "load material O - Z, 1 ,2" pos:[300,470] width:200 height:21
on loadmat01 pressed do
(
loadMaterialLibrary @"C:\matlib01.mat"
MatArray = #("mat01",
"mat02",
"mat03",
"mat04",
"mat05",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace",
"fillspace")
for i=1 to MatArray.count do
(
MeditMaterials[i] = currentMaterialLibrary[MatArray[i]]
)
)
on mat01 pressed do
(
clearSelection()
--- select the obj
for obj in geometry do
(
if obj.material != undefined and obj.material.name == "door" then
(
selectMore obj
)
--- apply mat to selection
for obj in selection do
(
obj.material = meditMaterials[1]
)
)
)
on mat02 pressed do
(
clearSelection()
--- select the obj
for obj in geometry do
(
if obj.material != undefined and obj.material.name == "door" then
(
selectMore obj
)
--- apply mat to selection
for obj in selection do
(
obj.material = meditMaterials[2]
)
)
)
)
createdialog MATLIB style:#(#style_titlebar, #style_sysmenu, #style_resizing)
)
The condition on each of your if statements is that the material name is “door.” But hen you reassign it with materials from your material library, the name changes to whatever the name is of the material. So if you go to apply a different material, the name is no longer “door” so it evaluates as false and does nothing.
This is kind of an assumption as I am not running your code, but that is what it looks like.
Nice catch! I was selecting objects by material instead of obj name. So stupid of me. Thank you so much.