Forum Groups
  All forums
    Help & Feedback
      Questions
      Work in progress
      Finished Art
      Non-Max related
    Community
      Offtopic
      News
    Hardware
    Photography



Maxunderground news unavailable

[MAXScript] How to include and then call a function from another script, I only get 'undefined'
show user profile  Nanne
I'm trying to break out my functions into different script files to make it more manageable, but I'm having problems with the include function of MAXScript.

Here's what I'm doing.

--script1
-- Load a Material Library located in the same folder as this script
fn LoadMaterialLibrary =
(
scriptPath = getThisScriptFilename()
scriptPath = trimright scriptPath "MaterialScript.ms"
matlibPath = (scriptPath + "MyMatLib.mat")
global matlib = loadTempMaterialLibrary matlibPath
)

fn MyFunction =
(
"Hello World!"
)
--scrtip1 ends

--script2
include "C:\MaterialScript.ms"
LoadMaterialLibrary()
MyFunction()
--scrtip2 ends

But all I get for LoadMaterialLibrary() is undefined as if the function is never created and can't be called. But the MyFunction works and prints out "Hello World" in the Listener. Why is this happening?

Kristoffer Helander
www.kristofferhelander.se
  :: Works ::   |  :: Resumé ::   |  :: Contact ::   |  :: CG Blog ::   |  :: Links ::     
read 288 times
6/13/2016 1:00:08 PM (last edit: 6/13/2016 1:27:18 PM)
show user profile  Nanne
I've updated the example above with a second function that does get called, and it works.

I've also tried using filein as well as include but it makes no difference.

I'm running Max 2016, could this be a bug in that version?

Kristoffer Helander
www.kristofferhelander.se
  :: Works ::   |  :: Resumé ::   |  :: Contact ::   |  :: CG Blog ::   |  :: Links ::     
read 278 times
6/13/2016 1:29:39 PM (last edit: 6/13/2016 1:29:39 PM)
show user profile  Nanne
I've been experimenting with this a bit, and it seems to be working in Max 2017 but not in 2016 SP3.

I had to edit the scripts a bit, but it still only work in 2017.

--script1
-- Load a Material Library located in the same folder as this script
fn LoadMyMaterialLibrary =
(
scriptPath = getThisScriptFilename()
scriptPath = trimright scriptPath "script1.ms"
matlibPath = (scriptPath + "MatLib.mat")
global matlib = loadTempMaterialLibrary matlibPath
)

fn MyFunction =
(
"Hello World!"
)
--scrtip1 ends

--script2
include @"C:\script1.ms"
LoadMyMaterialLibrary()
MyFunction()
--scrtip2 ends



Kristoffer Helander
www.kristofferhelander.se
  :: Works ::   |  :: Resumé ::   |  :: Contact ::   |  :: CG Blog ::   |  :: Links ::     
read 252 times
6/15/2016 2:28:53 PM (last edit: 6/15/2016 2:33:10 PM)
show user profile  br0t
Works for me once you put the script2 contents into parentheses. Include seems to have issues when inserting scripts into global scope, probably because then that part does not get evaluated, the docs are a bit conflicting there:

Inserts the specified file’s content into the Listener output pane. The inserted text is not evaluated. You can use this method to load a script and then step through it, executing any selected text with SHIFT+ENTER.

So rule of thumb probably is "it must be inside some kind of local scope".

never get low & slow & out of ideas


read 198 times
6/23/2016 2:35:41 PM (last edit: 6/23/2016 2:35:57 PM)
show user profile  br0t
If you are looking to modularize your scripts, you are well better off creating separate modules instead of separate files that basically make up a single script IMHO.

MAXScript has no notion of modules by default, but we can use structs to kind of get that behaviour. I use an easy pattern that basically works like this:


struct matlibtools (
fn load_default_matlib = (
-- Functionality here.
)
)

global matlibtools = matlibtools()


The beauty of this is, you have very little boilerplate and get a singleton from that, meaning you now have a matlibtools library that can not be created anew that you can use from anywhere. Your other tools can now:


fileIn "matlibtools.ms"

-- Execute these on startup maybe.
(
matlibtools.load_default_matlib()
-- Do other default stuff with other libraries that each contain some specific functionality.
)


By the way, you can use the pre html tag to properly indent your code within this forum.

never get low & slow & out of ideas


read 196 times
6/23/2016 2:41:28 PM (last edit: 6/23/2016 2:44:12 PM)
#Maxforums IRC
Open chat window


Support Maxforums.org