Maxscript problems

Maxscript problems

Anonymous
Not applicable
2,185 Views
19 Replies
Message 1 of 20

Maxscript problems

Anonymous
Not applicable
I'd like to start off by saying that my native language is not English and I apologize for any spelling/grammar mistakes.

I've recently started getting into MAXscript and whilst making some practice scripts I've encountered problems where my current MAXscript knowledge fails (due to lack thereof) to solve these.

So if any of you were so kind to reply with an answer and maybe give some constructive critisism I'd greatly apreciate it. So far I have 2 questions I'd like to ask involving the code I've written so far.

Question 1:

This is what I've got so far. It's a variation of the script from the first CGacedemy "MAXscript fundamentals" series.

I'd like to expand on this and parent the houses to the vertexes at wich they were created but I have no idea how I'd have to do so.

macroscript CreateACity
category:"myTESTscripts"
(
function makeSingleHouse inputhouseSize housePosition = (

rndValue1 = inputhouseSize - (inputhouseSize / 6.0)
rndValue2 = inputhouseSize + (inputhouseSize / 6.0)
houseSize = random rndValue1 rndValue2

houseBase = box length: houseSize width: houseSize height: (houseSize / 5.0 * 4.0)
houseRoof = pyramid width: houseSize depth: houseSize height: (houseSize / 5.0 * 3.0)

houseBase.mapcoords = true
houseRoof.mapcoords = true


houseBase.wirecolor = color 110 41 0
houseRoof.wirecolor = color 69 62 57


houseBase.position = housePosition
houseRoof.position = housePosition +

newSeed = random 0 9999
roofNoise = NoiseModifier seed: newSeed scale: 24.0 strength:

addModifier houseRoof roofNoise



)


baseArray = selection as array

for i = 1 to baseArray.count do ( --Repeat this step for every object in selection

vertexCount = getNumVerts baseArray.mesh


for vertIndex = 1 to vertexCount do ( --Repeat this step for every vertex in object


localVertexPosition = getvert baseArray.mesh vertIndex
newPosition = localVertexPosition*baseArray.transform

makeSingleHouse (random 18 22) newPosition
)


)
)






Question 2:

When I found out I could make floaters and all those visual things, I tried to make an easy calculator script as practice. Evaluating the script doesn't give any errors but when I run the script and press the button "Calculate", nothing happens, not even an error. So if someone can find out what I'm doing wrong please tell me so.

macroscript edittext
category: "myTESTscripts"
tooltip: "edittext test"
(
rollout EnterValues "Value Enter Rollout"
(

edittext Value1 "01"
edittext Value2 "02"
listbox wichCalculation "What to do?" items:#("Add", "Subtract", "Multiply", "Divide")

button calculate "Calculate"


on calculate pressed do
(

Value01 = (Value1.text as float)
Value02 = (Value2.text as float)
if wichCalculation.selection ==1 then result = ( Value01 + Value02)
else if wichCalculation.selection == 2 then result = ( Value01 - Value02)
else if wichCalculation.selection == 3 then result = ( Value01 * Value02)
else if wichCalculation.selection == 4 then result = ( Value01 / Value02)


)
)


addRollout EnterValues (newRolloutFloater "Calculator" 320 300)

print result



)



Thanks for reading through all this and I appreciate all support.
0 Likes
2,186 Views
19 Replies
Replies (19)
Message 2 of 20

Steve_Curley
Mentor
Mentor
This is a Maxscript question (not an SDK question which a completely different thing), so I'll move the thread to the MAxscript forum.

I'll have a look at your code, but it may be tomorrow - it's late evening here.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 20

Anonymous
Not applicable
Thanks for moving it. When I scrolled throught the sub-forums I instantly thought it was supposed to go in the SDK, but I hadn't seen the MAXscript section, should've looked a bit better.
0 Likes
Message 4 of 20

Anonymous
Not applicable
don't have time read the first one, but regarding the second one:

'result' is a variable which is local to the 'on calculate' event handler.
If does not exist outside of that handler.
You should move the 'print result' line inside that event handler.
Even if it was a global variable there would be no sense in printing right after the rollout is created, because the calculate button has not been pressed yet.
0 Likes
Message 5 of 20

