Hey guys. I'm a beginner when it comes to maxscript and scripting in general. I've been doing some small tool scripts as a learning exercise, but I'm still having trouble understanding the order in which things are initialized and called. For example, this is my current test script:
(
objCollection = $*
objArray = objCollection as array
objList = #()
txtNull = ""
local nameSearchBox
function updateList x=
(
objCollection = $*
objArray = objCollection as array
objList = #()
print(x)
for i = 1 to objArray.count do
(
if findString objArray[i].name x == 1 do
(
appendIfUnique objList objArray[i].name
)
)
print(objList)
nameSearchBox.objectList.items = objList
nameSearchBox.objectSearch.text = x
)
function selUpdate = (
print("Selection Changed")
)
rollout nameSearchBox "Name and Search"
(
edittext objectSearch
listbox objectList
on objectSearch changed txt do updateList txt
on nameSearchBox open do(
callbacks.addScript #selectionSetChanged "selUpdate()" id:#Selection_Update
)
on nameSearchBox close do(
callbacks.removeScripts id:#Selection_Update
)
)
createdialog nameSearchBox 150 200
)
When I run the code I get the error "-- selUpdate: undefined
>> MAXScript Callback script Exception: -- Type error: Call needs function or class, got: undefined <<"
This I believe means that the function the callback is looking to run is not yet defined. However, what I've learned, or at least think I understand, is that if the function is placed before the rollout in the script, it should be defined right? So I thought this was written with the right order, but now I'm not so sure. Or is there something else I'm missing here? Thanks for the help!
Replies
Weird to explain but basically its not in the global scope. So you can make the rollout global and then the function is accessible. (Callbacks are finicky or at least in my opinion. Would like to hear if there is a better way) Also I removed that crazyness $*. You can just use selection as Array where you can do whatever you want.
But also you don't need to declare the rollout global. Rollouts are all global once created.
So the way I had it written the functions were not in global scope? If so, why was that? I thought functions would always be global. Ugh, yeah I'm still having trouble wrapping my brain around this. Damn artist brain... If anyone could explain this to me, or point me to a tutorial that would be great.
Thanks again!
What is the use of using "(" at the start and ")" at the end of a maxscript file? Does it have a purpose? :icon_question:
I would make the code like so:
In regards to scope, heres some good reading from the documentation. Generally if you just google function and variable scope you should find bunch of articles. I can write some examples later if that will help you.
http://docs.autodesk.com/3DSMAX/15/ENU/MAXScript-Help/index.html?url=files/GUID-382E7583-DD2A-4DF5-B568-71502DF95ED9.htm,topicNumber=d30e137332,hash=WS3ED54CBA79FF2E3D1E593B6612B78359E8F-7668
@Pathologist: The () basically makes it into a macroscript that can be called on later or from my shitty understanding of maxscript. Not a fan of the language :P
My bad, I think I encased the code in () after testing. Normally I have a bunch of rollout definitions script in a plugin directory, and my macroscripts consist only of createDialogs calls.
That's what I suspected, just wasn't certain about it, I recall having situations where it did survive. That could have been another script conflicting though, we use a major library with over 400 functions defined on launch and kept in memory like that, I am no fan of it myself the previous co-worker who maintained and made our tools was though, as it allowed for easy control over the functions and you could call them at any time.
Thanks for answering
I don't know if you know about WHILE in FOR, but you can compact your FOR loop below to something like this:
This is one of the things that frustrate me well regarding the fact that you gotta launch the program with you're library unless you fileIn every script at the start to import a library. Though hopefully with the implementation of python this might be better
@Monster: Ah awesome.
code so far:
nodeMasterID = attributes obj_Identifier
(
parameters mainP rollout:mainR
(
nodeID type:#node animatable:false
)
rollout mainR "Node Identifier:"
(
checkbox nodeIDcBx enabled: false
)
)
custAttributes.add masterPH nodeMasterID
The Maxscript help is very conveluted and difficult to navigate, having to jump between pages for info is awful. Maya help is so much more helpful
To answer your question directly, remove the rollout from your Custom Attribute and it won't show in the UI.
However, a better solution is to remove then name dependency by using a Weak Reference instead of storing the node.
I almost never use the node type anymore. You can read more about Weak References here: http://paulneale.com/tutorials/Scripting/weakReferences/weakReferences.htm