Notifications
Clear all

[Closed] Select all children

Hello everyone,

I’ve got a stumper. I’m trying to write a function that will select all of the childen of the currently select object or objects. I’ve figured out how to select the children of a single object but when I wrap the same code in a for loop with some print statements to debug it, it doesn’t select anything the first time through the loop, then selects everything in the scene the second time through, and each time through after that. IE… I’ve got 2 simple chains of boxes, if I select 1 item in one chain it selects itself and it’s children correctly. If I select one item in each chain, everything in the scene is selected. No idea what I’ve done wrong. Here’s the script, any suggestions appreciated.

fn selectAllChildren =
(
a = #()
m = selection
if m.count > 1 then
(
print “yuummy”
b = selection as array
for i in b do
(
print “hello”
select i
print i as string
j = selection
print “in j”
in j select $…*
print $
)
)
else
(
print m
in m select $…*
print “Only one”
)
)
selectAllChildren()

8 Replies

hi anticz,

quickly worked this piece of code, which might help u… (i’m learning max script…) . select any object and run this… it selects all the current selected objects and its immediate children.

donno if this can be done somehow else… wud lov to learn…

 
 
fn selectMyChildren =
(
parnt=("$"+$.name) -- selected obj
objs=#()
objs=(for o in objects where o.parent != undefined collect o) -- returns all objects who have parents
for i in 1 to objs.count do
( 
nm=("$"+objs[i].name);
strng=(nm+".parent");
parnt1=("$"+(execute (strng)).name);
	 if (parnt1==parnt) do -- check if the selected obj is parent of the obj
	 (
		 strng1=("selectMore "+nm); -- selects the objs
		 execute strng1
	 ) 
)

) 
selectMyChildren();
 

this works with one obj at a time only. hope it helped…
rgds

You could achieve the same effect by holding CTRL+PageDown to select all the children of the object selected object. also works with multiple selection.

or through script:


fn selectAllChildren =
(
for obj in selection do
(
if obj.children != undefined do
(
selectmore obj.children
)
)
)
selectAllChildren()

this will loop through you selection:
for obj in selection do…

it then checks if it has children nodes:
obj.children != undefined

if it found some children, add it to the current selection:
selectmore obj.children

hope this helps!

hi galagast,

that was damn easy ! thanx for letting us know about the “.children” attribute. i didnt know it exists… any such useful attributes you know, we can use ?

thats a superb cute smalllll script !

cheers,

no biggie, most of the attributes you’ll probably want to take note of are under the “General Node Properties” in the help.

I found that it helps alot (when i was just starting to script) to read these parts from time to time:

  • General Node Properties
  • Node Common Properties, Operators, and Methods

Plus it also helps using the Favorites section of the Help. I’ve got stuff there linking to most of the String stuff, Particle Flow, ActiveX, Arrays, Callbacks… etc… but it would probably depend on what you usually work with.

Hi all,

Galagast, great work and a clear and concise script! I would do it differently though because:

  • I don’t really like changing the selection while I’m working on it.
  • I would want the children to be returned as an array so I could use the function in another script and may want to do some more processing.
  • Also, I might consider that I want the children, and not parent since I already know the parent (I’ve provided an option to include the parent)

that said:


global mychld=#()
fn selectchilden obj=
(
for o in obj do
(
-- append mychld o	-- to include the parent, uncomment this and comment out the other append below.
local cdn=o.children
if cdn.count>0 then
(
for c in cdn do
(
	append mychld c -- remove this to include the parent (see above)
	selectchilden c
)
)
)
return mychld
)
 
-- using the function:
sel=selection as array
x=selectchilden sel
select x

I hope this is useful!

Funny how a simple task can be done in so many ways, here’s my take on this:

fn collectMyChildren obj includeParent:true =
(
	-- start collecting direct children of <obj>
	myChildren = #(obj)
	
	-- add children and their children etc recursively
	i = 0
	while i < myChildren.count do
	(
		i += 1
		join myChildren myChildren[i].children
	)

	-- remove initial object if <includeParent> == false
	if not includeParent then deleteitem myChildren 1
	
	-- return array containing children
	myChildren
)

select (collectMyChildren $ includeParent:false)

Hey I guess it really pays to know WHAT to search for in the Maxscript help. I was looking for this info in help but had no idea to look under “Node Common Properties”…

Thanks!
Billibong

Thanks everyone!! I see now I was going about this completely wrong.

J_Man, that’s exactly what I was trying to do. I’d much rather have the selection as an array rather than changing the selection. I added the array as a condition so I can add the objects to any array instead of just [size=2]mychld=#(). Thanks again.
[/size]