Notifications
Clear all

[Closed] Strange macroscript issue

After some hard work and help from people on this forum, I had a script that was doing exactly what I wanted it to do. I made it into a macroscript, and it still worked without a hitch.

At least, it did then and there.

Subsequently I have tried running the macroscript and it wouldn’t work. If I run the script manually first, and THEN run it as a macroscript, it works fine. Because of that, my initial thought was that it was something to do with variable scope. But after seeing where the error occurs, I think it might be something else instead.

Reduced to its simplest form, the problem occurs in the following script:

macroScript TestMacro category:"Versatile Tools"
 (
 	struct objSet (theObject)
 	format "objSet: %
" objSet
 
 	fn testFunction =
 	(
 		for s in selection do
 		(
 			obj = execute (s.name + " = objSet theObject:$" + s.name)
 			format "obj: %
" obj
 		)
 	)
 	testFunction()
 )

I want to dynamically name a structure using the name of the selected object, and put that object into the structure. The restult I get is as follows:

objSet: #Struct:objSet(
   theObject:<data>)
 -- Error occurred during fileIn in StringStream:"Circle01 = objSet theObject:$Circle01"
 -- Error occurred in s loop; filename: C:\Users\Malkalypse\AppData\Local\Autodesk\3dsmax\9 - 32bit\enu\UI\usermacros\Versatile Tools-TestMacro.mcr; position: 37
 --  Frame:
 --   Obj: undefined
 --   s: $Circle01
 --   called in testFunction(); filename: C:\Users\Malkalypse\AppData\Local\Autodesk\3dsmax\9 - 32bit\enu\UI\usermacros\Versatile Tools-TestMacro.mcr; position: 311
 --  Frame:
 --   called in anonymous codeblock
 --  Frame:
 --   testFunction: testFunction()
 --   objSet: StructDef:objSet
 >> MAXScript MacroScript Error Exception: -- Type error: Call needs function or class, got: undefined <<

Now, the formatted output shows that the structure definition SHOULD be in place by that point, but it doesn’t work. If the line inside the s loop is stated as

obj = (Circle01 = objSet theObject:$Circle01)

then there is no problem. For a moment I thought there was some issue with using the execute() call inside the macroscript, but know I have seen that done many times. Just to be sure, I tried the following:

macroScript TestMacro category:"Versatile Tools"
 (
 	local obj
 	
 	fn testFunction =
 	(
 		for s in selection do
 		(
 			obj = execute ("$" + $.name)
 			format "obj: %
" obj
 		)
 	)
 	testFunction()
 )

and that one works just fine. So I’m completely baffled. Why am I having this problem, and how can I fix it??

2 Replies

I think you’re getting the error because the objSet struct does not exist in the global scope. Declaring it as a global right before the definition should do the trick.

Martijn

Argh… I can’t believe I didn’t think of that. Thanks man!