function inside a function?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 11 November 2012   #1
function inside a function?

How do correctively make a function inside a function?

fn getAllChildren obj =
(
	local tmpArr = #()
	
	fn getChildren obj = (
		for c in node.children do
		(
			append tmpArr c
			getChildren c
		)
	)
	tmpArr
)
children = getAllChildren $
__________________
____________ ___ __ _
John A. Martini
JokerMartini@hotmail.com
www.JokerMartini.com
 
Old 11 November 2012   #2

fn getChildren obj tmpArr =
(
	for c in obj.children do
	(
		append tmpArr c
		getChildren c tmpArr
	)
	return tmpArr
)

fn getAllChildren obj =
(
	local tmpArr = #()
	
	tmpArr = getChildren obj tmpArr
)
children = getAllChildren $
select children
__________________
____________ ___ __ _
John A. Martini
JokerMartini@hotmail.com
www.JokerMartini.com
 
Old 11 November 2012   #3
Is this more or less what you're after?

fn getChildren obj =
(
        local tmpArr = #()
	for c in obj.children do
	(
		append tmpArr c
		join tmpArr (getChildren c)
	)
	tmpArr --omitting "return" is faster!
)
children = getChildren $
 
Old 11 November 2012   #4
Thanks PJ

Thank you for the help. Just incase anyone made need this.
This script below collects hierarchies of objects and then changes the wirecolor for each bunch of objects. Each bundle consists of complete hierarchies .

You don't have to select the parent, it will automatically find the top most parent and then all it's children.


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!
)

clearlistener()

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
)
__________________
____________ ___ __ _
John A. Martini
JokerMartini@hotmail.com
www.JokerMartini.com
 
Old 11 November 2012   #5
By the way, I would say that generally the idea of nesting functions should not be encouraged. Even if it is syntactically correct, it has no real benefit. It just makes it harder to understand what the code does.
 
Old 11 November 2012   #6
you have two problems there.
# outer local variable references (tmpArr and getChildren itself when called recursively)
# you have to call getChildren inside main function

to solve the first problem you have to pass these variables to the local function.
there is the way how I usually do it.

fn getAllChildren obj =
(
	local tmpArr = #()
	local getChildren
	
	fn getChildren node arr:tmpArr fun:getChildren = 
	(
		for c in node.children do
		(
			append arr c
			fun c arr:arr fun:fun
		)
		arr
	)
	getChildren obj
)
children = getAllChildren $


BTW... LO showed a nice trick how to get all children:

join #() $

it includes the node itself but it's no problem to remove
 
Old 11 November 2012   #7
Thanks guys for the help and explanation of things.
Makes it so much easier to not nest functions.
__________________
____________ ___ __ _
John A. Martini
JokerMartini@hotmail.com
www.JokerMartini.com
 
Old 11 November 2012   #8
Originally Posted by denisT: BTW... LO showed a nice trick how to get all children:

join #() $

it includes the node itself but it's no problem to remove

Wow, now that's a bit of magic! Any idea why this happens?
It reminds me of this: http://www.youtube.com/watch?v=kXEgk1Hdze0

Last edited by Pjanssen : 11 November 2012 at 07:10 PM.
 
Old 11 November 2012   #9
Originally Posted by Pjanssen: By the way, I would say that generally the idea of nesting functions should not be encouraged. Even if it is syntactically correct, it has no real benefit. It just makes it harder to understand what the code does.

i kinda like nested functions. actually i feel opposite... it helps me sometime to have code organized.
here is a sample:

fn doSometing text type:1 = 
(
	fn something1 text:text = (format "1: %\n" text)
	fn something2 text:text = (format "2: %\n" text)
	fn something3 text:text = (format "3: %\n" text)
	
	case type of
	(
		1: something1()
		2: something2()
		3: something3()
	)
	ok
)
doSometing "something" type:1
doSometing "something" type:2
doSometing "something" type:3

 
Old 11 November 2012   #10
Originally Posted by Pjanssen: Wow, now that's a bit of magic! Any idea why this happens?
It reminds me of this: http://www.youtube.com/watch?v=kXEgk1Hdze0

one day i've read the MXS help for join function more carefully and understood how it works. JOIN function works for EVERY type of collections!
here is some cool samples:

join #() selection
join #() selectionsets[1]
join #() $.mat -- where mat is MultiMaterial
join #() meditmaterials
 
Old 11 November 2012   #11
Originally Posted by Pjanssen: It reminds me of this: http://www.youtube.com/watch?v=kXEgk1Hdze0

i think i could make the similar move about the maxscript
 
Old 11 November 2012   #12
Thread automatically closed

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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 12:12 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.