/* uiBuilder by Nicolas d'Haussy Description : Useful coding helper. Writes melScript UI interface from function declaration string. No time wasted. Works in a workflow where you would separate cleary an UI and process/function (common case). Usage : buildUI "myFucntionDeclarationScring"; (e.g. buildUI "myFunction(int $var1, string $var , float $whatever)" ) Note : You could also directly have the window to appear using runBuildUI "myFucntionDeclarationScring"; For geeks : you could even use uiBuilder to create an UI that runs uiBuilder ;) */ // Could enhance the parser getting easly data such as variables and types for next versions // Parses and returns an array with arguments data type and arguments global proc string[] getFunctionArguments(string $functionString) { string $functionArguments = `match "[(].*[)]" $functionString`; // Take from ( to ) $functionArguments = `substring $functionArguments 2 (size($functionArguments)-1)`; // Remove parentheses cause they were included $functionArguments = `substituteAllString $functionArguments "," " "`; // Replace commas with spaces string $arguments[]; int $argumentsNb = `tokenize $functionArguments " " $arguments`; // A space blank separates type and variable itself return $arguments; } // Parses and returns the function name global proc string getFunctionName(string $functionString) { string $functionName = `match ".*[(]" $functionString`; return `substring $functionName 1 (size($functionName)-1)`; } // Removes "$" from the given string global proc string removeDollars(string $variable) { return `substituteAllString $variable "$" ""`; // Could be imporved (uses substituteAllString which is an external script) } // Builds arguments mel UI part selecting relevant interface controls global proc string buildArgumentsUI(string $functionInfo[]) { string $uiString; for($n = 0; $n < `size $functionInfo`; $n += 2) { $uiString += "\t"; string $noDollarVariable = removeDollars($functionInfo[$n+1]); // Check whether the variable is an array if(! `gmatch $functionInfo[$n+1] "*[[]]"`) { // Select ui item function to data type switch (`tolower $functionInfo[$n]`) { case "int": $uiString += "string " + $functionInfo[$n+1] + "Control = `intSliderGrp -l \"" + $noDollarVariable + "\" -f true -min -10 -max 10 -fmn -100 -fmx 100 -v 0`;\n"; break; case "float": $uiString += "string " + $functionInfo[$n+1] + "Control = `floatSliderGrp -l \"" + $noDollarVariable + "\" -f true -min -10.0 -max 10.0 -fmn -100.0 -fmx 100.0 -v 0`;\n"; break; case "string": $uiString += "string " + $functionInfo[$n+1] + "Control = `textFieldGrp -l \"" + $noDollarVariable + "\"`;\n"; break; default: $uiString += "// uiBuilder : \"" + $functionInfo[$n] + "\" is not a supported variable data type. " + $functionInfo[$n+1] + " has not been interfaced (see also button -c flag).\n"; break; } } else { $uiString += "// uiBuilder : arrays (e.g. $variable[]) are not supported. " + $functionInfo[$n+1] + " has not been interfaced (see also button -c flag).\n"; } } return $uiString; } // Builds launch button command string (derivated from buildArgumentsUI) global proc string buildCommandButton(string $functionString) { string $buttonString = getFunctionName($functionString); string $functionInfo[] = getFunctionArguments($functionString); for($n = 0; $n < `size $functionInfo`; $n += 2) { // Check whether the variable is an array if(! `gmatch $functionInfo[$n+1] "*[[]]"`) { // Select ui item function to data type switch (`tolower $functionInfo[$n]`) { case "int": $buttonString += " `intSliderGrp -q -v " + $functionInfo[$n+1] + "Control`"; break; case "float": $buttonString += " `floatSliderGrp -q -v " + $functionInfo[$n+1] + "Control`"; break; case "string": $buttonString += " `textFieldGrp -q -text " + $functionInfo[$n+1] + "Control`"; break; } } } return $buttonString; } global proc string buildUI(string $functionString) { string $uiString = "\n\n\n//---8<-----------------------------------\n// Created with uiBuilder\n"; string $functionName = getFunctionName($functionString); // Write classic window context $uiString += "if(`window -query -exists " + $functionName + "UI`) { deleteUI " + $functionName + "UI; }\n"; $uiString += "window -title \"" + $functionName + "Window\" -wh 400 300 " + $functionName + "UI;\n\tcolumnLayout -adjustableColumn true;\n"; $uiString += buildArgumentsUI(getFunctionArguments($functionString)); $uiString += "\tbutton -label \"Launch " + $functionName + "\" -c \"" + buildCommandButton($functionString) + "\";\n"; $uiString += "\tbutton -label \"Quit\" -c \"deleteUI " + $functionName + "UI\";\n"; $uiString += "showWindow " + $functionName + "UI;"; $uiString += "\n\n\n//---8<-----------------------------------\n\n\n"; return $uiString; } global proc runBuildUI(string $functionString) { eval `buildUI($functionString)`; }