Hi, I need some help on a mel script, I hope someone can take a bit of time to look at my code and tell me why it doesn't work and how to fix it. I suspect it could even be a Maya bug...
What I am trying to do it create buttons dynamically on the fly using a '+' button. The '+' button will create a new button when I click on it. Each created button has a right click submenu that has a command to delete the button itself.
The problem I ran into is when I delete the button from it's own right click menu, Maya will crash.
This is my code:
<div>/*<br>source dynamic_buttons_test; dynamic_buttons_test;<br>*/<br><br>global proc dynamic_buttons_test()<br>{<br> global string $buttons_rows[];<br> global string $buttons[];<br> global string $add_button;<br> <br> if (`window -exists dynamic_buttons_test_UI`) deleteUI -wnd dynamic_buttons_test_UI;<br> if (`windowPref -exists dynamic_buttons_test_UI`) windowPref -r dynamic_buttons_test_UI;<br> <br> string $window = `window -title "Dynamic Button Test"<br> -widthHeight 300 200<br> "dynamic_buttons_test_UI"`;<br><br> string $main_layout = `columnLayout -adjustableColumn true`;<br><br> int $windowRowSize = 300-15;<br> int $row1col1Size = $windowRowSize * 0.2;<br> <br> int $number_of_rows = 4;<br> for ($i=0; $i<$number_of_rows; $i++)<br> {<br> $camera_select_row = `rowLayout -nc 5 -cw5 $row1col1Size $row1col1Size $row1col1Size $row1col1Size $row1col1Size<br> -columnAttach5 "both" "both" "both" "both" "both"<br> -columnOffset5 3 3 3 3 3<br> -columnAlign5 "center" "center" "center" "center" "center"<br> -parent $main_layout`;<br> $buttons_rows[$i] = $camera_select_row;<br> }<br> <br> setParent ..;<br><br> showWindow $window;<br> <br> // Init<br> clear $buttons;<br> on_add_new_button;<br>}<br><br>global proc on_add_new_button()<br>{<br> global string $buttons_rows[];<br> global string $buttons[];<br> global string $add_button;<br> <br> if (`button -exists $add_button`)<br> {<br> deleteUI $add_button;<br> }<br> <br> int $row_number = (`size $buttons`/5);<br> string $row_layout = $buttons_rows[$row_number];<br> string $new_button = `button -h 50 -l "?" -c on_button_click -p $row_layout`;<br> popupMenu;<br> int $button_number = `size $buttons`; // zero based (0 is the first button)<br> menuItem -label "Delete Button" -c ("on_delete_button " + $button_number);<br><br> $buttons[`size $buttons`] = $new_button;<br> <br> redraw_add_button;<br>}<br><br>global proc on_delete_button(int $button_number)<br>{<br> global string $buttons[];<br> <br> print2("Delete button number " + $button_number);<br><br> string $delete_button = $buttons[$button_number];<br> <br> if (`button -exists $delete_button`)<br> {<br> deleteUI $delete_button;<br> }<br> <br> stringArrayRemoveAtIndex($button_number, $buttons);<br>}<br><br>global proc print2(string $message)<br>{<br> print "\n >>>";<br> print $message;<br> print "\n";<br>}<br><br>global proc redraw_add_button()<br>{<br> global string $buttons_rows[];<br> global string $buttons[];<br> global string $add_button;<br> <br> if (`button -exists $add_button`)<br> {<br> deleteUI $add_button;<br> }<br> <br> print2(`size $buttons`);<br> <br> int $row_number = ( `size $buttons`/5);<br> string $row_layout = $buttons_rows[$row_number];<br><br> $add_button = `button -h 50 -l "+" -c on_add_new_button -p $row_layout`;<br>}<br><br>global proc on_button_click()<br>{<br>}<br><br></div>
Replies
@Panupat, I think the line added to windowsPrefs.mel is because I got a line that remove the window pref everytime it script run, I think that's why it's get added every time
@tsungyuw, I think it worked! Thanksssssss, I was thinking about trying to delay the delete function by using some sleep(1) function if maya got that thing.. but now I learned to use "evalDeferred", I stumbled upon this command a couple of times now I know what it is
Cheers!