PDA

View Full Version : MEL vs Python


Waynerz
09-26-2010, 09:09 PM
Hi all
I've written a few MEL scripts now and have a taste for the coding side of things. I'm going to have a crack at Python next, but I don't know what to start with.

I have a beginners book and looked through it. It seems like a nice syntax to work with; not a huge leap from MEL (except for the object oriented part).

At the moment, all the ideas i have had for scripts have been done with MEL. The things I have are very quick to execute, and MEL seems up to the task.

What I would like to know is when would you choose not to use MEL and use Python instead. What types of things would you code in Python?

Any suggestions of utilities/tools I could practise coding using Python to get a feel for it?

Thanks

efecto
09-27-2010, 05:31 AM
Best to way to learn python scripting for maya is to download some python scripts from highend3d and study the scripts. If you know mel then python isn't so hard to pick up. Perhaps start from converting your mel scripts to python.

Also pick up o'reilly's learning python book to get general udnerstandings of python. Also subscribe to Maya python group in google group.

Good luck.

Hi all
I've written a few MEL scripts now and have a taste for the coding side of things. I'm going to have a crack at Python next, but I don't know what to start with.

I have a beginners book and looked through it. It seems like a nice syntax to work with; not a huge leap from MEL (except for the object oriented part).

At the moment, all the ideas i have had for scripts have been done with MEL. The things I have are very quick to execute, and MEL seems up to the task.

What I would like to know is when would you choose not to use MEL and use Python instead. What types of things would you code in Python?

Any suggestions of utilities/tools I could practise coding using Python to get a feel for it?

Thanks

ginodauri
09-27-2010, 07:30 AM
Don't learn python through other scripts.

Take some good book.o'reilly's learning python is good for starting.

Read book from start to end and see everything python has to offer.

If you start with minimal python knowledge , you are gonna actually scripting mel through python.

If you learn python consider learning pymel.There you can see python at his full potential.

Waynerz
09-27-2010, 07:16 PM
Thanks for the advice, guys.

So MEL and python do pretty much the same thing until you start making larger scripts where python's OOP starts to make a big difference?

I dont know anything about OOP yet, so I'll look foward to trying that out.

Cheers :)

nessus
09-30-2010, 01:38 AM
Thanks for the advice, guys.

So MEL and python do pretty much the same thing until you start making larger scripts where python's OOP starts to make a big difference?

I dont know anything about OOP yet, so I'll look foward to trying that out.

Cheers :)

I'd say dont worry too much about OOP yet. This concept is normally only considered useful when the scale of the program is relatively large. Say you were to write a large python library instead of the a script/tool to solve a particular problem.

I personally find that i still program python 90% the time in a procedure style (like coding in MEL) rather than considering packaging implementations into classes.

haggi
09-30-2010, 09:34 AM
I dont agree. You should know how the whole obejct oriented thing works and how to use it efficiently.
The way python is implemented in maya is not very pythonic. The whole cmds module is nothing else but a simple translation of mel commands to python commands. So if you do a:
objList = cmds.ls()
cmds.setAttr(objList[0] + ".tx", 5)

You will get a list of strings. So if you use python with maya, you should always use pymel. Pymel is a wrapper for the default python in maya so that it works as you expect. With the pymel ls(), you will get objects. So the above example would look like:
objList = ls()
objList[0].tx.set(5)

This way you will have to use objects a lot. And if you create a user interface, the pymel way is much more interesting because you can override the default behaviour. e.g. you can create a new class and inherit the Window() class and encapsulate all necessary methods in this class. So if you push a button, the whole data management is very much easier.
This way you will have to use objects and classes even for small scale projects.

The other advantage of using pymel and python is that you code will shrink. All our mel scripts which are now rewritten in python, need no more than half the lines of code than the old mel ones.

RyanT
10-01-2010, 08:13 PM
"...you should always use pymel."

Is a matter of opinion and user preference.

I agree that you should read a Python book to get a good grasp on how Python works. If you start working with Maya using its standard maya python commands it will give you a good understanding of how things work. If you learn how to write Python and do it the way Maya works you will be able to write your own functions that override things you dont like. Pymel under the hood is just using standard Maya Python commands ect. and is essentially just a bunch of functions that users have written to extend or change the way the standard Maya commands work with Python.

The advantages of Python that I dont see mentioned here are: Python can call any command MEL can, Python can call MEL functions from your other scripts if needed, Python can use Maya API C++ classes. There are no cases where you would need to use MEL. However MEL can be easier to learn because the script editor prints out MEL that users can copy and paste. By using the API C++ classes you can write tools that process meshes that are hundreds of times faster than MEL as well. The API is a more advanced feature that you would have available to you later after you have learned everything else though.

I have several tutorials on my web site for Python if you want to check them out.

NateH
10-01-2010, 10:02 PM
There are no cases where you would need to use MEL.

Sadly this isn't true, some UI callbacks must still be in mel, or at the very least wrapped in a simple wrapper function with mel. Hopefully they fix this soon...