Steve_Curley
Mentor
Mentor
1) You can't "parent" an object to a vertex (on another object). I assume that what you want is to be able to move the initial object and have the "houses" move with the vertices rather than the entire object? Not possible, AFAIK, without a script (attached to each house) which continuously moves the house to the current location of its associated vertex - possibly something to consider later, but may be a little advanced at this point.
You could Link the roof to the house and the house to the original object quite easily, but not directly to a vertex, so moving the original will cause the house (with its roof) to follow, but if you resized the original object the the houses would retain their current position relative to that object (or more accurately, the objects' pivot point).

(
function makeSingleHouse inputhouseSize housePosition =
(
rndValue1 = inputhouseSize - (inputhouseSize / 6.0)
rndValue2 = inputhouseSize + (inputhouseSize / 6.0)
houseSize = random rndValue1 rndValue2

houseBase = box length: houseSize width: houseSize height: (houseSize / 5.0 * 4.0)
houseRoof = pyramid width: houseSize depth: houseSize height: (houseSize / 5.0 * 3.0)

houseBase.mapcoords = true
houseRoof.mapcoords = true

houseBase.wirecolor = color 110 41 0
houseRoof.wirecolor = color 69 62 57

houseBase.position = housePosition
houseRoof.position = housePosition +

newSeed = random 0 9999
roofNoise = NoiseModifier seed: newSeed scale: 24.0 strength:

addModifier houseRoof roofNoise
houseRoof.parent = houseBase -- <<< new line, parent the roof to the house

houseBase -- <<< new line, return houseBase as the result of the function
)

baseArray = selection as array

for i = 1 to baseArray.count do --Repeat this step for every object in selection
(
vertexCount = getNumVerts baseArray.mesh
for vertIndex = 1 to vertexCount do --Repeat this step for every vertex in object
(
localVertexPosition = getvert baseArray.mesh vertIndex
newPosition = localVertexPosition*baseArray.transform
newHouse = makeSingleHouse (random 18 22) newPosition -- <<< changed line, save the result
newHouse.parent = baseArray -- <<< new line, parent the house to the original object
)
)
)


2) "Result" will be an auto-created local variable within the scope of the "on calculate pressed" and will not be visible outside of that function. Read up on the Scope of Variables for an explanation.
Move the print to after the calculation, but before the closing parenthesis of the function. Alternatively you would have to set up a global variable to hold the value - best to avoid globals if you can avoid it. They are easy, in fact too easy, to use (and misuse).

Note. You may find the "case" statement a little clearer than having all those "else if" statements.

result = case wichCalculation.selection of
(
1: Value01 + Value02
2: Value01 - Value02
3: Value01 * Value02
4: Value01 / Value02
)
print result

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 6 of 20

Anonymous
Not applicable
Thanks a lot for replying. It's realy frustrating if your script doesn't work how you want it to, the first time you run it.
0 Likes
Message 7 of 20

Anonymous
Not applicable
that's the case though 100% of the time. 🙂
It'll never run right the first time.
0 Likes
Message 8 of 20

Steve_Curley
Mentor
Mentor
And in many cases the 2nd 3rd and 4th times as well - until you get to grips with Maxscript's rather obscure error messages (if/when you get any, that is).

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 9 of 20

Anonymous
Not applicable
Instead of creating a new thread I'll just expand on this one.

