[Closed] how get this?
The thread you started on Monday gave the answer:
http://forums.cgsociety.org/showthread.php?t=354408
I slightly modified j-man’s code below, and it works fine in max:
fn findhead obj =
(
if (isGroupHead obj)
then (return obj)
else (if (isGroupMember obj) then (findhead (obj.parent)) else (return undefined))
)
This function will return the node itself, so if you want the name of the group (or “None” if there is no group), use the code below:
(
local ReturnedNode = (findhead obj)
local TheText = (if ReturnedNode!=undefined then ReturnedNode.name else "None")
)
I finally began using the above code in a script of my own, and I realized that it only returns the first group head found as it goes upward through the group structure. If you are testing a node that is a member of a group within a multitiered or “nested” group, then it will not return the top level group head.
I modified the test expression to fix this. Before returning any group head node, it tests whether that group head is also a group member, which would make it a lower group nested within another group. When this is the case, it keeps searching up the structure until it finds the top level group head.
fn findhead obj =
(
if ((isGroupHead obj)AND(not (isGroupMember obj)))
then (return obj)
else (if (isGroupMember obj) then (findhead (obj.parent)) else (return undefined))
)
I was pleased with that little one liner, but never go any thanks for it. I’m glad you’ve managerd to transform it into something a little bit more useful. I feel dirty though LOL.
J.
Thank you very much for that post earlier this year…seriously.
You were answering czweb’s question, but you showed the first clear example of a recursive function I’d ever seen. I didn’t even know a function could call itself until I read your post. Even after having your example, I still have to stare at it a bit to get my “barely-coding” head around it. Thanks again.