Populating pop up window button with .mat file content

Populating pop up window button with .mat file content

contactZZUHX
Explorer Explorer
323 Views
3 Replies
Message 1 of 4

Populating pop up window button with .mat file content

contactZZUHX
Explorer
Explorer

I'm trying to create a pop up window with buttons that will be populated with the name of an extracted .mat file that contains different materials. The idea is to use this pop up window as a quick way to apply textures within the viewport without going back to the SME by directly loading my standard .mat library that I use for all my projects. 

 

Anyway, I keep getting some basic syntax error and I can't for the life of me find where the thing is going wrong. The script is not complete yet (as in I'll need to add a few things for it to do exactly what I need), but it's already breaking at the current step. 

 

Below is the code : 

 

try (destroyDialog materialButtonsDialog) catch()

rollout materialButtonsDialog "Material Library" width:350 height:640
(
    local buttons = #()

    -- Function to create buttons dynamically based on material names
    fn createButtons materialNames =
    (
        local posX = 10
        local posY = 30
        local buttonWidth = 320
        local buttonHeight = 25
        local margin = 5
        
        for i = 1 to materialNames.count do
        (
            local btnName = "btn_" + i as string
            local btn = button btnName text:materialNames[i] pos:[posX, posY] width:buttonWidth height:buttonHeight
            posY += buttonHeight + margin  -- Increment position for the next button
            append buttons btn  -- Add button to the array

            -- Add a simple event to demonstrate functionality
            on btn pressed do
            (
                messageBox ("Material selected: " + btn.text)
            )
        )
    )

    -- Function to load materials from a .mat file and return their names
    fn loadMaterialNamesFromMatFile =
    (
        local filename = getOpenFileName caption:"Select Material Library" types:"Material Library (*.mat)|*.mat"
        local materialNames = #()

        if filename != undefined then
        (
            local myLibrary = loadTempMaterialLibrary filename
            if classOf myLibrary == MaterialLibrary then
            (
                for mat in myLibrary do
                (
                    if mat != undefined then
                    (
                        append materialNames mat.name
                    )
                )
            )
            else
            (
                messageBox "Failed to load the selected .mat file as a Material Library."
            )
        )
        else
        (
            messageBox "No file was selected. Please select a valid .mat file."
        )

        return materialNames
    )

    -- Event to load materials and create buttons when the rollout opens
    on materialButtonsDialog open do
    (
        local materialNames = loadMaterialNamesFromMatFile()
        if materialNames.count > 0 then
        (
            createButtons materialNames
        )
        else
        (
            messageBox "No materials found in the selected .mat file."
        )
    )

    button btnClose "Close" pos:[10, 610] width:330 height:25
    on btnClose pressed do
    (
        destroyDialog materialButtonsDialog
    )
)

createDialog materialButtonsDialog 350 640 resizable:false

 

 

If anybody has any idea on how to fix it, or do it differently, that would be greatly appreciated!

Thanks 

Simon

0 Likes
324 Views
3 Replies
Replies (3)
Message 2 of 4

contactZZUHX
Explorer
Explorer

Ok, after some fiddling and further research I managed to make it work. I'll put it here in case people are interested in the concept : 

global myLibrary

-- Prompt the user to select a .mat file
filename = getOpenFileName caption:"Select Material Library" types:"Material Library (*.mat)|*.mat" initialDir:""

-- Check if a file was selected
if filename != undefined then
(
    -- Attempt to load the material library
    myLibrary = loadTempMaterialLibrary filename
    
    -- Check if the library loaded successfully
    if classOf myLibrary == MaterialLibrary then
    (
        -- Generate the rollout definition as a string
        rolloutDef = "rollout dynamicRollout \"Material Buttons\" width:440 height:640\n"
        rolloutDef += "(\n"
        rolloutDef += "    local hiddenObjects = #()\n"
        rolloutDef += "    checkbox chkHideAfterRename \"Hide after rename\" pos:[10,10] width:190 height:20\n"
        rolloutDef += "    button closeBtn \"Close\" pos:[230,10] width:190 height:30\n"
        
        -- Add buttons for each material
        local btnPosY = 50
        for i = 1 to myLibrary.count do
        (
            local mat = myLibrary[i]
            if mat != undefined then
            (
                local btnName = "btn" + i as string
                rolloutDef += "    button " + btnName + " \"" + mat.name + "\" pos:[10," + btnPosY as string + "] width:420 height:25\n"
                btnPosY += 30
            )
        )
        
        -- Add function definitions and event handlers
        rolloutDef += "    fn renameAndHideObjects newName matIndex = (\n"
        rolloutDef += "        if selection.count > 0 then (\n"
        rolloutDef += "            for obj in selection do (\n"
        rolloutDef += "                obj.name = newName\n"
        rolloutDef += "                obj.material = ::myLibrary[matIndex]\n"
        rolloutDef += "                if chkHideAfterRename.state then (\n"
        rolloutDef += "                    append hiddenObjects obj\n"
        rolloutDef += "                    obj.isHidden = true\n"
        rolloutDef += "                )\n"
        rolloutDef += "            )\n"
        rolloutDef += "        )\n"
        rolloutDef += "    )\n"
        
        -- Add button event handlers
        for i = 1 to myLibrary.count do
        (
            local mat = myLibrary[i]
            if mat != undefined then
            (
                local btnName = "btn" + i as string
                rolloutDef += "    on " + btnName + " pressed do renameAndHideObjects \"" + mat.name + "\" " + i as string + "\n"
            )
        )
        
        -- Add close button event handler
        rolloutDef += "    on closeBtn pressed do (\n"
        rolloutDef += "        for obj in hiddenObjects do obj.isHidden = false\n"
        rolloutDef += "        destroyDialog dynamicRollout\n"
        rolloutDef += "    )\n"
        rolloutDef += ")\n"
        
        -- Execute the rollout definition
        execute rolloutDef
        
        -- Create and show the dialog
        createDialog dynamicRollout
    )
    else
    (
        messageBox "Failed to load material library. Please ensure you selected a valid .mat file."
    )
)
else
(
    messageBox "No file selected."
)
0 Likes
Message 3 of 4

denisT.MaxDoctor
Advisor
Advisor

you can use rolloutcreator and subrollout control instead:

 

try(destroydialog rol) catch ()
rollout rol "RO:183453F0" width:191 height:300
(
	
	subrollout sub_rol width:180 height:280 
	
	fn makeMatButtons lib = 
	(
		rollout_name = #LibMaterialsRollout
		rcg = rolloutCreator rollout_name "Lib Materials" width:140 
		rcg.begin()
		
		for k=1 to lib.count do 
		(
			mat = lib[k]
			name = "mat_bt" + (k as string)
			rcg.addControl #button name mat.name paramstr:("align:#center width:120")
			rcg.addHandler name #pressed \
				codeStr:("(format \">> %\\n\"" + name + ".text" + ")")			
		)
		
		rcg.end()
		rcg.def
	)
	
	on rol open do
	(
		lib = meditmaterials
		mrol = makeMatButtons lib
		
		addsubrollout sub_rol mrol autoLayoutOnResize:on
	)
)
createdialog rol

 

 

0 Likes
Message 4 of 4

contactZZUHX
Explorer
Explorer

I'll give it a go. Thanks!

0 Likes