//=========================== guipy 2.0 =========================================// // // guipy.mel 2.0 // // By Andrew Osiow 2009 Copyright // // To be used freely with the aknowledgement that the author is not responsible // for any perceived damage this script may directly (or indirectly) cause. // Please contact the author if you plan to use this script in a commercial project. // // This is a Simple mel script for running python scripts. One small advantage that // Mel scripts still have over Python is if they are in the default scripts directory, // they are "sourced" at start up. // // GuiPy gives you quick access to any Python scripts. A user can execute or import // and execute any python script from a conveinent list gadget // // This script a slightly modified version of Mel-O-Matic which did the same thing // for Mel Scripts // // Guipy scripts should be place in the default scripts directory, which is usually, // "..maya/2009/scripts. // // v2.0 adds a collapable script reporter gadget with "clear history" and "echo all" controls // adds a execute command strng gadget when then script command is different // than the file name // //==================================================================================// global proc guipy() { global int $gpWindowHeight; string $gpPath = `internalVar -userScriptDir`; string $gpFiles[] = `getFileList -fld $gpPath`; // Interface Begins if (`window -ex guipyWindow`) deleteUI guipyWindow; string $guipyWin = `window -title "GuiPy 2.0" -iconName "GuiPy" -rtf 1 -s 0 guipyWindow`; string $guipyForm = `formLayout -numberOfDivisions 100 guipyLayout`; // Create Controllers string $scriptHistorySection = `frameLayout -w 470 -h 294 -l "Script History" -cll 1 -mw 4 -bs "in" -cc "formLayout -e -af \"gpScriptRunSect\" \"top\" 20 guipyLayout; window -e -h $gpWindowHeight guipyWindow" -ec "formLayout -e -af \"gpScriptRunSect\" \"top\" 294 guipyLayout; window -e -h ($gpWindowHeight+274) guipyWindow" gpScriptHistorySect`; string $guipyHistoryForm = `formLayout -numberOfDivisions 100 guipyHistoryLayout`; cmdScrollFieldReporter -width 430 -height 210 gpFieldReporter; button -width 150 -height 26 -label "Clear History" -align "center" -command "cmdScrollFieldReporter -e -clr gpFieldReporter" gpClearButton; // Clears History checkBox -label "Echo All Commands" -cc "cmdScrollFieldReporter -e -eac (`checkBox -q -v gpEchoSwitch`) gpFieldReporter" gpEchoSwitch; formLayout -e // arrange layout -af "gpFieldReporter" "left" 16 -af "gpFieldReporter" "top" 12 -af "gpClearButton" "left" 60 -ac "gpClearButton" "top" 12 "gpFieldReporter" -af "gpEchoSwitch" "left" 250 -ac "gpEchoSwitch" "top" 18 "gpFieldReporter" $guipyHistoryForm; setParent $guipyForm; string $scriptRunSection = `frameLayout -w 470 -h 310 -l "Select a Python Script" -cll 0 -mw 4 -bs "in" gpScriptRunSect`; string $guipyRunForm = `formLayout -numberOfDivisions 100 guipyRunLayout`; textField -text $gpPath -width 356 gpdirNameField; button -width 70 -height 22 -label "Browse" -command "browseForDirectory" gpbrowseButton; textScrollList -w 430 -sc "textField -e -text (stringArrayToString(`textScrollList -q -si gpythonList`,\"\")+\"()\") gpExCommandField" -numberOfRows 12 -allowMultiSelection false -showIndexedItem 1 gpythonList; string $gpFile; for ( $gpFile in $gpFiles ) if ($gpFile != "guipy.mel") // don't include this script in the list if ( fileExtension($gpFile) == "py") textScrollList -e -append (basename($gpFile,".py")) gpythonList; text -label "Execute Command" gpExCommandLabel; textField -text "" -width 300 gpExCommandField; text -label "Action" gpActionText; radioCollection gpActionType; radioButton -label "Execute" gprunButton; radioButton -label "Import and Execute" gpsourceAndRunButton; radioButton -label "Import Only" gpsourceOnlyButton; radioCollection -e -sl "gpsourceAndRunButton" gpActionType; button -width 150 -height 26 -label "Execute" -align "center" -command "runPython; deleteUI guipyWindow" createButton; // executes and closes button -width 150 -height 26 -label "Apply" -command "runPython" applyButton; // executes and stays open button -width 150 -height 26 -label "Close" -command "deleteUI guipyWindow" closeButton; // closes interface // Layout Interface formLayout -e // arrange layout -af "gpdirNameField" "left" 18 -af "gpdirNameField" "top" 8 -af "gpbrowseButton" "top" 6 -ac "gpbrowseButton" "left" 4 "gpdirNameField" -af "gpythonList" "left" 18 -ac "gpythonList" "top" 6 "gpdirNameField" -af "gpExCommandLabel" "left" 20 -ac "gpExCommandLabel" "top" 10 "gpythonList" -ac "gpExCommandField" "left" 8 "gpExCommandLabel" -ac "gpExCommandField" "top" 8 "gpythonList" -af "gpActionText" "left" 30 -ac "gpActionText" "top" 16 "gpExCommandLabel" -ac "gprunButton" "left" 20 "gpActionText" -ac "gprunButton" "top" 16 "gpExCommandLabel" -ac "gpsourceAndRunButton" "left" 24 "gprunButton" -ac "gpsourceAndRunButton" "top" 16 "gpExCommandLabel" -ac "gpsourceOnlyButton" "left" 24 "gpsourceAndRunButton" -ac "gpsourceOnlyButton" "top" 16 "gpExCommandLabel" -ac "createButton" "top" 12 "gprunButton" -af "createButton" "left" 4 -ac "applyButton" "top" 12 "gprunButton" -ac "applyButton" "left" 4 "createButton" -ac "closeButton" "top" 12 "gprunButton" -ac "closeButton" "left" 4 "applyButton" -af "closeButton" "right" 4 $guipyRunForm; setParent $guipyForm; formLayout -e // arrange layout -af "gpScriptHistorySect" "top" 0 -af "gpScriptHistorySect" "left" 0 -af "gpScriptHistorySect" "right" 0 -af "gpScriptRunSect" "top" 294 -af "gpScriptRunSect" "left" 0 -af "gpScriptRunSect" "right" 0 -af "gpScriptRunSect" "bottom" 0 $guipyForm; // Interface Ends showWindow $guipyWin; $gpWindowHeight = `window -q -h guipyWindow`; frameLayout -e -cl 1 gpScriptHistorySect; formLayout -e -af "gpScriptRunSect" "top" 20 guipyLayout; window -e -h ($gpWindowHeight-274) guipyWindow; $gpWindowHeight = `window -q -h guipyWindow`; } global proc browseForDirectory() {// Open file browser to default Script Directory string $pyPath = `internalVar -userScriptDir`; $pyPath = `fileDialog -directoryMask $pyPath`; string $pyDirectory = dirname ($pyPath) + "/"; textField -e -tx $pyDirectory gpdirNameField; textScrollList -e -ra gpythonList; string $pyFiles[] = `getFileList -fld $pyDirectory`; string $pyFile; for ( $pyFile in $pyFiles ) if ($pyFile != "guipy.mel") // don't include this script in the list if ( fileExtension($pyFile) == "py") textScrollList -e -append (basename($pyFile,".py")) gpythonList; } global proc runPython () { // execute Python script - source first if requested python ("import os, sys"); string $pyPath = `textField -q -tx gpdirNameField`; python ("sys.path.append(os.path.abspath('" + $pyPath + "'))"); string $selectedPyFile = stringArrayToString (`textScrollList -q -si gpythonList`,""); string $excutionCommand = `textField -q -text gpExCommandField`; if (size($selectedPyFile)>0) { string $actionSelection = `radioCollection -q -sl gpActionType`; switch ($actionSelection) { case "gprunButton": python ($selectedPyFile + "." + $excutionCommand); break; case "gpsourceAndRunButton": python ("import " + $selectedPyFile ); python ("reload (" + $selectedPyFile + ")" ); python ($selectedPyFile + "." + $excutionCommand); break; case "gpsourceOnlyButton": python ( "import " + $selectedPyFile ); break; } } }