I've further expanded on the first script and tried to make a error message when the selection isn't a geometry class node.
macroscript CreateACity2
category:"myTESTscripts"
(
function makeSingleHouse inputhouseSize housePosition houseBaseParent = (

rndValue1 = inputhouseSize - (inputhouseSize / 6.0) --Defines the random barriers for the house size
rndValue2 = inputhouseSize + (inputhouseSize / 6.0)
houseSize = random rndValue1 rndValue2

houseBase = box length: houseSize width: houseSize height: (houseSize / 5.0 * 4.0)
houseRoof = pyramid width: houseSize depth: houseSize height: (houseSize / 5.0 * 3.0)

houseBase.mapcoords = true
houseRoof.mapcoords = true



houseBase.wirecolor = color 110 41 0
houseRoof.wirecolor = color 69 62 57


houseBase.position = housePosition
houseRoof.position = housePosition +

newSeed = random 0 9999
roofNoise = NoiseModifier seed: newSeed scale: 24.0 strength:

addModifier houseRoof roofNoise

houseRoof.parent = houseBase
houseBase.parent = houseBaseParent
)

rollout ErrorMessage "Error"(

Button Create "Create-A-City"
edittext errormessagebox ">>"

on Create pressed do
(

baseArray = selection as array



for i = 1 to baseArray.count do ( --Repeat this step for every object in selection

vertexCount = getNumVerts baseArray.mesh
if classof i == geometry then (

for vertIndex = 1 to vertexCount do
( --Repeat this step for every vertex in object
localVertexPosition = getvert baseArray.mesh vertIndex
newPosition = localVertexPosition*baseArray.transform

makeSingleHouse (random 18 22) newPosition baseArray

)


else errormessagebox.text = "One or more items of you selection isn't a geometry class" ; createDialog ErrorMessage

)

)





)
)



But when I evaluate the listener gives me an error and I can't work out the problem.


This is the error message:
-- Error occurred in anonymous codeblock; filename: C:\Users\Menno\AppData\Local\Autodesk\3dsMax\2011 - 32bit\enu\UI\usermacros\TestScript2.mcr; position: 1785; line: 61
-- Syntax error: at else, expected <factor>
-- In line: else e


(The line it's talking about is the last "else" line you see)

If anyone knows what I should do please reply. I don't know MAXscript's syntax well.
0 Likes
Message 10 of 20

Anonymous
Not applicable
you're missing another ) before the else.

other than that, you should be checking if
superclassof i == geometryclass

not the class.
0 Likes
Message 11 of 20

Anonymous
Not applicable
Thanks for the quick reply, the error when evaluating is no more but the roullout doesn't pop up when I activate the macroscript. Also any tips for organising brackets?
0 Likes
Message 12 of 20

Anonymous
Not applicable
You define the rollout but not create any dialog or floater
 rollout ErrorMessage "Error"
(
...
)
createDialog ErrorMessage --&gt;> missed part

Ah, and also a problem on this line:
--// incorrect:
else errormessagebox.text = "..." ; createDialog ErrorMessage

-- above is executed as:
else (errormessagebox.text = "...")
createDialog ErrorMessage

--// correct:
else (errormessagebox.text = "..." ; createDialog ErrorMessage)

but... one quick tip -- not all geometry class objects has vertices, so better (suggestion) filter your objects when collect to array instead of check per object and display error message for each, ie ...
-- variant #1
baseArray = for obj in selection where (canConvertTo obj TriMeshGeometry) collect obj
-- variant #2
baseArray = for obj in selection where ((getPolygonCount obj) > 0) collect obj

well, and finally, when create new dialog, it will be created with the default value for the edittext (ie ">>" in this case), ie you should create the dialog 1st and then change its ui-controller's values, this seams a bit complicated (to display error messages in this manner), right? If really need error feedback to popup, then what about MessageBox:
if (...) else (messageBox "Error message here" title:"Wooops")
0 Likes
Message 13 of 20

Anonymous
Not applicable
Thanks for replying, you're all awsome.
0 Likes
Message 14 of 20

Anonymous
Not applicable
So if I have multiple commands for an (if,then,else,do,..) I should put them all between brackets?
0 Likes
Message 15 of 20

Anonymous
Not applicable
yes, thats true for any context (including for eg 'with undo on', 'in coordsys local', ...)
0 Likes
Message 16 of 20

Steve_Curley
Mentor
Mentor
It's not bad practice to always use parentheses around code in an if..then..else situation even if they are not strictly required (single statements don't need them, multiple ones do).

And btw they aren't brackets - (..) are Parentheses, are Brackets, {..} are Braces. They are different and do different jobs and are not interchangeable. Terminology can sometimes be quite important 😉

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 17 of 20

Anonymous
Not applicable
Also, is there a way to usee an infinite number? Like for instance when I'm making a spinner and I define the range, can I set it that you can chose whatever number you want? Without the boundaries.
0 Likes
Message 18 of 20

Swordslayer
Advisor
Advisor
You can try something like this:

spinner spn range:


Note however that with very large numbers (I'd say more than 6 digits) the values stored will be way off anyway so the real question is if you really need it...
0 Likes
Message 19 of 20

Anonymous
Not applicable
Since I started learning MAXscript, I've often come across a point where I wanted something to constantly update but I didn't know how. If you know how to do this, please reply. Like for example, I've made a script wich shows me what the distance between a tape and it's target is. But what I want to do with that, is make a text spline wich displays the distance and changes when moving the target or the tape.
0 Likes
Message 20 of 20

Anonymous
Not applicable
You could maybe use Scripted Plugins (search for "scripted plug-ins helper" in Max Script help) and use some specific event handlers inside ..
0 Likes