Notifications
Clear all

[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.

20 Replies

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.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

it may sound funny but i can remember any of my functions written for a least 5 last years…

 lo1

you can replace getChildren with:


fn getChildren node = join #() node

2 Replies
(@jsnb)
Joined: 11 months ago

Posts: 0

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…

(@denist)
Joined: 11 months ago

Posts: 0

we already discussed it on this forum. try to search

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
)


 lo1

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
 lo1

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
 )
 
Page 1 / 2