Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

I have following MaxScript: -- V-Ray Scene Validation Tool for ALL3D -- Version: 0.5 --…

  1. User

    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 ) )

    -- 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.

  2. Assistant

    Certainly! Below is your adjusted MaxScript. This script now opens a dialog box with individual buttons for each function when the user clicks on the main icon in the toolbar. Each button corresponds to a specific validation step, and they're enabled sequentially upon successful completion of the previous step.

    Here are the key changes made:

    1. Dialog Creation: When the script is run, it opens a dialog (rollout) containing individual buttons for each validation step instead of running all functions at once.

    2. Sequential Button Activation: All buttons are disabled by default except the first one. Each button becomes enabled only after the previous function runs successfully.

    3. Function Isolation: Each function now operates independently and doesn't call subsequent functions. This allows for better control over the sequential enabling of buttons.

    4. Success Checks: Functions return true upon successful completion or false if they encounter any issues. This return value is used to decide whether to enable the next button.

    Here's the adjusted script:

    macroScript ALL3DVRaySceneValidator
        category:"ALL3D"
        buttonText:"ALL3D Scene Validator"
        toolTip:"V-Ray Scene Validation Tool for ALL3D"
        icon:#("ALL3DVRaySceneValidator", 1)
    (
        -- Function definitions
        fn checkGeometries =
        (
            if geometry.count == 0 then
            (
                messageBox ("Current scene does not contain any geometries.") title:"ALL3D Scene Validator" icon:#warning
                return false
            )
            else
            (
                format "Geometries found in the scene.\n"
                return true
            )
        )
    
        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"
                )
            )
            return true
        )
    
        fn productPlacement =
        (
            local success = false
            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()
                    success = true
                    destroyDialog askUser
                )
            )
            createDialog askUser
            return success
        )
    
        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
                return false
            ) else (
                messageBox "All materials are supported by V-Ray Renderer." title:"Supported Materials" icon:#information
                return true
            )
        )
    
        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 updated to include file name prefix.\n"
                ) else (
                    format "Material name already includes file name prefix.\n"
                )
            )
            return true
        )
    
        fn checkAssets =
        (
            format "Checking assets...\n"
            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
                )
            )
    
            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()
                    format "Asset paths updated to ..\\VrayTextures\\.\n"
                ) else (
                    format "Asset paths not changed.\n"
                )
            )
    
            format "V-Ray scene validation complete.\n"
            return true
        )
    
        -- Now, define the rollout with buttons
        rollout sceneValidatorRollout "ALL3D Scene Validator" width:400
        (
            button btn_step1 "Step 1: Check Geometries" enabled:true width:380 align:#center
            button btn_step2 "Step 2: Append Max File Name To Objects Name" enabled:false width:380 align:#center
            button btn_step3 "Step 3: Product Placement" enabled:false width:380 align:#center
            button btn_step4 "Step 4: Check Materials" enabled:false width:380 align:#center
            button btn_step5 "Step 5: Append Max File Name To Materials Name" enabled:false width:380 align:#center
            button btn_step6 "Step 6: Check Assets" enabled:false width:380 align:#center
            button btn_exit "Exit" width:380 align:#center
    
            on btn_step1 pressed do
            (
                if checkGeometries() then
                (
                    format "Step 1 completed successfully.\n"
                    btn_step2.enabled = true
                )
                else
                (
                    format "Step 1 failed.\n"
                    btn_step2.enabled = false
                )
            )
    
            on btn_step2 pressed do
            (
                if appendMaxFileNameToObjectsName() then
                (
                    format "Step 2 completed successfully.\n"
                    btn_step3.enabled = true
                )
                else
                (
                    format "Step 2 failed.\n"
                    btn_step3.enabled = false
                )
            )
    
            on btn_step3 pressed do
            (
                if productPlacement() then
                (
                    format "Step 3 completed successfully.\n"
                    btn_step4.enabled = true
                )
                else
                (
                    format "Step 3 failed.\n"
                    btn_step4.enabled = false
                )
            )
    
            on btn_step4 pressed do
            (
                if checkMaterials() then
                (
                    format "Step 4 completed successfully.\n"
                    btn_step5.enabled = true
                )
                else
                (
                    format "Step 4 failed.\n"
                    btn_step5.enabled = false
                )
            )
    
            on btn_step5 pressed do
            (
                if appendMaxFileNameToMaterialsName() then
                (
                    format "Step 5 completed successfully.\n"
                    btn_step6.enabled = true
                )
                else
                (
                    format "Step 5 failed.\n"
                    btn_step6.enabled = false
                )
            )
    
            on btn_step6 pressed do
            (
                if checkAssets() then
                (
                    format "Step 6 completed successfully.\n"
                    messageBox "All steps completed successfully." title:"ALL3D Scene Validator" icon:#information
                )
                else
                (
                    format "Step 6 failed.\n"
                )
            )
    
            on btn_exit pressed do
            (
                destroyDialog sceneValidatorRollout
            )
        )
    
        createDialog sceneValidatorRollout
    )
    

    How the Adjusted Script Works:

    • Step 1 (Check Geometries): Verifies if there are any geometries in the scene. If none are found, it displays a warning and prevents the user from proceeding to the next step.

    • Step 2 (Append Max File Name To Objects Name): Renames objects to include the current Max file name as a prefix. If successful, it enables the next step.

    • Step 3 (Product Placement): Groups objects and adjusts their placement based on user input (Floor, Ceiling, Wall). Upon completion, it enables the next step.

    • Step 4 (Check Materials): Checks for unsupported materials in the scene. If unsupported materials are found, it warns the user and stops the progression.

    • Step 5 (Append Max File Name To Materials Name): Renames materials to include the current Max file name as a prefix. If successful, it enables the final step.

    • Step 6 (Check Assets): Validates asset paths and checks for missing assets. It allows the user to fix incorrect paths. Upon completion, it notifies the user that all steps are completed.

    • Exit Button: Allows the user to close the dialog at any time.

    Notes:

    • Function Independence: Each function is self-contained and does not call any other validation function. This ensures clear control flow and sequential execution.

    • User Interaction: Functions that require user input (like productPlacement) handle their own dialogs within the function.

    • Error Handling: If a function encounters an issue (e.g., unsupported materials, missing assets), it returns false, and the script prevents the user from proceeding to the next step.

    • Dialog Consistency: The dialog is designed to be user-friendly, with buttons aligned centrally and consistent in width.

    Usage Instructions:

    1. Add the script to your 3ds Max environment.

    2. Run the script or click the "ALL3D Scene Validator" button in the toolbar.

    3. The dialog will appear with the first button enabled.

    4. Click each button in sequence, addressing any issues that are reported.

    5. Proceed through each step; the next button will become enabled only after successfully completing the current step.

    6. After completing all steps, a confirmation message will appear.

    7. You can exit the dialog at any time by clicking the "Exit" button.

    Testing the Script:

    • Before using the script on important scenes, test it on a sample scene to ensure it behaves as expected.

    • Verify that each step performs its intended function and that buttons enable/disable appropriately.

    By incorporating these changes, the script now offers a modular and user-controlled validation process, aligning with your requirements.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.