PDA

View Full Version : How to select a hierarchy


Guibou
02-24-2009, 06:07 PM
HI everyone, quick question...

How do I select a node and then select all of his children to then place them in an array?

Thanks all and have a good day.

Vsai
02-24-2009, 06:45 PM
I've been using this snip in one form or another for a while, someone else may have something clearer though.


fn addChildrenToArray theChildren currentObjsToExport =
(
for c in theChildren do
(
append currentObjsToExport c
addChildrenToArray c.children currentObjsToExport
)
)

currentObjsToExport = #(o)
addChildrenToArray o.children currentObjsToExport

JHN
02-24-2009, 08:51 PM
You could also make use of the pathName literals in mxs:


fn children obj:selection[1] = (execute ("$'" + (obj.name)+"'/*/.../*"))

select (children())
children() as array
theChildren = children obj:$yourOwnObj as array


-Johan

JHN
02-24-2009, 08:57 PM
And another take on it:


fn getTheChildren obj:selection[1] includeParent:false = (execute ("$'" + (obj.name)+"'/"+(if includeParent then "" else "*/")+".../*"))

select (getTheChildren includeParent:true)
select (getTheChildren())

getTheChildren includeParent:true as array
getTheChildren() as array


-Johan

Gravey
02-25-2009, 09:23 AM
or alternatively: fn getChildren obj includeParent:false selectObjs:false =
(
children = for c in obj.children collect c
for c in children do join children (for c in c.children collect c)
if includeParent do append children obj
if selectObjs do select children
children
)

ZeBoxx2
02-25-2009, 12:04 PM
Gravey - note that your function only returns the children and grandchildren; Vsai and JHN's methods should return the entire hierarchy down from the node specified (so great-grandchildren, great-great-grandchildren, etc. as well).

Guibou - if you need -only- the children: $.children already returns an array with exactly that.

Vsai
02-25-2009, 03:35 PM
... pathname literals...

("$'" + (obj.name)+"'/*/.../*"))



now thats a help page thats making me cringe.

JHN
02-25-2009, 03:40 PM
I have to admit I don't use it a lot either, it's like regex for max :) But there is some useful stuff you can do it, like writing a single line expression to select an entire hierarchy ;)

-Johan

Polimeno
02-25-2009, 04:55 PM
Johan,
your code is awsome and get all the hierarquy down, but deselect the current first object...

ObjectSet Values

select <PathName>

Deselects any current selection first, then selects the node(s) you specified.


so,
using selectMore instead select will keep the main selection :

selectMore <PathName>
Adds the specified node(s) to the current selection.



fn getTheChildren obj:selection[1] includeParent:false = (execute ("$'" + (obj.name)+"'/"+(if includeParent then "" else "*/")+".../*"))

selectMore (getTheChildren includeParent:true)
selectMore (getTheChildren())

getTheChildren includeParent:true as array
getTheChildren() as array


what about the standard DOUBLECLICK operation ??
eg. double click root select entire hierarchy ? is there any way to play around that ?

unfortnatly at this point,
i can only think about using keyboard.controlPressed...

Vsai
02-25-2009, 06:51 PM
If all you're looking to do is select them, not to use them in a script/array you can use the built in 'Select Children' hotkey.. i think it defaults to Control+PageDown, if not, just search Customize UI for it.

Gravey
02-25-2009, 11:54 PM
Gravey - note that your function only returns the children and grandchildren; Vsai and JHN's methods should return the entire hierarchy down from the node specified (so great-grandchildren, great-great-grandchildren, etc. as well).Richard - I'm afraid you are wrong. The function i provided works correctly and in exactly the same way as the getFilesRecursive example function in the maxscript reference under 'External File Methods'. I'm sure you know it :) I use this method for collecting recursively fairly often and it works a treat!

magicm
02-26-2009, 01:12 AM
Richard - I'm afraid you are wrong. The function i provided works correctly and in exactly the same way as the getFilesRecursive example function in the maxscript reference under 'External File Methods'. I'm sure you know it :) I use this method for collecting recursively fairly often and it works a treat!
You're right, the function you provided operates on the 'children' array in-place (instead of having the function call itself repeatedly) thereby growing it automatically on each iteration. Having said this, I do think a recursive function would be slightly faster.

Martijn

JHN
02-26-2009, 10:16 AM
Sounds like it's time someone is going to iterate the functions a few thousand times and see by how many milliseconds one differs from another... :)
But I feel "my" solution should win the creativity price. :arteest:

-Johan

ZeBoxx2
02-26-2009, 12:09 PM
Richard - I'm afraid you are wrong.
Indeed I am - mea culpa; adjusting the array used for a loop, could've sworn that didn't work - time to review scripts :curious:

Gravey
02-26-2009, 01:13 PM
But I feel "my" solution should win the creativity price. :arteest:LOL! (there's a prize?)time to review scriptsdamn...

RobGalanakis
02-26-2009, 01:44 PM
If no one gets to it first I will TRY to remember to do a speed test this weekend and put it on the tech-artists.org wiki.

We use this (it passes by reference), though I may change the function to something else if it is faster, based on this thread! It uses by-reference parameters so I THINK it is more memory efficient than a regular recursive that passes an ever-growing number of arrays.
fn getAllChildren obj &arr:#() =
(
for c in obj.children do
(
append arr c
getAllChildren c arr:arr
)
arr
)
select (getAllChildren $ arr:(selection as array))

Guibou
05-04-2009, 06:09 PM
Hi guys, i'm glad to see that my question stirred up an interesting conversation. It quickly went over this noobs head though.

I'm sorry but i don't understand what this line does exactly and i really don't understand it's structure.

execute ("$'" + (obj.name)+"'/"+(if includeParent then "" else "*/")+".../*")

Could somebody explain it to me please. Thanks everyone for you help and your time. Have a good day

CGTalk Moderation
05-04-2009, 06:09 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.