[Closed] function inside a function?
How do correctively make a function inside a function?
fn getAllChildren obj =
(
local tmpArr = #()
fn getChildren obj = (
for c in node.children do
(
append tmpArr c
getChildren c
)
)
tmpArr
)
children = getAllChildren $
fn getChildren obj tmpArr =
(
for c in obj.children do
(
append tmpArr c
getChildren c tmpArr
)
return tmpArr
)
fn getAllChildren obj =
(
local tmpArr = #()
tmpArr = getChildren obj tmpArr
)
children = getAllChildren $
select children
Is this more or less what you’re after?
fn getChildren obj =
(
local tmpArr = #()
for c in obj.children do
(
append tmpArr c
join tmpArr (getChildren c)
)
tmpArr --omitting "return" is faster!
)
children = getChildren $
Thank you for the help. Just incase anyone made need this.
This script below collects hierarchies of objects and then changes the wirecolor for each bunch of objects. Each bundle consists of complete hierarchies .
You don’t have to select the parent, it will automatically find the top most parent and then all it’s children.
fn getRoot node = ( --get top most parent node
if isvalidnode node do (--Check if the node passed to the function is a valid node.
while node.parent != undefined do node = node.parent; --Loop through the hierarchy until the current node's parent is undefined (i.e. rootnode)
node --Return the rootnode
)
)
fn getChildren node = (
local tmpArr = #()
for c in node.children do
(
append tmpArr c
join tmpArr (getChildren c)
)
tmpArr --omitting "return" is faster!
)
clearlistener()
parents = makeUniqueArray (for o in selection collect (getRoot o)) --unique array of parents
for p in parents do (
bundles = getChildren p
append bundles p --add the parent into the children
bundles.wirecolor = random black white
)
By the way, I would say that generally the idea of nesting functions should not be encouraged. Even if it is syntactically correct, it has no real benefit. It just makes it harder to understand what the code does.
i kinda like nested functions. actually i feel opposite… it helps me sometime to have code organized.
here is a sample:
fn doSometing text type:1 =
(
fn something1 text:text = (format "1: %
" text)
fn something2 text:text = (format "2: %
" text)
fn something3 text:text = (format "3: %
" text)
case type of
(
1: something1()
2: something2()
3: something3()
)
ok
)
doSometing "something" type:1
doSometing "something" type:2
doSometing "something" type:3
you have two problems there.
outer local variable references (tmpArr and getChildren itself when called recursively)
you have to call getChildren inside main function
to solve the first problem you have to pass these variables to the local function.
there is the way how I usually do it.
fn getAllChildren obj =
(
local tmpArr = #()
local getChildren
fn getChildren node arr:tmpArr fun:getChildren =
(
for c in node.children do
(
append arr c
fun c arr:arr fun:fun
)
arr
)
getChildren obj
)
children = getAllChildren $
BTW… LO showed a nice trick how to get all children:
join #() $
it includes the node itself but it’s no problem to remove
Wow, now that’s a bit of magic! Any idea why this happens?
It reminds me of this: http://www.youtube.com/watch?v=kXEgk1Hdze0
one day i’ve read the MXS help for join function more carefully and understood how it works. JOIN function works for EVERY type of collections!
here is some cool samples:
join #() selection
join #() selectionsets[1]
join #() $.mat -- where mat is MultiMaterial
join #() meditmaterials
Thanks guys for the help and explanation of things.
Makes it so much easier to not nest functions.