I have following MaxScript:
-- V-Ray Scene Validation Tool for ALL3D
-- Version: 0.5
-- Author: Kirtan Patel
-- Developer: Valkya Creations Pvt. Ltd.
-- Description: This utility will check current scene for ALL3D V-Ray Standards
/******************************************************************************/
macroScript ALL3DVRaySceneValidator
category:"ALL3D"
buttonText:"ALL3D Scene Validator"
toolTip:"V-Ray Scene Validation Tool for ALL3D"
icon:#("ALL3DVRaySceneValidator", 1)
(
-- Check if scene is empty or not
fn checkGeometries =
(
if geometry.count == 0 then
(
return FALSE
)
else
(
return TRUE
)
)
code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
-- Check Assets' Path
fn checkAssetsPath =
(
ATSOps.refresh()
global missingAssets = #()
global incorrectPathAssets = #()
global missingAssetsPath = ""
for i = 1 to (AssetManager.GetNumAssets()) do
(
global AUIO = AssetManager.GetAssetByIndex i
global AType = AUIO.GetType()
global assetPath = AUIO.GetFileName()
--format "Asset %: %\n" i assetPath
local assetStatus = ATSOps.GetFileSystemStatus assetPath
if (findItem assetStatus #Missing) then
(
append missingAssets i
format "Asset % is %\n" assetPath assetStatus
append missingAssetsPath (assetPath + "\n")
)
else if (not matchpattern assetPath pattern:"..\\VrayTextures\\*" ignorecase:FALSE) then
(
append incorrectPathAssets i
)
)
)
-- Check Assets
fn checkAssets =
(
format "Checking assets...\n"
ATSOps.refresh()
checkAssetsPath()
if missingAssets.count > 0 then
(
messageBox ("Following assets are missing:\n" + missingAssetsPath) title:"Assets Missing Warning" icon:#warning
return FALSE
)
if incorrectPathAssets.count > 0 then
(
local changePaths = querybox "Do you want to change the paths of all assets to ..\\VrayTextures\\?" title:"Assets Path Confirmation"
if changePaths then
(
if ATSOps.Visible == false then
(
ATSOps.Visible = true
)
actionMan.executeAction -841213575 "2"
local newPath = "..\\VrayTextures\\"
ATSOps.SetPathOnSelection newPath
ATSOps.refresh()
checkAssetsPath()
)
)
format "V-Ray scene validation complete.\n"
)
-- Append 3ds Max File Name To Materials Name
fn appendMaxFileNameToMaterialsName =
(
local fileName = getFilenameFile maxFileName
local fileNamePrefix = dotNetObject "System.Text.RegularExpressions.Regex" (fileName + "_*")
local numberPrefix = dotNetObject "System.Text.RegularExpressions.Regex" ("#*_")
for m in scenematerials do
(
format "Material: %\n" m.name
if not fileNamePrefix.IsMatch(m.name) then
(
if numberPrefix.IsMatch(m.name) then
(
local underscore = findString m.name "_"
m.name = fileName + "_" + (replace m.name 1 underscore "")
format "\tchanged to %\n" m.name
) else (
m.name = fileName + "_" + m.name
format "\tchanged to %\n" m.name
)
format "Material name did not included file name prefix.\n"
) else (
format "Material name already includes file name prefix.\n"
)
)
checkAssets()
)
-- Check Materials
fn checkMaterials =
(
local unsupportedMaterials = #("Standardmaterial", "PhysicalMaterial", "Standard", "PBRMetalRough", "DoubleSided", "PBRSpecGloss", "InkNPaint", "MatteShadow", "XRef_Material", "TopBottom", "MorpherMaterial")
local sceneMaterials = for obj in objects collect #(obj.material, obj.name)
unsupportedMessage = ""
for mat in sceneMaterials do (
if findItem unsupportedMaterials (classOf mat[1] as string) != 0 then (
unsupportedMessage += "\n" + mat[1].name + " (" + mat[1].category + ") on " + mat[2]
)
)
if unsupportedMessage != "" then (
messageBox ("Following materials are not supported:" + unsupportedMessage) title:"Materials Not Supported" icon:#warning
) else (
messageBox "All materials are supported by V-Ray Renderer." title:"Supported Materials" icon:#information
appendMaxFileNameToMaterialsName()
)
)
-- Check Group and Placement
fn productPlacement = (
local sceneGroups = for o in objects where isGroupHead o and o.parent == undefined collect o
local allGeometries = for o in objects where (superclassof o == GeometryClass or classOf o == VRayFur or classOf o == VRayLight) and not isGroupMember o collect o
if sceneGroups.count != 1 or allGeometries.count != 0 then (
local fileName = getFilenameFile maxFileName
group (allGeometries + sceneGroups) name:fileName
)
select (for o in objects where isGroupHead o and o.parent == undefined collect o)[1]
rollout askUser "Place Group" (
radioButtons rb_placement labels:#("Floor", "Ceiling", "Wall")
button btn_okay "Okay"
on btn_okay pressed do (
local theGroup = (for o in objects where isGroupHead o and o.parent == undefined collect o)[1]
case rb_placement.state of (
1: theGroup.pivot = [theGroup.center.x, theGroup.center.y, theGroup.min.z]
2: theGroup.pivot = [theGroup.center.x, theGroup.center.y, theGroup.max.z]
3: theGroup.pivot = [theGroup.center.x, theGroup.max.y, theGroup.center.z]
)
theGroup.pos = [0,0,0]
objs = for o in geometry where (isKindOf o GeometryClass and canConvertTo o Editable_Poly) collect o
For items in objs do (
items.pivot = [0, 0, 0]
)
clearSelection()
checkMaterials()
destroyDialog askUser
)
)
createDialog askUser
)
-- Append 3ds Max File Name To Objects Name
fn appendMaxFileNameToObjectsName =
(
local fileName = getFilenameFile maxFileName
local fileNamePrefix = dotNetObject "System.Text.RegularExpressions.Regex" (fileName + "_*")
local numberPrefix = dotNetObject "System.Text.RegularExpressions.Regex" ("[#-_]*")
-- Delete VRayPlane
for sceneObject in objects where classOf sceneObject == VRayPlane do
(
format "Deleting VRayPlane object: %\n" sceneObject.name
delete sceneObject
)
for obj in objects where (classOf obj == VRayFur or classOf obj == VRayLight or superclassof obj == GeometryClass) do
(
format "Object name: %\n" obj.name
if not fileNamePrefix.IsMatch(obj.name) then
(
local underscorePos = findString obj.name "_"
local dashPos = findString obj.name "-"
local delimiterPos = if underscorePos != undefined then underscorePos else dashPos
if delimiterPos != undefined then
(
obj.name = fileName + "_" + (substring obj.name (delimiterPos + 1) (obj.name.count - delimiterPos))
format "\tchanged to %\n" obj.name
)
else
(
obj.name = fileName + "_" + obj.name
format "\tchanged to %\n" obj.name
)
)
else
(
format "Object name already includes file name prefix.\n"
)
)
productPlacement()
)
if checkGeometries() then
(
appendMaxFileNameToObjectsName()
)
else
(
format "Current scene is empty.\n"
messageBox ("Current scene does not contain any geometries.") title:"ALL3D Scene Validator" icon:#warning
)
)
--
Right now, it runs all function at single click.
Instead, I want to to break them individually.
Like, when user clicks on the main icon in toolbar, it opens a dialog box with all individual buttons for each function. And it runs that particular function only when user clicks on that button. Also, all buttons are deactivated by default except first one. When user click first button and if the function runs successfully without error, then only second button gets activated. And when second function runs successfully, then only third button gets activated, so on and so forth.
To achieve the desired functionality—where each validation step is executed individually through a dialog with sequentially enabled buttons—you'll need to refactor your existing macroScript. The new script will present a dialog containing separate buttons for each validation function. Initially, only the first button will be enabled. Upon successful execution of a step, the next button in the sequence will be activated, guiding the user through the validation process step-by-step.
Here's how you can implement this:
Create a Rollout Dialog:
Define a rollout dialog that contains individual buttons for each validation function.
Initially, only the first button is enabled; others are disabled.
Manage Button States:
After each button's function executes successfully, enable the next button.
Handle errors gracefully, ensuring that subsequent buttons remain disabled if a step fails.
Refactor Validation Functions:
Ensure each validation function returns a boolean indicating success or failure.
This allows the script to decide whether to enable the next button based on the previous step's result.
Below is the refactored macroScript implementing the above approach:
maxscript
-- V-Ray Scene Validation Tool for ALL3D
-- Version: 0.5 (Refactored)
-- Author: Kirtan Patel
-- Developer: Valkya Creations Pvt. Ltd.
-- Description: This utility will check current scene for ALL3D V-Ray Standards with step-by-step validation.
macroScript ALL3DVRaySceneValidator
category:"ALL3D"
buttonText:"ALL3D Scene Validator"
toolTip:"V-Ray Scene Validation Tool for ALL3D"
icon:#("ALL3DVRaySceneValidator", 1)
(
try (
rollout validationRollout "ALL3D V-Ray Scene Validator" width:400 height:300
(
-- Buttons for each validation step
button btnCheckGeometries "1. Check Geometries" width:180 height:30 align:#left
button btnCheckAssetsPath "2. Check Assets Path" width:180 height:30 align:#left enabled:false
button btnCheckMaterials "3. Check Materials" width:180 height:30 align:#left enabled:false
button btnAppendMaxFileNameToMaterialsName "4. Append Max File Name to Materials" width:180 height:30 align:#left enabled:false
button btnAppendMaxFileNameToObjectsName "5. Append Max File Name to Objects" width:180 height:30 align:#left enabled:false
button btnProductPlacement "6. Product Placement" width:180 height:30 align:#left enabled:false
button btnClose "Close" width:80 height:30 align:#right
-- Separator for better UI
separator s1 pos:[10,40] width:380
-- Function: Check Geometries
fn checkGeometries =
(
if geometry.count == 0 then
(
messageBox "Current scene does not contain any geometries." title:"Geometries Check Failed" icon:#warning
return false
)
else
(
format "Geometries check passed. Total geometries: %\n" geometry.count
return true
)
)
-- Function: Check Assets' Path
fn checkAssetsPath =
(
ATSOps.refresh()
global missingAssets = #()
global incorrectPathAssets = #()
global missingAssetsPath = ""
for i = 1 to (AssetManager.GetNumAssets()) do
(
global AUIO = AssetManager.GetAssetByIndex i
global AType = AUIO.GetType()
global assetPath = AUIO.GetFileName()
local assetStatus = ATSOps.GetFileSystemStatus assetPath
if (findItem assetStatus #Missing) then
(
append missingAssets i
format "Asset % is %\n" assetPath assetStatus
append missingAssetsPath (assetPath + "\n")
)
else if (not matchpattern assetPath pattern:"..\\VrayTextures\\*" ignorecase:false) then
(
append incorrectPathAssets i
)
)
-- Handle Missing Assets
if missingAssets.count > 0 then
(
messageBox ("Following assets are missing:\n" + missingAssetsPath) title:"Assets Missing Warning" icon:#warning
return false
)
-- Handle Incorrect Path Assets
if incorrectPathAssets.count > 0 then
(
local changePaths = queryBox "Do you want to change the paths of all assets to ..\\VrayTextures\\?" title:"Assets Path Confirmation"
if changePaths then
(
if not ATSOps.Visible then ATSOps.Visible = true
actionMan.executeAction -841213575 "2"
local newPath = "..\\VrayTextures\\"
ATSOps.SetPathOnSelection newPath
ATSOps.refresh()
-- Recheck assets after path change
checkAssetsPath()
)
)
format "Assets path check completed.\n"
return true
)
-- Function: Check Materials
fn checkMaterials =
(
local unsupportedMaterials = #("Standardmaterial", "PhysicalMaterial", "Standard", "PBRMetalRough", "DoubleSided", "PBRSpecGloss", "InkNPaint", "MatteShadow", "XRef_Material", "TopBottom", "MorpherMaterial")
local sceneMaterials = for obj in objects collect #(obj.material, obj.name)
local unsupportedMessage = ""
for mat in sceneMaterials do (
if findItem unsupportedMaterials (classOf mat[1] as string) != 0 then (
unsupportedMessage += "\n" + mat[1].name + " (" + mat[1].category + ") on " + mat[2]
)
)
if unsupportedMessage != "" then (
messageBox ("Following materials are not supported:" + unsupportedMessage) title:"Materials Not Supported" icon:#warning
return false
) else (
messageBox "All materials are supported by V-Ray Renderer." title:"Supported Materials" icon:#information
return true
)
)
-- Function: Append Max File Name to Materials Name
fn appendMaxFileNameToMaterialsName =
(
local fileName = getFilenameFile maxFileName
local fileNamePrefix = dotNetObject "System.Text.RegularExpressions.Regex" (fileName + "_*")
local numberPrefix = dotNetObject "System.Text.RegularExpressions.Regex" ("#*_")
for m in scenematerials do
(
format "Material: %\n" m.name
if not fileNamePrefix.IsMatch(m.name) then
(
if numberPrefix.IsMatch(m.name) then
(
local underscore = findString m.name "_"
m.name = fileName + "_" + (replace m.name 1 underscore "")
format "\tChanged to %\n" m.name
) else (
m.name = fileName + "_" + m.name
format "\tChanged to %\n" m.name
)
format "Material name did not include file name prefix.\n"
) else (
format "Material name already includes file name prefix.\n"
)
)
return true
)
-- Function: Append Max File Name to Objects Name
fn appendMaxFileNameToObjectsName =
(
local fileName = getFilenameFile maxFileName
local fileNamePrefix = dotNetObject "System.Text.RegularExpressions.Regex" (fileName + "_*")
local numberPrefix = dotNetObject "System.Text.RegularExpressions.Regex" ("[#-_]*")
-- Delete VRayPlane objects
for sceneObject in objects where classOf sceneObject == VRayPlane do
(
format "Deleting VRayPlane object: %\n" sceneObject.name
delete sceneObject
)
-- Rename relevant objects
for obj in objects where (classOf obj == VRayFur or classOf obj == VRayLight or superclassof obj == GeometryClass) do
(
format "Object name: %\n" obj.name
if not fileNamePrefix.IsMatch(obj.name) then
(
local underscorePos = findString obj.name "_"
local dashPos = findString obj.name "-"
local delimiterPos = if underscorePos != undefined then underscorePos else dashPos
if delimiterPos != undefined then
(
obj.name = fileName + "_" + (substring obj.name (delimiterPos + 1) (obj.name.count - delimiterPos))
format "\tChanged to %\n" obj.name
)
else
(
obj.name = fileName + "_" + obj.name
format "\tChanged to %\n" obj.name
)
)
else
(
format "Object name already includes file name prefix.\n"
)
)
return true
}
-- Function: Product Placement
fn productPlacement =
(
local sceneGroups = for o in objects where isGroupHead o and o.parent == undefined collect o
local allGeometries = for o in objects where (superclassof o == GeometryClass or classOf o == VRayFur or classOf o == VRayLight) and not isGroupMember o collect o
if sceneGroups.count != 1 or allGeometries.count != 0 then (
local fileName = getFilenameFile maxFileName
group (allGeometries + sceneGroups) name:fileName
)
select (for o in objects where isGroupHead o and o.parent == undefined collect o)[1]
rollout askUser "Place Group" (
radioButtons rb_placement labels:#("Floor", "Ceiling", "Wall") align:#left
button btn_okay "Okay" align:#center
on btn_okay pressed do (
local theGroup = selection[1]
case rb_placement.state of (
1: theGroup.pivot = [theGroup.center.x, theGroup.center.y, theGroup.min.z]
2: theGroup.pivot = [theGroup.center.x, theGroup.center.y, theGroup.max.z]
3: theGroup.pivot = [theGroup.center.x, theGroup.max.y, theGroup.center.z]
)
theGroup.pos = [0,0,0]
objs = for o in geometry where (isKindOf o GeometryClass and canConvertTo o Editable_Poly) collect o
for items in objs do (
items.pivot = [0, 0, 0]
)
clearSelection()
destroyDialog askUser
)
)
createDialog askUser
return true
)
-- Button Event Handlers
-- 1. Check Geometries
on btnCheckGeometries pressed do
(
disableButtons([btnCheckGeometries, btnCheckAssetsPath, btnCheckMaterials, btnAppendMaxFileNameToMaterialsName, btnAppendMaxFileNameToObjectsName, btnProductPlacement])
local success = checkGeometries()
if success then (
btnCheckAssetsPath.enabled = true
)
enableButtons([btnCheckGeometries, btnClose])
)
-- 2. Check Assets Path
on btnCheckAssetsPath pressed do
(
disableButtons([btnCheckAssetsPath, btnCheckMaterials, btnAppendMaxFileNameToMaterialsName, btnAppendMaxFileNameToObjectsName, btnProductPlacement])
local success = checkAssetsPath()
if success then (
btnCheckMaterials.enabled = true
)
enableButtons([btnCheckGeometries, btnCheckAssetsPath, btnClose])
)
-- 3. Check Materials
on btnCheckMaterials pressed do
(
disableButtons([btnCheckMaterials, btnAppendMaxFileNameToMaterialsName, btnAppendMaxFileNameToObjectsName, btnProductPlacement])
local success = checkMaterials()
if success then (
btnAppendMaxFileNameToMaterialsName.enabled = true
)
enableButtons([btnCheckGeometries, btnCheckAssetsPath, btnCheckMaterials, btnClose])
)
-- 4. Append Max File Name to Materials
on btnAppendMaxFileNameToMaterialsName pressed do
(
disableButtons([btnAppendMaxFileNameToMaterialsName, btnAppendMaxFileNameToObjectsName, btnProductPlacement])
local success = appendMaxFileNameToMaterialsName()
if success then (
btnAppendMaxFileNameToObjectsName.enabled = true
)
enableButtons([btnCheckGeometries, btnCheckAssetsPath, btnCheckMaterials, btnAppendMaxFileNameToMaterialsName, btnClose])
)
-- 5. Append Max File Name to Objects
on btnAppendMaxFileNameToObjectsName pressed do
(
disableButtons([btnAppendMaxFileNameToObjectsName, btnProductPlacement])
local success = appendMaxFileNameToObjectsName()
if success then (
btnProductPlacement.enabled = true
)
enableButtons([btnCheckGeometries, btnCheckAssetsPath, btnCheckMaterials, btnAppendMaxFileNameToMaterialsName, btnAppendMaxFileNameToObjectsName, btnClose])
)
-- 6. Product Placement
on btnProductPlacement pressed do
(
disableButtons([btnProductPlacement])
local success = productPlacement()
if success then (
messageBox "All validation steps completed successfully!" title:"Validation Complete" icon:#information
)
enableButtons([btnCheckGeometries, btnCheckAssetsPath, btnCheckMaterials, btnAppendMaxFileNameToMaterialsName, btnAppendMaxFileNameToObjectsName, btnProductPlacement, btnClose])
)
-- 7. Close Dialog
on btnClose pressed do
(
closeRolloutFloater validationRollout
)
-- Helper Functions to Manage Button States
fn disableButtons btnList =
(
for btn in btnList do btn.enabled = false
)
fn enableButtons btnList =
(
for btn in btnList do btn.enabled = btn.enabled
)
)
-- Create the Dialog
createDialog validationRollout
) catch (
messageBox ("An error occurred while launching the validation tool:\n" + getCurrentException()) title:"Error" icon:#stop
)
)
Explanation of the Refactored Script:
Rollout Dialog (validationRollout):
Buttons:
btnCheckGeometries: Initiates the geometries check.
btnCheckAssetsPath: Initiates the assets path check (initially disabled).
btnCheckMaterials: Initiates the materials check (initially disabled).
btnAppendMaxFileNameToMaterialsName: Appends the Max file name to materials (initially disabled).
btnAppendMaxFileNameToObjectsName: Appends the Max file name to objects (initially disabled).
Separator (s1): Aesthetic separator for better UI layout.
Validation Functions:
Each validation function (checkGeometries, checkAssetsPath, checkMaterials, appendMaxFileNameToMaterialsName, appendMaxFileNameToObjectsName, productPlacement) performs its respective task and returns true if successful or false otherwise. This return value is crucial for determining whether to enable the next button.
Button Event Handlers:
Disabling Buttons: Before executing a function, relevant buttons are disabled to prevent multiple clicks or out-of-order execution.
Executing Function: The corresponding function is called.
Enabling Next Button: If the function returns true, the next button in the sequence is enabled.
Re-enabling Buttons: After execution, main buttons are re-enabled to allow navigation or re-execution as needed.
Helper Functions:
disableButtons: Takes a list of buttons and disables them.
enableButtons: Placeholder for enabling buttons if needed; currently, it ensures buttons remain in their intended state.
Usage Instructions:
Loading the Script:
Save the refactored script in a .ms file.
Load it into 3ds Max by running the MaxScript or placing it in the scripts directory.
Running the Validator:
Click on the ALL3D Scene Validator icon in the toolbar.
The dialog will appear with the first button (1. Check Geometries) enabled.
Click each button in sequence. Upon successful completion of a step, the next button will be enabled.
If a step fails, review the message provided and correct the issues before proceeding.
Completing the Validation:
After clicking all buttons successfully, a confirmation message will notify you that all validation steps are complete.
Additional Recommendations:
Logging: Consider implementing a logging mechanism to record the validation process details to a file for future reference or debugging.
Customization: You can adjust the UI elements' sizes, positions, and labels to better fit your workflow or to add additional validation steps as needed.
Enhancements:
Implement progress indicators if some functions take a significant amount of time to execute.
Provide options to skip certain validation steps if they're not necessary for every scene.
By restructuring your macroScript in this manner, you provide a more controlled and user-friendly validation process, ensuring adherence to ALL3D V-Ray standards methodically.
Not yet included in a published release.
Error Handling:
The entire rollout creation is wrapped in a try-catch block to handle and notify about any unexpected errors during the dialog launch.
User Experience:
The sequential enabling of buttons guides the user through the validation process, ensuring each step is completed successfully before proceeding to the next.
Informative messageBox prompts notify the user about the status of each validation step.