Home Technical Talk

MEL syntax ?

CreativeSheep
polycounter lvl 8
Offline / Send Message
CreativeSheep polycounter lvl 8
I have two questions about MEL programming, the first.  If I want to declare an integer I must always use int, I can't type;

int $apple = "bbox"; // correct ? It must be;
int $apple = 54; // always a number, the same can be said for floats ?

What is meant by, MEL allows you to type commands in command syntax and function syntax ?

Replies

  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    int $apple = "bbox" is technically trying to assign a string to the variable. You're not actually assigning the bbox (which by the way will return 6 floats) to that variable.

    So what you want is to create an array and query the bounding box via xform (http://download.autodesk.com/us/maya/2010help/Commands/xform.html) :
    float $bbox[] = `xform -q -bb "meshName"`; 

    See the ` ` that wraps the xform command? That's how you get data using MEL. I don't use MEL anymore, but when I did this was a good learning resource: https://nccastaff.bournemouth.ac.uk/jmacey/RobTheBloke/www/mel/index.html#5


    Hope this helps
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    int $apple = "bbox" is technically trying to assign a string to the variable. You're not actually assigning the bbox (which by the way will return 6 floats) to that variable.
    6 floats ?
    I know, no one uses MEL, well, maybe not no one, before I move to python I want to learn some MEL !

  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    Yes, 6 floats . The bounding box is essentually  the following values: xmin,xmax, yminx,ymax,zmin,zmax.

    And if you're not sure what a float is, do check Rob The Bloke's page : https://nccastaff.bournemouth.ac.uk/jmacey/RobTheBloke/www/mel/DDATA_basic.html


  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    From a string in a integer variable, you get; xmin, xmax, ymin,ymax,zmin,zmax ? 

    Backticks is how you get data in MEL, what is the difference between

    setAttr("mySphere1.translateX".10); 
    setAttr mySphere1.translateX 10;

    ==============================

    $a = getAttr ("mySphere.translateX");
    $b = `getAttr mySphere.translateY`;
  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 18
    I'd go straight to python imo. If you get used to python and ever need to switch to MEL for whatever reason you won't have a problem adapting.

    Here's a good resource to get started with python.
    http://www.chadvernon.com/blog/resources/python-scripting-for-maya-artists/
  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    "From a string in a integer variable, you get; xmin, xmax, ymin,ymax,zmin,zmax ? "
    See the [] in the bbox float I made earlier? That signifies it's a list of numbers, or an array. If you look at what I previously typed:

    float $bbox[] = `xform -q -bb "meshName"`;  and apply it to mySphere you'll get this:
    // Result: -1 -1 -1 1 1 1 // 

    While it "looks" like integers, they're not! This is what you get if you try:

    int $bbox[] = `xform -q -bb "mySphere"`; 
    // Error: Line 1.11: Invalid redeclaration of variable "$bbox" as a different type. // 

    You need to be careful when you create variables in MEL otherwise you'll get errors like that.

    As for:

    $a = getAttr ("mySphere.translateX");
    $b = `getAttr mySphere.translateY`;

    I suppose they are the same, but I'd recommend you use the backticks: http://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/Commands/getAttr.html

    Yes, I'd also recommend Python first. Chad's website is pretty good, I also recommend http://learnpythonthehardway.org/
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    float $bbox[] = `xform -q -bb "meshName"`;  and apply it to mySphere you'll get this:
    // Result: -1 -1 -1 1 1 1 // 
    How do you know, what to declare as a float, int in MEL ? What about my int $apple = "bbox" variable, will Maya read it as a integer or a string ?

    setAttr("mySphere1.translateX".10); 
    setAttr mySphere1.translateX 10;

    ==============================

    $a = getAttr ("mySphere.translateX");
    $b = `getAttr mySphere.translateY`;

    What is different ? 

    Why isn't there a quoting system on this new forum update for polycount ?
  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    No idea about quoting, it is kinda busted :/

    "How do you know, what to declare as a float, int in MEL ? What about my int $apple = "bbox" variable, will Maya read it as a integer or a string ?"

    I think for now, you'd be better off not explicitly declaring a type and let Maya assign it automatically. So...

    $bat = 50; 
    $bbox = `xform -q -bb "mySphere"`;

    and so on.
    You can use whatIs to see what the resulting variable type is:

    whatIs "$bat";// Result: int variable // 
    whatIs "$bbox";// Result: float[] variable // 

    If we look at int $apple = "bbox" - you are assigning a string to an integer variable so you won't get anything:
    // Result: 0 // 
    // Result: int variable // 

    But.. if we allow Maya to automatically assign the variable:

    $apple2 = "bbox";
    whatIs "$apple2";
    // Result: string variable // 

    As for getAttr/setAttr - I'd say just choose what works for you and stick with it to be consistent... but again I'd recommend using the ` ` to get data.

    Honestly at this point, you should try a few examples, go through the MEL resources Autodesk provides, go through RobTheBloke and try some stuff and see how it works.. or maybe even try Python since there are more resources available to help understand data structures and flow etc
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    Thanks for the info on the whatls command and a few other tips :smile:, but; 
    $bat = 50;
    whatls "$bat"; // doesn't write a result, instead gives an error ?

    I'm reading though some MEL resources, but I sometimes stumble across something to which I must ask a question :smile:
  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    What's the error? Make sure everything is entered correctly (case sensitive) and that you're ending with ; etc.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    The error is cannot find procedure ?
    $bat = 50;
    whatls "$bat";
  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    whatIs with an I, not L

    it will turn blue to indicate it's a recognized MEL command
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    Yeah I know it's with an I, otherwise it wouldn't be blue, but it's still giving me the same error ? :disappointed: 

  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    You didn't type it in correctly when you posted it here - a simple copy and paste  into the script editor confirmed you had typos. I'd suggest you double check and try again
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    If I assign a float to a variable, then change the float to a string, Maya continues to see it as a float, rather then a string ?
  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    I'm assuming you're trying to re-cast the same variable to a string right? You cannot recast the same variable as a different type once it has been declared. You'll want to make a new variable:

    float $a = 4.00;

    whatIs "$a"; // Result: float variable //

    string $b = (string)$a;

    whatIs "$b"; // Result: string variable // 


    With Python you can - but with MEL nope. 


  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 18
    Come to the dark side! Python has cookies and crazy re-casting of variables all over the place.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    Yeah I know you can convert variables in Python, I'm not going to the dark side, yet :smile:

    MEL saves, even if not executed, code automatically to memory; that is why a variable can't be changed ?
  • fmnoor
    Options
    Offline / Send Message
    fmnoor polycounter lvl 17
    No, that's not why you can't re-cast (it shouldn't be)..  It's just how MEL works as as scripting language I suppose. DIfferent languages, different quirks. 

    Also nothing is actually saved into memory unless you execute the script. You can have dozens of lines and just highlight and execute one if you want. If you want to know what's loaded into memory use env: http://download.autodesk.com/us/maya/2009help/Commands/env.html


    IDK about Python being the dark side. While I get why you want to start with MEL first, you might find it easier to pick up scripting with Python. There are a lot less quirks and it's more "standardized" per se. But honestly, whatever works for you - do you have any projects in mind? When I taught myself I basically had a tool (like auto-create basic collision meshes for exporting into Unreal) in mind and let that drive my learning
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    I have a project in mind, that I first want to create in MEL, then Python. :smile: There will be more questions when I begin to build my tool.
Sign In or Register to comment.