[Closed] get complete hierarchy
This may be useful for people.
The script below grabs an entire hierarchy no matter where you select in the tree of hierarchy.
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!
)
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
)
Thanks to Denis for a contribution to a portion of this script.
getRoot… hey! this is my function:
http://forums.cgsociety.org/showpost.php?p=7231023&postcount=4
Nice!
I had no idea where this script came from. It is a few years old for me. I was asked by another employee if I had a script which collect the complete hierarchy. I dug away in my batch of tools and saw this.
Credit goes to Denis! 1 point.
it may sound funny but i can remember any of my functions written for a least 5 last years…
Sorry to backtrack a bit – but why does this work?
Why does the node become a collection of the parent and its children? I wouldn’t expect this. I would have expected the result to be #(node). Or undefined … cuz node isn’t a list of stuff…
Updated. Thank you Rotem for the suggestion. Much much shorter now.
Are there further ways of optimizing this?
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 = join #() node
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
)
I’m not sure if this is more performant or only slightly shorter, haven’t checked:
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
)
)
parents = makeUniqueArray (for o in selection collect (getRoot o)) --unique array of parents
bundles = #()
for p in parents do join bundles p
bundles.wirecolor = random black white
This is another option which requires testing on various datasets:
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
)
)
bundles = #()
for o in (getCurrentSelection()) where findItem bundles o == 0 do
(
join bundles (getRoot o)
)
bundles.wirecolor = random black white
This is great. I’ll check it out. Appears much shorter and efficient.
there are another two functions about a hierarchy matter:
fn getDescendants node =
(
descendants = join #() node
deleteitem descendants 1
)
fn getAscendants node =
(
ascendants = #()
while node.parent != undefined do insertitem (node = node.parent) ascendants 1
ascendants
)
fn getRoot node = if isvalidnode node do
(
while node.parent != undefined do node = node.parent
node
)
mapped fn collectHierarchy node list:#() = if finditem list node == 0 do
(
join list (getRoot node)
)
(
bundles = #()
collectHierarchy selection list:bundles
)