CGTalk > Software > Autodesk 3ds max > 3dsMax SDK and MaxScript
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 11-20-2012, 06:33 PM   #1
JokerMartini
Expert
 
JokerMartini's Avatar
portfolio
John Martini
CG Supervisor, 3D, Maxscript, VFX
Ingenuity Engine
Los Angeles, USA
 
Join Date: Mar 2009
Posts: 1,330
Send a message via AIM to JokerMartini
function inside a function?

How do correctively make a function inside a function?

Code:
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-20-2012, 06:46 PM   #2
JokerMartini
Expert
 
JokerMartini's Avatar
portfolio
John Martini
CG Supervisor, 3D, Maxscript, VFX
Ingenuity Engine
Los Angeles, USA
 
Join Date: Mar 2009
Posts: 1,330
Send a message via AIM to JokerMartini
Code:
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-20-2012, 06:55 PM   #3
Pjanssen
Veteran
portfolio
Pier Janssen
Göteborg, Sweden
 
Join Date: Apr 2003
Posts: 2,400
Is this more or less what you're after?
Code:
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-20-2012, 06:59 PM   #4
JokerMartini
Expert
 
JokerMartini's Avatar
portfolio
John Martini
CG Supervisor, 3D, Maxscript, VFX
Ingenuity Engine
Los Angeles, USA
 
Join Date: Mar 2009
Posts: 1,330
Send a message via AIM to JokerMartini
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.

Code:
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-20-2012, 07:02 PM   #5
Pjanssen
Veteran
portfolio
Pier Janssen
Göteborg, Sweden
 
Join Date: Apr 2003
Posts: 2,400
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-20-2012, 07:02 PM   #6
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,871
Send a message via ICQ to denisT
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.
Code:
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:
Code:
join #() $

it includes the node itself but it's no problem to remove
 
Old 11-20-2012, 07:06 PM   #7
JokerMartini
Expert
 
JokerMartini's Avatar
portfolio
John Martini
CG Supervisor, 3D, Maxscript, VFX
Ingenuity Engine
Los Angeles, USA
 
Join Date: Mar 2009
Posts: 1,330
Send a message via AIM to JokerMartini
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-20-2012, 07:07 PM   #8
Pjanssen
Veteran
portfolio
Pier Janssen
Göteborg, Sweden
 
Join Date: Apr 2003
Posts: 2,400
Quote:
Originally Posted by denisT
BTW... LO showed a nice trick how to get all children:
Code:
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-20-2012 at 07:10 PM.
 
Old 11-20-2012, 07:10 PM   #9
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,871
Send a message via ICQ to denisT
Quote:
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:
Code:
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-20-2012, 07:19 PM   #10
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,871
Send a message via ICQ to denisT
Quote:
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:
Code:
join #() selection join #() selectionsets[1] join #() $.mat -- where mat is MultiMaterial join #() meditmaterials
 
Old 11-20-2012, 07:26 PM   #11
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,871
Send a message via ICQ to denisT
Quote:
Originally Posted by Pjanssen

i think i could make the similar move about the maxscript
 
Old 11-20-2012, 07:26 PM   #12
CGTalk Moderation
Expert
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,478
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 08:10 AM.


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