PDA

View Full Version : maxscript and how to call a function


rollin
08-19-2011, 02:55 AM
heyho,

ok the problem is not that easy but I can hardly find information about that. At least nothing that helps me.


I have several functions in my .ms file
They are not inside of anything.


Now I want to create some macroscripts that call these functions instead of putting the function itself inside the macroscript.

Now how do I tell max to parse all the functions first? Because after starting max I can not call these functions because they are not defined. But if I evaluate the script they are defined (of course) and so I can call the functions from the macros

Piflik
08-19-2011, 03:59 AM
Have a look in the MaxScript User Reference, keywords 'Including Scripts within Scripts'.

Basically it is the 'include' command. But Bobo describes it way better than I ever could ;)

rollin
08-19-2011, 04:17 AM
well, not exactly what I was looking for but it does work. I have split the script into UI stuff and core functions and use

include "fnctions.ms"

at the start of my macroscripts

thx!

Bryan Cavett
08-19-2011, 06:51 AM
You could put the functions.ms in the scripts startup folder so they get initialized every time max is run

perna
08-19-2011, 08:01 AM
Do as Bryan says.
Once you've evaluated an ms file, those functions are now global and can be used any time.
Repeatedly evaluating the same functions is generally not a good approach.

Mark Dygert
08-19-2011, 08:52 AM
You could put the functions.ms in the scripts startup folder so they get initialized every time max is run+2

Nash 3d
10-15-2012, 01:27 PM
I just found a better answer. I too wanted to call a function from the UI. You need to pre-initialize any function you want to call from the UI.

This allows the script to access a function that is not yet defined.

Similar to the following article.

http://docs.autodesk.com/3DSMAX/15/ENU/MAXScript-Help/index.html?url=files/GUID-FAE5BB27-C3B2-46B4-B0BA-FF7CA80969A8.htm,topicNumber=d30e91632

monster
10-15-2012, 08:18 PM
You could put the functions.ms in the scripts startup folder so they get initialized every time max is run

Actually, against logic, the Macroscript and UserMacro folders are evaluated before the Scripts/Startup and UserStartup folders. (The name suggests the opposite.) This is because Startup scripts, unlike macros, have access to the viewports and can create and manipulate objects.

If you want to use functions in MacroScripts they need to go in the stdplugin/stdscripts folder.

http://docs.autodesk.com/3DSMAX/15/ENU/3ds-Max-Help/files/GUID-C372E124-2B7E-41BB-94B0-5FF945D51095.htm

SimonT
03-11-2013, 06:12 AM
I use this thread since i have a problem which is simliar to this problem:

I've a function and want to include some code but get an error when i execute master.ms :( Any ideas?

MASTER.MS
fn run = (
include "slave.ms";
)

run();

SLAVE.MS
print "test";

ERROR
-- Error occurred in anonymous codeblock; filename: D:\Users\Simon\Desktop\master.ms; position: 37; line: 3
-- Syntax error: at ), expected <factor>
-- In line: )

monster
03-11-2013, 04:25 PM
It's the semicolon. In MaxScript you don't need them. I stopped using thme because stuff like this would happen occasionally.

This works for me.

fn run = (
include "slave.ms"
)

SimonT
03-12-2013, 12:48 AM
WTF?! Why can't Max just say "Delete the ; you moron?" ARRRGH!

Thank you, monster! :) Never had thought that this is the problem.

SimonT
03-12-2013, 01:22 AM
Ok, now i have another question, i want define a local variable in the master.ms, include the slave.ms which just prints out the local variable. This works fine already. BUT i want overhand "include" a variable as filename which isn't possible when i believe the manual. And if i use filein, i'm not able to use the variable.

MASTER.MS
fn run filename = (
local mytext = "I'm yours, master!"
filein filename;
)

run "slave.ms";

SLAVE.MS
print mytext;


OUTPUT
undefined

rollin
03-12-2013, 04:10 AM
It's the semicolon. In MaxScript you don't need them. I stopped using thme because stuff like this would happen occasionally.

This works for me.

fn run = (
include "slave.ms"
)


for sure? that would be really really "WTF?!"

monster
03-12-2013, 09:13 AM
Bobo (the other one) explains why this doesn't work here: http://forums.cgsociety.org/showpost.php?p=4137629&postcount=2

You'll need to use a global variable with fileIn.

Typically I'll make a global struct and house all my variable in there. That way you only have one global. I'll even put the functions in it.


struct myGlobals
(
myVar1 = undefined,
myVar2 = undefined,
myVar3 = undefined,
mytext = undefined,

fn run filename =
(
mytext = "I'm yours, master!"
filein filename
)
)

global myInstance = myGlobals()
myInstance.run "slave.ms"



print myInstance.mytext

SimonT
03-13-2013, 01:34 AM
Thanks Monster! i think i'll do it like you did :)