On everything else I agree. Just want to add my $.02: I've been using Python in Maya since it was added, and pymel or not, it's the way to go for sure. Being able to intermix the C++ api and Maya commands together is really powerful, and that's just the icing on the cake of a full featured programming language. Python is a "Batteries-included" sort of tool: the list of standard modules is huge and the community is massive by comparison to mel.

Waynerz
10-02-2010, 03:21 PM
Thanks again, all. Very informative. I will check out your suggestions and tutorials.

misterwolfy
10-02-2010, 03:48 PM
I am kind of in your shoes, but maybe a few months ahead. I know MEL ok, definitely not a master, but I can write scripts that are useful. I wanted to learn Python too, so I did some tutorials, read some way through a couple of books, and got a bit confused at first. I just didn't get it.

I had a breakthrough the other day though, and now I feel like I am flying, relatively speaking, with Python. Now I really love Python. MEL is hard to look at compared to Python's elegant syntax. Plus Python can be useful outside of Maya.

The thing that really got me going was file objects (http://docs.python.org/release/2.5.2/lib/bltin-file-objects.html). This means you can open or create a text file and read and write to it. so for example:


myData = open("path/hello.txt", r)
theLines = myData.readlines()


This will open a text file, hello.txt, to a variable called myData, then it will read the lines of the file into a list (like an array) called theLines. Now you can manipulate the list with pythons multitudinous commands.

Like I said, I'm basically a beginner, but I hope that helps, or at least encourages you to learn.

For a painless and fun way to break into Python, try this link:

http://www.youtube.com/watch?v=4Mf0h3HphEA

Waynerz
10-02-2010, 06:59 PM
cheers misterwolfy, those videos look cool. I'll be checking them out :)

haggi
10-02-2010, 11:25 PM
"...you should always use pymel."
Is a matter of opinion and user preference.


Of couse you are right. And indeed pymel is nothing else but a wrapper for maya.cmds. But a very powerful one. Without pymel you simply code python the old mel - way what resuts in a much larger and heavier to read code. Thats a fact, and of course my personal preference.

e.g. I prefer to read something like:

myObject = ls("objecname")[0]
trans = myObject.translate.get()
myObject.translate.set([trans[0] + 2, trans[1] + 2, trans[2] + 2])

instead of:

myObject = ls("objectname")[0]
trans = getAttr(myObject + ".translate")
setAttr(myObject + ".translate", [trans[0] + 2, trans[1] + 2, trans[2] + 2])

misterwolfy
10-06-2010, 03:17 PM
For beginners there is actually no point in learning MEL. Although there are some Maya related issues with the commands wrapped that are quite not so "pythonic" and people need to get into.

What about expressions, can these be written in Python?

ewerybody
10-06-2010, 03:19 PM
about pymel: did (http://www.macaronikazoo.com/?p=309) you (http://www.macaronikazoo.com/?p=290) read (http://www.macaronikazoo.com/?p=271) Mr McKenzie (http://www.macaronikazoo.com/?p=350)?

turns out its nice but not the silver bullet..
As I have .. I'd say quite some knowledge of Maya and MEL, I actually decided to not use pymel so far... but python of course!

For beginners there is actually no point in learning MEL. Although there are some Maya related issues with the commands wrapped that are quite not so "pythonic" and people need to get into.

haggi
10-07-2010, 11:28 AM
What about expressions, can these be written in Python?

No they cannot.

haggi
10-07-2010, 12:03 PM
Of course, a wrapper cannot have the same speed as the wrapped element.

If you really need speed, use python maya api directly or c++ and not pymel.
We use it in our production environment since several years and we never had speed issues. Well, maybe it is because we seldom need to collect all objects in the scene a thousend times as in the example from the link above (this here (http://www.macaronikazoo.com/?p=271)). His time was 24 seconds. If we used such a thing, we call the scene once and if my mathematical knowledge is correct, this would took 0.024 seconds.
Another example is a simple non optimal usage of creating classes.
So most of his examples are very special cases where you have to thing about optimisations anyway. So pymel is not the ultimate solution, but it is worth working with it.

ginodauri
10-08-2010, 05:33 AM
What about expressions, can these be written in Python?

You can do following.
Writing python , pymel code in "external" file.
Good practice is to put this file in project script directory.
Than in maya expression editor just call python procedure.
example(python("myFile.myproc()").

ewerybody
10-08-2010, 10:24 AM
of course expressions are more MEL like but actuallly they are neither MEL nor python. They surely have a little of a language of itself.
Of course its possible to call MEL commands (like python (http://download.autodesk.com/us/maya/2009help/Commands/python.html)) from an expression. But be aware that for best performance an expression should contain only expression style code (which is not always possible, I know)

ginodauri
10-08-2010, 11:01 AM
That is right.
Expressions not equal to mel.
Expression node is like any maya node in sense , that is take inputs and calculates outputs.
So writing python procedure in external file is not good practice because you can't directly "connect" output from expression.
Solution to this would be that python procedure should return value , and than you can make connections.
Example:
float $values=python("myfile.myproc")
node1.input=$value[0];
etc


One thing that i don't like about expressions is that your output can't be anything else than float value(you cannot output float3 etc)

CGTalk Moderation
10-08-2010, 11:01 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.