I added a couple more lines (172, 174) that seem to produce the desired result; I have no idea whether it is optimal, sensible, efficient, scalable, etc.
for i=1 to theMaterials.count do
(
obj_list = (for o in Geometry where o.material == theMaterials[i] collect o)
obj_list_vis = (for o in obj_list where not o.isHidden collect o)
if obj_list.count == 0 do continue
if obj_list_vis.count == 0 do continue
re.addrenderelement (CMasking_Mask())
theelement = re.getrenderelement (re.numrenderelements() - 1 )
theelement.elementName = ("alpha_" + theNames[i] as string)
theelement.nodesMonoOn = true
theelement.nodesMonoIncludeMod= true
theelement.nodesMono = obj_list;
obj_list=#()
--maskEls = append maskEls theelement.elementName
)
Complete script:
/*
------------------------------------------------------------------------------------------------------
--> by Gabriel Sumner & durandal_1707 (big ThX for the original script structure and wireColor code)
--> timestamp: 23/05/15
-->Version 1.0 Alpha
-------------------------------------------------------------------------------------------------------
*/
/*Credits for code snippets (sorry if someone has been forgotten, his / her name shall be added here asap:
> Dave Wortleys tutorial for the basis idea of adding renderElements: https://davewortley.wordpress.com/2012/06/22/making-render-element-masks/
> Bobo "select object my material" : http://forums.cgsociety.org/archive/index.php/t-183739.html
>
*/
/* known issues:
- only Corona materials are renamed to shorten the naming of the saved renderElement
- only poly geometry is supported: lines need to have an editPoly on top of the stack to be recognized; Groups are not supported
- if the "create mono masks" button is pressed several times the script keeps adding new render elements, no check for existing elements at the moment
- open / close script window is somewhat buggy, needs optimizin'...
*/
/* TODO: planed functions (everyone is welcome to change the script in any way / participate in the development ;) )
- check for existing elements and warn the user to create additional or replace the existing once
- delete all mono mask elements
- set different path for render elements
- set different file format for render elements
*/
(
--try(destroydialog wireColorByMatRollout)catch() >> toDo dialog creation
re = maxOps.GetCurRenderElementMgr()
local theMaterials = for m in SceneMaterials collect m
/* Material Names List */
local theNames = for n in theMaterials collect n.name
/*Remove the word "Material" for easier file naming */
for i=1 to theNames.count do
(
try(
if i != undefined then
theNames[i] = replace theNames[i] (findstring theNames[i] "Material ") 10 ""
)catch()
)
local fixedPaletteColors = #
(
(color 255 0 0), /* 1 - RED */
(color 0 255 0), /* 2 - GREEN */
(color 0 0 255), /* 3 - BLUE */
(color 255 0 255), /* 4 - MAGENTA */
(color 255 255 0), /* 5 - YELLOW */
(color 0 255 255), /* 6 - CYAN */
(color 120 0 0), /* 7- DARK RED */
(color 0 120 0), /* 8 - DARK GREEN */
(color 0 0 120), /* 9 - DARK BLUE */
(color 120 0 120), /* 10 - DARK MAGENTA */
(color 120 120 0), /* 11 - DARK YELLOW */
(color 0 120 120), /* 12 - DARK CYAN */
(color 60 60 60), /* 13 - DARK GRAY */
(color 120 120 120), /* 14 - GRAY */
(color 255 255 255) /* 15 - WHITE */
)
struct materialWireColor
(
matRef, matName, wireColor
)
/* unique scene materials */
local sceneMats = #()
local sceneMatTags = #()
for sceneObject in (objects) do
(
if sceneObject.material != undefined then
appendIfUnique sceneMats sceneObject.material
)
/* pre-select wire colors from a fixed palette defined above */
for matIdx = 1 to sceneMats.count do
(
local wireColor = (color 128 128 128)
if(matIdx <= fixedPaletteColors.count) then
wireColor = fixedPaletteColors[matIdx]
local matName = undefined
if sceneMats[matIdx].name != undefined then
matName = sceneMats[matIdx].name
else
matName = "unnamed_material_" + matIdx
append sceneMatTags (materialWireColor sceneMats[matIdx] matName wireColor)
)
/* Main UI */
global rofWireColors
rollout wireColorByMatRollout "Assign wire colors by material" width:340 height:320
(
listbox materialListBox "Scene materials" pos:[16,16] width:179 height:9 items:(for matTag in (sceneMatTags) collect matTag.matRef.name)
button assignColorsButton "Assign wire colors" pos:[50,160] width:235 height:40
--button createMonoMasksButton "Create mono masks" pos:[50,240] width:235 height:40
colorPicker wireColorPicker "" pos:[208,40] width:112 height:112
label lbl1 "Pick wire color" pos:[216,16] width:72 height:24
on wireColorByMatRollout open do
(
try
(
materialListBox.selection = 1
wireColorPicker.color = sceneMatTags[1].wireColor
)catch()
)
on materialListBox selected mlbSelIndex do
(
try
(
wireColorPicker.color = sceneMatTags[mlbSelIndex].wireColor
)catch()
)
on wireColorPicker changed newColor do
(
selMatIndex = materialListBox.selection
if selMatIndex != undefined then
sceneMatTags[selMatIndex].wireColor = newColor
)
on assignColorsButton pressed do
(
/*assign wire colors to objects based on their material */
for sceneObject in (objects) do
(
for sceneMatTag in (sceneMatTags) do
(
if sceneObject.material == sceneMatTag.matRef then
sceneObject.wireColor = sceneMatTag.wireColor
)
)
/* close UI */
try
(
closeRolloutFloater rofWireColors
) catch()
)
)
rollout monoMasksRollout "Generate Mono Masks for assigned sceneMats" width:340 height:260
(
button createMonoMasksButton "Create mono masks" pos:[5,10] width:150 height:40
button deleteAllMonoMasksButton "Delete all mono mask passes" pos:[180,10] width:150 height:40 enabled: false
on createMonoMasksButton pressed do
(
/*
--Materials List
theMaterials = for m in SceneMaterials collect m
--Material Names List
theNames = for n in theMaterials collect n.name
--Remove the word "Material" for easier file naming
for i=1 to theNames.count do
(
theNames[i] = replace theNames[i] (findstring theNames[i] "Material ") 10 ""
)
*/
for i=1 to theMaterials.count do
(
obj_list = (for o in Geometry where o.material == theMaterials[i] collect o)
obj_list_vis = (for o in obj_list where not o.isHidden collect o)
if obj_list.count == 0 do continue
if obj_list_vis.count == 0 do continue
re.addrenderelement (CMasking_Mask())
theelement = re.getrenderelement (re.numrenderelements() - 1 )
theelement.elementName = ("alpha_" + theNames[i] as string)
theelement.nodesMonoOn = true
theelement.nodesMonoIncludeMod= true
theelement.nodesMono = obj_list;
obj_list=#()
--maskEls = append maskEls theelement.elementName
)
/*update Render Setup Window */
if renderSceneDialog.isOpen()==true do
(
renderSceneDialog.close()
renderSceneDialog.open()
)
/*show Render Setup Window */
renderSceneDialog.open()
/* close UI */
try
(
closeRolloutFloater rofWireColors
) catch()
)
/*
on deleteAllMonoMasksButton pressed do
(
)*/
)
/* prevent the user from opening more than one dialog at a time */
rollout miscRollout "Toolbox" width:340 height:260
(
label lbl1 "Select objects by mat" pos:[15,5] width:200 height:16
dropDownList matsDropDownList items:theNames
on matsDropDownList selected itm do
select (for o in Geometry where o.material == theMaterials[itm] collect o)
label lbl2 "Set different path for render elements" pos:[15,55] width:200 height:16 enabled:false
editText edt1 "" pos:[10,75] width:200 height:25 enabled:false
button btn1 "Change path" pos:[220,75] width:85 height:25
enabled:false
label lbl3 "Set different file format for render elements" pos:[15,110] width:250 height:16 enabled:false
dropdownList ddl_file_ext "" pos:[12,125] width:110 height:40 items:#(".avi", ".bmp", ".cin", ".eps", ".ps", ".exr", ".fxr", ".hdr", ".pic", ".jpg", ".png", ".rgb", ".rla", ".rpf", ".tga", ".tif", ".dds")enabled:false
)
rofWireColors = newRolloutFloater "Corona Mask Manager" 350 500
addRollout wireColorByMatRollout rofWireColors
addRollout monoMasksRollout rofWireColors
addRollout miscRollout rofWireColors
)