Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk 3ds® Max® / MaxScript / Defining functions correctly
  RSS 2.0 ATOM  

Defining functions correctly
Rate this thread
 
63420
 
Permlink of this thread  
avatar
  • bobcat
  • Posted: 09 January 2012 05:07 PM
  • Total Posts: 20
  • Joined: 23 August 2006 06:23 PM

This scripts throws a error at the name every second time its run.
I think its because of the way or layout of the script and function.
should the function be inside the actual script?

----------START-----------------------------------------
macroScript speedsortByVertex
category
"Troys Tools Speed Sort"
buttontext"Sort by Vertex"
tooltip"Sort by Vertex"
(
----------
FUNCTIONS-----------------------------------------
fn speedsortByVertexFunction theNodes vertexCount 
(
 
tempObjectArray #()
 
for i=1 to theNodes.count do
 (
 
theNode theNodes[i]
 
if theNode.numverts == vertexCount then append tempObjectArray theNode 
 
)
 
tempObjectArray
)
----------
SCRIPT-----------------------------------------
 
on isEnabled return selection.count == 1
 on execute 
do
 (
 
theVertexCount = $.numverts
 max select all
 ss 
getCurrentSelection()
 
select (speedsortByVertexFunction ss theVertexCount)
 )
)
----------
END------------------------------------------


Replies: 0
avatar
  • Location: West Midlands, England, UK
  • Total Posts: 14445
  • Joined: 06 August 2007 11:06 PM
  • Permlink of this post

Yes, it must be inside the outermost parentheses which enclose the script itself.

A few pointers:-

Always enclose any maxscript in your posts within CODE tags - the forum can display code incorrectly making it impossible to copy/paste from the post. I added the code tags to your post for this reason.

Naming. SelectByVertexCount would be more accurate - it doesn’t “sort” anything at present.

Valid object type. Script fails if the selected object is not an Editable Mesh.

Return values from functions. The result of a function is the result of the last statement executed within the function. The “return” statement is redundant in this instance (just tempObjectArray on its own is sufficient). It is also SLOW - read the “The Return Expression” section of the Maxscript Help.

Last, but by no means least - always state your Max version. Best to put that (and your brief system specs) in your sig.



Max 4.2 through 2013.
XP-64 (SP2)
NVidia 9800GTX-512 (Driver 266.58).
Core 2 Quad Q6600 2.4GHz, 8Gb Ram, DX9.0c.

Replies: 1
/img/forum/dark/default_avatar.png

Thanks Steve, I have poped the function inside and all works great, see above.

Thanks for the other tips, all work-in-progress.

My only questions now is, is this function visible to other scirpts, or should a create a seperate script to contain all the functions required for my master sorting script.
Should I declare the fn as Global?

Something like

----------START FUNCTIONS SCRIPT------------------------
macroScript speedsortFunctions
(
fn speedsortByVertexFunction theNodes vertexCount 
(
 
tempObjectArray #()
 
for i=1 to theNodes.count do
 (
 
theNode theNodes[i]
 
if theNode.numverts == vertexCount then append tempObjectArray theNode 
 
)
 
tempObjectArray
)--end fn
)
----------
END------------------------------------------

----------
START MAIN SCRIPT-----------------------------------------
macroScript speedsortByVertex
category
"Troys Tools Speed Sort"
buttontext"Sort by Vertex"
tooltip"Sort by Vertex"
(
 
on isEnabled return selection.count == 1
 on execute 
do
 (
 
theVertexCount = $.numverts
 max select all
 ss 
getCurrentSelection()
 
select (speedsortByVertexFunction ss theVertexCount)
 )
)
----------
END------------------------------------------
Author: bobcat

Replied: 10 January 2012 12:52 PM  
avatar
  • Location: West Midlands, England, UK
  • Total Posts: 14445
  • Joined: 06 August 2007 11:06 PM
  • Permlink of this post

That’s not what I meant.

MacroScript headers
(
fn definition(s)
rest of script
)

In that case, no - it will not be (directly) accessible to other scripts. Globals would normally be avoided if at all possible to prevent conflicts between different scripts which happen to have identical function names. You can “separate” the functions by saving them in a separate .ms and Including them, if you’re trying to keep the main script less cluttered.

The “Scope of Variables” topic in the Help makes the above point, and others which you may find useful.



Max 4.2 through 2013.
XP-64 (SP2)
NVidia 9800GTX-512 (Driver 266.58).
Core 2 Quad Q6600 2.4GHz, 8Gb Ram, DX9.0c.

Replies: 0