[Closed] Max Script Newbie – Select objects by Material Name
Hi,
-I use an array to extract and store the material names “from the current scene”.
-I then use this array to display these Material names in a dropdown list (thanks Rens! )
-I now need to find out how to select objects from my scene by the current material selection in the dropdown list :shrug:
(I have RTFM and TFM doesn’t seem to point me in the right direction – FM!! )
if anyone could help?!
TFM cannot show you EVERY possible solution for every possible problem! :)
Anyway, you have 2 ways to implement this:
*If you want to search by name, you can do the following:
(
theNames = #()
for o in Geometry where
o.material != undefined and findItem theNames o.material.name == 0 do
append theNames o.material.name
rollout theRollout "Select By Material"
(
dropDownList myDropDownList items:theNames
on myDropDownList selected itm do
select (for o in Geometry where try(o.material.name == myDropDownList.selected)catch(false) collect o)
)
createDialog theRollout
)--end script
or
(
theNames = for m in SceneMaterials collect m.name
rollout theRollout "Select By Material"
(
listBox myDropDownList items:theNames --show more than 1 material at a time!
on myDropDownList selected itm do
select (for o in Geometry where o.material != undefined and o.material.name == myDropDownList.selected collect o)
)
createDialog theRollout
)--end script
In both cases, you have to check whether the object has a material, as you cannot get the .name from undefined. In the first example, the error is being trapped by try()catch(). In the second case, the material is compared to undefined before checking the name.
*If you want to search by the material itself, you would have to collect two arrays – the one with the names to be displayed, and anotherone with the actual materials. Then you can select by comparing the material instead of the name. This has the positive affect that even if the material’s name was changed in the meanting, the material instance itself would still be the same, so the objects using that material would still be selected:
(
theMaterials = for m in SceneMaterials collect m
theNames = for n in theMaterials collect n.name --collect their names, use this array in the dropdown list
rollout theRollout "Select By Material"
(
dropDownList myDropDownList items:theNames
on myDropDownList selected itm do
select (for o in Geometry where o.material == theMaterials[itm] collect o)
)
createDialog theRollout
)--end script
Now let me know WHAT part of this is not explained in TFM? For loops? Arrays? Try()Catch()? Material access? Name access of materials?
Or did you expect the whole example to be there in the FAQ, waiting for you?
You can bet it will be in the next update of TFM :) Just not today...
:applause:
WOW!
As of yet I have only skimmed over your reply and am still taking it in! Thaks for spending time bothering to answer what was possibly a simple question in such depth!
- I’m only working on Max Script inbetween projects so it’s stop/start etc which isn’t too helpful.
I need to look at your script and try and work out myself what it does (although your explanation is good I need to get my head around it ).
The problems I get with the manual stem from not having the experience to understand the errors within the listener, and arrange the script in the correct way! (although I am getting <slightly> better!) This time last week, I hadn’t even looked at Max Script, so Arrays/Loops are still all a bit new to me!!!
Thanks again! …I’ll let you know how I get on…
Geri