[Closed] Select layers by prefix name
Hi,
I’m still new to max script and I’m writing this function to select all nodes in layers that matches a given prefix name.
so far I’m able to find the right layers, I just don’t know how to select objects in these layer.
here’s my code :
fn selectLayerPrefix str = (
local objArray = #()
for i= 1 to (LayerManager.count-1) where (
matchpattern (LayerManager.getLayer i).name pattern:(str+"*")
) do (
append objArray (LayerManager.getLayer i).nodes
)
select objArray
)
selectLayerPrefix "LGT_"
so my array gives me: objArray: #(nodes(), nodes()), I just what ton know how to select them.
Thanks!
It’s a little weird, but nodes isn’t a property of a layer. It’s a function that assigns the array of nodes to a variable.
Also, you’ll want to use JOIN instead of APPEND since the select command doesn’t like arrays inside of arrays.
fn selectLayerPrefix str =
(
local objArray = #()
for i= 1 to (LayerManager.count-1) where
(
matchpattern (LayerManager.getLayer i).name pattern:(str+"*")
)
do
(
(LayerManager.getLayer i).nodes &theNodes
join objArray theNodes
)
select objArray
)
selectLayerPrefix "LGT_"
A shorter version would be:
function selectNodesInsideLayerWithPrefix inPrefix =
(
local nodesInsideLayerWithPrefix = for nodeOn in objects where matchPattern nodeOn.layer.name pattern:(inPrefix +"*") collect nodeOn
if nodesInsideLayerWithPrefix.count > 0 then
select nodesInsideLayerWithPrefix
else
clearSelection()
)
selectNodesInsideLayerWithPrefix "LGT_"
if a function selects something by a condition it has to clear selection if nothing meets the criteria.
if list.count > 0 then select list else clearselection()
other way the selecting of empty list will keep the current selection, which is not correct.
Even shorter. With no arrays.
function selectLayerPrefix str =
(
clearSelection()
for i = 1 to LayerManager.count-1 where matchPattern (LayerManager.getLayer i).name pattern:(str +"*") do (LayerManager.getLayer i).select on
)
selectLayerPrefix "Control"
In my talk at EUE the year before last I detailed a layer control script that we used in production for many years. There are a lot of methods included that you could use for writing something similar.