//=========================== guipy 1.6 =========================================// // // guipy.mel 1.6 // // 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. // // Simple mel script for running python scripts. The mel scripts should be place in the // default scripts directory, which is usually, "..maya/8.5/scripts. // // The 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 // //==================================================================================// global proc guipy() { string $gpPath = `internalVar -userScriptDir`; string $gpFiles[] = `getFileList -fld $gpPath`; // Interface Begins if (`window -ex guipyWindow`) deleteUI guipyWindow; string $guipyWin = `window -title "Gui Py 1.6" -iconName "GP" -rtf 1 -s 0 guipyWindow`; string $guipyForm = `formLayout -numberOfDivisions 100`; // Create Controllers text -label "Select a Python Script to run:" gpLabelName; textField -text $gpPath -width 356 gpdirNameField; button -width 70 -height 22 -label "Browse" -command "browseForDirectory" gpbrowseButton; textScrollList -w 430 -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 "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 "gpLabelName" "left" 10 -af "gpLabelName" "top" 12 -af "gpdirNameField" "left" 18 -ac "gpdirNameField" "top" 8 "gpLabelName" -ac "gpbrowseButton" "top" 8 "gpLabelName" -ac "gpbrowseButton" "left" 4 "gpdirNameField" -af "gpythonList" "left" 18 -ac "gpythonList" "top" 6 "gpdirNameField" -af "gpActionText" "left" 30 -ac "gpActionText" "top" 12 "gpythonList" -ac "gprunButton" "left" 20 "gpActionText" -ac "gprunButton" "top" 12 "gpythonList" -ac "gpsourceAndRunButton" "left" 24 "gprunButton" -ac "gpsourceAndRunButton" "top" 12 "gpythonList" -ac "gpsourceOnlyButton" "left" 24 "gpsourceAndRunButton" -ac "gpsourceOnlyButton" "top" 12 "gpythonList" -ac "createButton" "top" 12 "gprunButton" -af "createButton" "left" 4 -af "createButton" "bottom" 4 -ac "applyButton" "top" 12 "gprunButton" -ac "applyButton" "left" 4 "createButton" -af "applyButton" "bottom" 4 -ac "closeButton" "top" 12 "gprunButton" -ac "closeButton" "left" 4 "applyButton" -af "closeButton" "bottom" 4 -af "closeButton" "right" 4 $guipyForm; // Interface Ends showWindow $guipyWin; } 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`,""); if (size($selectedPyFile)>0) { string $actionSelection = `radioCollection -q -sl gpActionType`; switch ($actionSelection) { case "gprunButton": python ($selectedPyFile + "." + $selectedPyFile+"()"); break; case "gpsourceAndRunButton": python ("import " + $selectedPyFile ); python ("reload (" + $selectedPyFile + ")" ); python ($selectedPyFile + "." + $selectedPyFile+"()"); break; case "gpsourceOnlyButton": python ( "import " + $selectedPyFile ); break; } } }