Get the top group/node in a selection

Get the top group/node in a selection

sinderltd
Explorer Explorer
948 Views
5 Replies
Message 1 of 6

Get the top group/node in a selection

sinderltd
Explorer
Explorer

Hey, I need to get the top node/group in a selection. I have groups within groups within groups etc, but when I get selection it gets all objects in the group but in no particular order. I need the one at top of the hierarchy. I am using isGroupHead to collect the group heads in a selection but it doesn't provide any order or hierarchy. Being able to sort the array based on the order or hierarchy would be cool.

0 Likes
949 Views
5 Replies
Replies (5)
Message 2 of 6

denisT.MaxDoctor
Advisor
Advisor
fn isAncestor node child = 
(
	while child != node and child != undefined do (child = child.parent)
	child == node
)
fn sortByRelation n1 n2 = if isAncestor n1 n2 then -1 else if isAncestor n2 n1 then 1 else 0
	
nodes = selection as array 
qsort nodes sortByRelation

 

roots come first

Message 3 of 6

MartinBeh
Advisor
Advisor

Or you could use isGroupHead to collect the group heads and keep the one that has an empty .parent property? 

top = for o in selection where ((isGroupHead o) and (o.parent == undefined)) collect o
Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
Message 4 of 6

sinderltd
Explorer
Explorer

Awesome thanks for the help, this works perfectly.

0 Likes
Message 5 of 6

sinderltd
Explorer
Explorer

Amazing, thanks for your help, sorting the order is going to be super helpful!

0 Likes
Message 6 of 6

denisT.MaxDoctor
Advisor
Advisor

@MartinBeh wrote:

Or you could use isGroupHead to collect the group heads and keep the one that has an empty .parent property? 

 

top = for o in selection where ((isGroupHead o) and (o.parent == undefined)) collect o

 



What if every grouphead has a parent?