I want you to write a MaxScript function that get value from following a string (textbox) like:
Height
30 Inches
Width
77 Inches
Depth
37 Inches
Here, get value of Depth as length, Width as width and Height as height of the box, and create box at 0, 0, 0 axis in top view.
Name it "Overall_Dimension"
Then, make it "display as box".
I want it UI based. Create a dialog box, where there is a textarea field where user paste like following value:
Height
30 Inches
Width
77 Inches
Depth
37 Inches
And there is a button "Create Dimension Box" below it. When user click that button, then only the function should run.
Also, if the values are not in the above mentioned format, then show error.
Also, when createBoxFromDimensions runs successfully, destroy DimensionDialog rollout.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a comprehensive MaxScript script that fulfills your requirements. This script creates a user interface (UI) dialog with a textarea for pasting dimension data and a button to generate a box based on the provided dimensions. It includes error handling to ensure the input adheres to the specified format and cleans up the dialog upon successful box creation.
MaxScript Code:
maxscript
/*
Function: createBoxFromDimensions
Description:
- Opens a dialog where users can input dimensions in a specific format.
- Parses the input to extract Height, Width, and Depth values.
- Creates a box named "Overall_Dimension" at the origin with the specified dimensions.
- Sets the box to display as a bounding box.
- Handles input validation and displays error messages for incorrect formats.
*/
fn createBoxFromDimensions =
(
rollout DimensionDialog "Dimension Input"
(
-- Textarea for user to paste dimension data
textarea txtInput "Paste dimensions here:" width:300 height:100
-- Button to trigger box creation
button btnCreate "Create Dimension Box" width:200
/*
Event Handler: btnCreate pressed
Actions:
1. Retrieves and processes the input text.
2. Validates the format and content.
3. Creates the box if validation passes.
4. Displays error messages if validation fails.
*/
on btnCreate pressed do
(
local inputText = txtInput.text
-- Split the input text into lines, handling different line endings
local lines = filterString inputText "\r\n"
-- Remove empty lines and trim whitespace
lines = for l in lines where (trim l != "") collect (trim l)
-- Validate that there are exactly 6 lines
if lines.count != 6 then
(
messageBox "Error: Input must contain exactly 6 non-empty lines in the following order:\n\nHeight\n<value> Inches\nWidth\n<value> Inches\nDepth\n<value> Inches"
)
else
(
try
(
-- Verify that the labels are correctly placed
if (lines[1] != "Height") then throw()
if (lines[3] != "Width") then throw()
if (lines[5] != "Depth") then throw()
/*
Function: extractNumber
Description:
- Extracts the numerical value from a string formatted as "<number> Inches".
Parameters:
s (string): The input string to parse.
Returns:
float or undefined: The numerical value if successful, undefined otherwise.
*/
fn extractNumber s =
(
local tokens = filterString s " "
if tokens.count >=1 then
(
try
(
return (tokens[1] as float)
)
catch
(
return undefined
)
)
else
return undefined
)
-- Extract numerical values for Height, Width, and Depth
height = extractNumber lines[2]
width = extractNumber lines[4]
depth = extractNumber lines[6]
-- Ensure all extracted values are valid numbers
if (height == undefined or width == undefined or depth == undefined) then
throw()
-- Create the box with the specified dimensions at the origin
theBox = box length:depth width:width height:height pos:[0,0,0]
theBox.name = "Overall_Dimension"
-- Set the box to display as a bounding box
theBox.displayMode = #bounding_box
-- Optional: Freeze the transformation if needed
-- theBox.transform = (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
-- Close the dialog upon successful creation
destroyDialog DimensionDialog
)
catch
(
messageBox "Error: Input format is incorrect.\n\nPlease ensure the input follows the exact format:\n\nHeight\n<value> Inches\nWidth\n<value> Inches\nDepth\n<value> Inches"
)
}
)
)
-- Display the dialog to the user
createDialog DimensionDialog
)
-- Execute the function to display the dialog
createBoxFromDimensions()
How It Works:
User Interface:
Textarea (txtInput): Provides a space for users to paste their dimension data in the specified format.
Button (btnCreate): When clicked, it initiates the process of parsing the input and creating the box.
Input Validation:
Line Count Check: Ensures that exactly 6 non-empty lines are provided.
Label Verification: Confirms that the labels "Height", "Width", and "Depth" are in the correct order and positions.
Value Extraction: Extracts numerical values from lines containing " Inches". It gracefully handles any parsing errors and undefined values.
Box Creation:
Dimensions Mapping: Maps Depth to length, Width to width, and Height to height of the box.
Positioning: Places the box at the origin (0,0,0).
Naming: Assigns the name "Overall_Dimension" to the created box.
Display Mode: Sets the box to display as a bounding box (#bounding_box).
Error Handling:
If the input does not meet the required format or contains invalid values, an error message is displayed, guiding the user to correct the input.
Cleanup:
Upon successful creation of the box, the dialog is destroyed/closed automatically.
Usage Instructions:
Run the Script:
Copy and paste the above MaxScript code into the MaxScript Editor in 3ds Max.
Press Ctrl + E or click the Evaluate All button to execute the script.
Provide Input:
In the dialog that appears, paste your dimension data in the following format:
code
Height
30 Inches
Width
77 Inches
Depth
37 Inches
Create the Box:
Click the "Create Dimension Box" button.
If the input is correctly formatted, a box named "Overall_Dimension" will be created at the origin, and the dialog will close.
If there's an error in the input format, an error message will prompt you to correct it.
Example:
After running the script and providing the following input:
code
Height
30 Inches
Width
77 Inches
Depth
37 Inches
A box named "Overall_Dimension" with dimensions Length: 37, Width: 77, Height: 30 will be created at the origin (0,0,0) and set to display as a bounding box.
Feel free to customize the script further to fit additional requirements or to enhance its functionality!