|
|||||
Using Case Expressions with MAXScript
Summary: This document introduces the MAXScript case expression by defining and discussing a script constructed with case expressions.
The tips, tricks, examples and suggestions
outlined in Autodesk Product Support technical documents are suggested
for use at your own risk. Document contents are subject to change without
notice. Autodesk is not responsible or liable for damage or events that
may occur as a result of following suggestions from any Autodesk Product
Support technical document.
© 1998 Autodesk. Autodesk and
the Autodesk logo are registered trademarks of Autodesk Inc. All other
brand names, product names or trademarks belong to their respective holders.
Overview
The ‘Case Expression’ is used to select which expression from a set of expressions will be evaluated. Among other things, case expressions allow the user to choose between blocks of code to execute. Conceptually, the Case Expression is identical with the ‘If Expression’, but can be easier to use when there are many expressions to choose from, since ‘Case’ abbreviates the ‘If’ test.
This document demonstrates the case expression in relation to radio buttons. First the document steps through evaluating the case expression, then analyzes each section of the script.
This document does not cover User Interface Design or the MAXScript language in general. For more information on the case expression, see the MAXScript Reference in the help menu of 3D Studio MAX. Related technical documents are listed in the document description header, above.
2. Press the ‘New Script’ button from the MAXScript rollout.
3. Copy and paste the following text into the new MAXScript window:
Note: The following script will reset your scene. Save your work before proceeding.
resetMaxFile #noPrompt -- USER INTERFACE ITEMS CaseTest = newRolloutFloater "Best Case Scenario" 300 150 rollout rgender_issue "Describe yourself" ( radioButtons btn_gender "Are you a: " labels:#("man",\ "woman", "child", "other") on btn_gender changed state do ( case state of ( 1: (c = cone () ) 2: (s = sphere () ) 3: (b = box () ) 4: (t = spindle radius:20 height:42 cap_height:15) ) btn_gender.enabled = false ) ) addrollout rgender_issue CaseTest4. In the file menu of the new script, Save the script as ‘BestCaseScenario.ms’
5. In the file menu of the new script, select Evaluate All.
6. In the ‘Best Case Scenario’ floating dialog box, choose the button that best describes you. Notice that the radio buttons gray after one is chosen. A primitive is created in the scene. To run the script again, first close the ‘Best Case Scenario’ floating dialog box. From the file menu of the new script, select Evaluate All.
Script Analysis
resetMaxFile #noPrompt
This line resets the current session without further prompting to ensure a new start. This option can be commented out.
Tip: "Commenting out" is a method of including text in code that the program will ignore. This is useful for commenting on lines of the code as you write it, and for disabling parts of a routine without deleting the code. In MAXScript, the comment symbol is two hyphens. [ -- ].MAXScript ignores whatever follows the two dashes as it evaluates that line of code. Comments will only apply to one line of code.
-- USER INTERFACE ITEMS CaseTest = newRolloutFloater "Worst Case Scenario" 250 150This begins the user interface definition section for the floating dialog box. This line of code defines the dialog box within which the rollouts will be included and then sizes it.
rollout gender_issue "Describe yourself" ( radioButtons gender "Are you a: " labels:#("man",\ "woman", "child", "other")This defines the only rollout for the floating dialog box. Four radio buttons are defined with labels of man, woman, child, and other. Notice the single backslash character after the "man" label. This is a line continuation character: MAXScript continues to read the next line as if it was part the first line. For more information on continuation characters, search the MAXScript reference, index tab for "Source Code Layout and Continuation Lines."
On btn_gender changed state do (When any of the radio buttons are picked, the case expression is evaluated.
case state of ( 1: (c = cone () ) 2: (s = sphere () ) 3: (b = box () ) 4: (t = spindle radius:20 height:42 cap_height:15) )Depending on the button pressed, the corresponding case is evaluated. The radio button state is compared with the labeled expressions. For example, ‘woman’ is radio button state 2 , so the expression labeled 2 is evaluated. In this example, in the case that radio button is woman, make a sphere; in the case that radio button is child, make a box.
When there is a long list of expressions to choose from, the ‘Case’ form is more convenient to read and write than the ‘If’ form, although the two forms amount to the same thing. For example, if the radio button is woman, make a sphere; if the radio button is child, then make a box.
Btn_gender.enabled = falseAfter any of the radio buttons are selected, they are all disabled.
Addrollout rgender_issue CaseTestThe rollout is included in the floating dialog box.
Note: For a more involved script using the case expression, refer to Technical Document 5507 (TD805507) Using Radio Buttons with Case Expressions.
Was this article helpful?
(very helpful) 5 43 21 (not helpful) |
Home | Products | Tech Assist | Autodesk | Prof Net | Search | Sitemap | Purchase
©Copyright
1998 Autodesk, Inc. All rights reserved.
Reproduction or copying
of images is prohibited.
Autodesk
Privacy Policy