Wednesday 3 November 2010

Update Time - This is how we MEL it.

This past week-ish I've been learning MEL scripting

I decided to write a naming script, firstly I had to outline the features and limitations of the script

i.e. what did it name
did it rename the whole object
how did it know what to call the objects

Next I identified the validation NEEDed [users are dumb and break things], the main body of validation would be in the actual naming section, the script will check for any previous naming applied. Secondary validation would be in the prefix and sufix default values applied if nothing is present in the user fields.

After I had a rough idea of what I wanted it to do I could start to piece together the different sections starting with a GUI window, [a good way to instantly feel like you've accomplished something is to make a window] and piece by piece I wrote this [with help from georg]:


proc sc_gui()
{
    string $selection[] = (`ls -selection`);  
    if (`window -exists "sc_gui"`) deleteUI "sc_gui";     
        window -w 150 -h 300 -t "GUI" -maximizeButton false -minimizeButton false sc_gui;             
        print "Selected: ";           
        print ($selection);         
            columnLayout;
                text -w 150 -al "left" -fn "boldLabelFont" -l "Prefix";
                textField -width 150 sc_preffix;
                text -w 150 -al "left" -fn "boldLabelFont" -l "Sufix";
                textField -width 150 -text `date -format "DDMMYY"` sc_suffix;
                button -label "Convention" -w 150 -command "sc_fix";
                separator -style "double" -w 150;
                button -label "Delete" -w 150 -command "sc_undo";
        showWindow "sc_gui";
}

proc sc_fix()
{
    string $marker = "nc_";
    string $prefix = `textField -q -text sc_preffix`;
    string $sufix = `textField -q -text sc_suffix`;   
    string $selection[] = (`ls -selection`);
    if (size($prefix) == 0 ||  size($prefix) < 1 )
        {
            print "\nNo Prefix defined\n";
        }
    else
        {
            print "\nPREFIX: ";
            print ($prefix + "\n");
        }
    if (size($selection) == 0 ||  size($selection) < 1 )
        {       
        print "No Objest selected\n";
        }
    else
        {  
        for ($items in $selection)
             {
                print ($items + "\n");
                string $matches = (`match $marker $items`);
                print ($matches + "\n");
                if ( $matches == $marker)
                    {
                     print "false \n ";
                    }
                else
                    {
                    rename $items ("nc_" + $prefix + "_" + $items + "_" + $sufix);
                    }
            }
        }
    string $selection[] = (`ls -selection`);
            print "Selected:\n";
            print $selection "\n";
}

proc sc_undo()
{  
        delete `ls -selection`;
}
sc_fix()
sc_undo()
sc_gui()

This either looks like a lot or very little, but it works, and the exercise wasn't about really making something more getting to know the language.

I also created a flow diagram to accompany the sc_fix() process though largely incomplete as it doesn't show what's being called and where from, it was used mainly to map out the logic behind the IF statements as this was a big problem. I wont be uploading it here as I don't feel its entirely necessary, and most of the documentation for the script is just running notes that I've made while coding, i.e. list of string var's and this is the kind of thing that is better suited in release notes, which really is overkill for the project.

Moving on....
If you noticed the red parts of the script and wondered what that is, well it links down to the last proc () at the bottom, this is the process that is called and the location its called from, and if you notice the process is labelled "undo" and that's the next feature I shall be adding, an "undo" button. I could have just stopped here as the script works as intended but to make the undo function I will have to learn scripting nodes, pushing my knowledge further and advancing my learning.

Note: When you run the script, it will produce this;

// Error: sc_undo()
//
// Error: Syntax error // 

ignore it, the error comes from line 65 and will change when you rearrange the proc()'s and will reflect what ever is on line 65, if nothing is on line 65 it will move down to the next filled line. Completely weird but can't figure out why it happens at the moment.  

FIXED!!!!!

No comments:

Post a Comment