I’m loving Python!

loocas | 3ds Max,maxscript,Maya,opinions,Python,software,technical | Sunday, March 2nd, 2008

max_maya_python.png

Interestingly enough, when I started looking into extending my technical skills and knowledge beyond 3ds Max itself at first I got a bit frustrated. The reason was I thought (as probably many other technical artists out there, based on many discussions I read) that I spent years developing some scripting and technical skills to find out they were useless and I’ll have to learn something entirely different in order to stay at the cutting edge and still being competitive. Now while 3ds Max isn’t a factor here, it’s just a platform anyways, I dived into Maya recently and faced a, seemingly, difficult decision: do I go the MEL way or do I learn Python (available in Maya since 8.5)? The decision turned out to be petty simple! I’ll learn both! As I devoured the user reference files, some tutorials and some books, I found out that once you learn one scripting language syntax and logic, picking up any other (similar) language is quite easy.

Well, MEL, out of the three, comparatively, is the most difficult to grasp, not because of the sheer number of functions and attributes, but because the syntax is a bit too complicated (to my tastes) and the integration of MEL in Maya is also a little too restrictive (compared to MAXScript that I’m coming from). Now, the fact that Maya supports Python (too bad Max doesn’t natively support it as well), even though in a bit of a “MEL falvour” is pretty cool and only thanks to this fact I was able to pick up scripting in Maya very, very quickly! So, what’s so special about Python? Python is extremely easy to learn! Let me compare complexity-wise the three: Python < MAXScript < MEL and if I was to compare the three feature-wise: Python > MEL > MAXScript, so the clear winner is Python. Now, I’m primarily a 3ds Max TD, therefore I’m not able to fully appretiate Python’s power and strength from within Max, or am I? Thanks to the ever-so-awesome folks at Blur and their dedication to using 3ds Max in their pipeline, we have something that’s called “blurPython” which is an extension to MAXScript that allows you to call Python functions and modules. This alone takes scripting in Max to a whole new level! Even though you don’t have a direct Python syntax support in Max, you can’t call Max’s functions with Python (sort of, you can do that actually but it hasn’t got much to do with Python itself) you can access Python modules and functions from within Max, which is awesome. Now think about the feature set of Python, the fact that Python has been around for longer than Max itself and that it really is a production proven scripting framework and you’ve got a one hell of a powerful weapon in your TD arsenal! Especially if you’re into building custom pipelines and data sharing!

This article isn’t that much about all the posibilities you can do with Python itself, I’d rather like to compare some features of MEL, MXS (MAXScript) and PY (Python). So, first things first. When I started exploring Maya’s scripting and its logic, I buped several times into my old habbits of code testing, exploring and writing that I carried from Max. Some examples, when developing some code I tend to setup a bunch of testing global variables in order to speed up some UI testing, cut down on number of clicks in order to test out some tools with more complex UI etc… So I started developing some primitive scripts in Maya that were a bit ahead of print "YAY!\n"; :D. Very soon I bumped in MEL’s design “limitation” (it’s not a limitation so to speak, only in my eyes it looks like a limitation, while in fact it’s simply a matter of design), I cannot reassign a different value class to an already declared variable. Let me explain: MXS allows me to do the following: global testVar = "Hello!"; testVar = 10 which means: globally declare a variable called “testVar” and assign it a string value of “Hello!” then try to assign it an integer value of “10”. MEL won’t let you do this, which was to me, as a starting MEL scripter, quite confusing. The problem got more serious and I got frustrated even more when I learned that there was no way of “undeclaring” variables in MEL, so the “testVar” was available all the time during the session (which means until I restarted Maya). So, as recommended, you should always test your code in a local scope in MEL, which means writing your code within two curly braces: { ⟨your code⟩ }

This was quite shocking to me as a MXS scripter that I didn’t have the freedom to reassign variables with different value classes. Further on, the limitation touched all the aspects of the language I was used to, like arrays that can only carry a predefined value class, you can’t mix up integers with strings for example in MEL’s arrays (lists). So I put up with the fact and tried to learn a “different” syntax. I spent about 2 weeks learning MEL intentionally avoiding Python in Maya as, at first, I thought it was some kind of a “second layer” language above MEL that won’t do much difference in Maya. Oh, how wrong I was! Python, in fact, is much more powerful (to my surprise) in Maya than MEL. You can do the same with Python in Maya as you can with MEL, however, you can do much more with Python than you can do with MEL! This is quite interesting and since before MEL I had been looking into Python a bit, I got interested in it even more.

Everybody on the net says how easy, yet powerful, Python really is, I had to prove it to myself and started looking into the syntax and general Python logic. How glad and happy was I when I discovered that you CAN reassign different class values to a single variable (just like in MXS), I can mix up array (list) item classes any way I like (same as MXS), but the real surprise came when I realized that the PY syntax indeed was easy, so easy that I even think that it’s easier than MXS! And MXS really is easy to pick up and code in, believe me.

Ok, let’s take a look at some examples that I discovered which are very important in scripting in general. The globally declared variables in PY and MXS can be reassigned with a different value class! MEL doesn’t support this feature.

Python:
varA, varB, varC = 10, 20, 30
print varA, varB, varC
varA, varB, varC = "It", "just", "works"
print varA, varB, varC

MAXScript:
varA = 10; varB = 20; varC = 30
print varA; print varB; print varC
varA = "It"; varB = "just"; varC = "works"
print varA; print varB; print varC

(notice the difference, Python actually supports multiple variable assignments on a single line in order as opposed to MXS)

MEL:
{
   $varA = 10; $varB = 20; $varC = 30;
   print ($varA+"\n"); print ($varB+"\n"); print ($varC+"\n");
   $varA = "It"; $varB = "just"; $varC = "works";
   print ($varA+"\n"); print ($varB+"\n"); print ($varC+"\n");
}

Notice that MEL won’t let you reassign a different value class to an already created variable! It’ll try to convert the new class input to the one implicitly created. So, the code above will actually result in:

// Warning: $varA = “It”; $varB = “just”; $varC = “works”; //
// Warning: Converting string “It” to an int value of 0. //
// Warning: $varA = “It”; $varB = “just”; $varC = “works”; //
// Warning: Converting string “just” to an int value of 0. //
// Warning: $varA = “It”; $varB = “just”; $varC = “works”; //
// Warning: Converting string “works” to an int value of 0. //
// Result: 0 //

Now, let’s take a look at loops. These are most common in any scripter’s arsenal so you really have to learn how to write these in order to process repetitive tasks!

Python:
for i in range(1, 10, 2):
   print i

MAXscript:
for i = 1 to 10 by 2 do(print i)

MEL:
{
   for ($i = 1; $i < 10; $i+=2)    {       print ($i+"\n");    } }

(notice that I put the MEL code in a local scope)

The MAXScript syntax in this case is far easier to read than Python and especially then MEL (also notice how many extra characters you have to put in the code in order to achieve the same result)

Another very useful feature is functions (in MEL they're called procedures). I'm yet into learning what all is possible with functions in Python, but what I've learnt so far is quite enough for my needs.

Python:
def sumTheNumbers(num1, num2):
   return num1+num2
sumTheNumbers(10, 20) #call the function with some input numbers of choice
sumTheNumbers("You ", "can!") #notice that Python doesn't actually care what class of variables you throw at it as long as it can perform the required task

MAXScript:
fn sumTheNumbers num1 num2 =
   (
      return num1+num2
   )
sumTheNumbers 10 20 -- call the function with some input numbers of choice
sumTheNumbers "You " "can!" -- notice that MAXScript doesn't actually care what class of variables you throw at it as long as it can perform the required task

(same thing, MAXScript is also "kind" to the user as to letting them assigning different value classes and performs the operations on them, if possible.)

MEL:
{
   proc int sumTheNumbers(int $num1,int $num2)
   {
      return $num1+$num2;
   }
   print(sumTheNumbers(10, 20)+"\n"); // call the function with some input numbers of choice (from within a print() function in order to see the result)
   print(sumTheNumbers("You ", "can't!")+"\n"); // MEL will automatically convert the input classes to integer class, if possible
}

(notice that I just declared an INTEGER procedure that expects two INTEGER values to be passed to it, so, there's no way you could summarize two floats (i.e.: 10.0 and 20.0) or even strings (i.e.: "You " and "can't!") and expect MEL to return float or string values respectively. What MEL will do is it'll convert the incoming value classes to, in this case, integers, if possible, and then performs the required calculations!

So, there you go, these were basic and simplyfied examples of the different languages. The conclusion could be stated as Python being the superior language to MXS and MEL and its syntax being most conformable to MAXScript (or rather the oposite, MAXScript being very conformable o Python, as MAXscript is younger). There are, of course, much, much deeper differences or similarities that wasn't the purpose of this article to bring up. I'm yet to learn Python more deeply myself, so I can't name more concrete, deeper, differences among the languages. However, if you're facing the same problem (which language to learn) coming from 3ds max, I'd recommend both, MEL and Python. I learnt MEL marginally just to get a glimpse of how it works and how it treats user input and then I went to Python for its much cleaner and easier syntax and much wider possibilities.

21 Comments »

  1. Zdravim,
    za prvy musim rict ze blog mas celkem zajimavy, pro mne hodnotne informace. Kazdopadne ohledne pythonu – delam v maye uz par let a posledni dobou si rikam, ze skriptovani bych taky mel pojmout do samostudia a s pythonem se mi zalibilo, ze je to i dost univerzalni jazyk, pouzitelny nejen v maye. Mohl bys poradit nejakou publikaci, vhodnou pro zacatecnika? Programovani jsem nikdy moc nedelal a v melu jsem se hrabal spis u uplnych trivialit. Na grada.cz jsem nasel jednu knihu, asi si ji obednam. Jen mne napadlo, ze bys mohl neco doporucit z vlastni zkusenosti. Diky

    Comment by Vojtech Lacina — March 11, 2008 @ 16:39

  2. Nejprodavanejsi kniha o Pythonu za rok 2007
    http://www.oreilly.com/catalog/lpython2/index.html

    Comment by Castelis — March 11, 2008 @ 22:09

  3. Vedet o tom, ze existuje i 2nd a dokonce snad i 3rd edition, tak si ji objednam ze zahranici. V CR se da sehnat jen 1st edition, ktera je pomerne stara, nicmene zaklady se z ni daj naucit vyborne a v tomhle to nijak nevadi. Konec koncu, nejlepsi reference je on-line dokumentace k Pythonu samotnymu:

    http://www.python.org/doc/

    Comment by loocas — March 11, 2008 @ 22:35

  4. Dik lidi, uz jsem se na to koukal, takze po vyplate asi obednam. I kdyby jen tu ceskou, tak si myslim, ze pro nauceni zakladu to nebude spatny material. A ty docy k pythonu jsem si stahl a zacnu procitat

    Comment by Vojtech Lacina — March 12, 2008 @ 18:04

  5. Osobne jsem zacinal na tomhle: http://www.freenetpages.co.uk/hp/alan.gauld/czech/index.html (cesky preklad) a pak zbyva jen doplnit novinky z manualu a mas slusny zaklad, pak uz jen zalezi kterym smerem se chces dal orientovat / skripty/ databaze / import/export atd..

    Comment by PetrZ — March 16, 2008 @ 02:35

  6. Ad PetrZ – tuto stranku mi doporucoval kamarad, uz procitam :)

    Comment by Vojtech Lacina — March 17, 2008 @ 15:36

  7. No tak to ma podrz, loocas sa zacal sprtat v mayi ;)
    Zaujimavy clanocek…ja som bol doteraz zamerany na MEL, ale s prichodom Pythonu som sa rozhodol dat mu sancu, najma kvoli pouzitelnosti aj inde.
    Len mi bude asi chvilu trvat preorientovat sa…mam prilis nacitany MEL.
    Kde beries inspiraciu a ako konkretne vyuzivas resp. planujes vyuzivat Python v Mayi…particles? a co expressions? Vies o nejakej stranke venovanej vyuzitelnosti Python-u v Mayi?
    Thx in advance ;)

    Comment by MaxtorAG — March 18, 2008 @ 12:39

  8. Cauves MaxtorAG,

    co se “inspirace” tyce, tak jde hlavne o pracovni nasazeni :) Krom toho Python poskytuje sirsi moznosti vyuziti nez MEL nebo MXS. Ja osobne volam nektery moduly/funkce Pythonu i z Maxe, hlavne kvuli slozitejsim operacim se stringy, XML atd… atd… nebo, kdyz potrebuju napojit na databazi nebo treba FTP ;)

    Ohledne particles, tak s tema jsem v Maye jeste nic nedelal, takze jsem nemel ani duvod na ne nasazovat Python, nicmene v Maye se Python stejne chova jako wrapper, takze je uplne jedno, jestli volam funkce particlu nebo cokoliv jinyho.

    Expressions jsou na tom trosku jinak, tam je o neco pohodlnejsi volat MEL, kvuli jejich integraci, kazdopadne ale i Python se da vyuzit k volani z expressions, ale neni to tak pohodlny a privetivy, krom toho to i pridava par problemu s managementem scriptu atd… atd…

    Kazdopadne Python je strasne jednoduchej, co se syntaxe tyce a je mnohem pouzitelnejsi a pohodlnejsi nez MEL, kterej me osobne moc nevyhovuje, nicmene je nezbytny znat alespon jeho zaklady, bez toho s Pythonem v Maye nic moc nezmuzes.

    Comment by loocas — March 18, 2008 @ 14:36

  9. No, ja vyuzivam MEL cisto na dynamic expressions alebo proceduralnu animaciu + kde/tu nejaky skriptik na automatizaciu ukonov (pri tvorbe scen a tak).
    Zatial som neskriptoval nic zasadnejsie ani ziadne pluginy/nody, ale mozno sa nakoniec pozriem na zubok Pyhtonu, akurat skor casti Maya API Python-u.

    Comment by MaxtorAG — March 20, 2008 @ 11:50

  10. Nice article.
    I do agree that maxscript syntax is nicer I dont agree that mel has more features compared to maxscript. Maxscript is every bit as powerful as mel. Maxscript can do certain things that mel cant and there are few things that mel does better.

    This is coming from a mel user who has only recently dived into maxscript :-)

    Comment by vijay nair — March 25, 2008 @ 11:56

  11. Ked uz zacinas s tou Mayou a pises o MEL, tak sa skus pozriet na moj videonavod:
    http://www.maxtorag.sk/tut/tutorial01.html
    and let me know if it was any good ;)

    Comment by MaxtorAG — March 28, 2008 @ 10:43

  12. Thx MaxtorAG, peknej navod :)

    Z moji, zatim maly, zkusenosti s MELem je Maya a vseobecne MEL o dost jednodussi nez MXS, ne co se syntaxe tyce (to je spis naopak), ale spis co se pouzitelnosti a celkovyho navrhu tyce. Maya je diky cely svoji nodovy strukture velice jednoducha na scriptovani i na celkovou praci. Jen to UI je malinko moc segmentovany na muj vkus a clovek se tak trochu zbytecne naklika nez udela nejakej jednoduchej ukol :D

    Comment by loocas — March 28, 2008 @ 15:53

  13. No jo…
    Ja casto pouzivam mel prikazy miesto naklikavania UI ;) Plus kopec custom shortcuts =)
    A Mayi by mozno prospelo non-overlapping UI, ale co uz. Maya sa v zasade menit nebude, je privela old-timerov, ktori ju pouzivaju a su zvyknuti na to, co bolo. Popravde, po tom, co som vyskusal ten novy prvok Navigation Cube ci jak to nazvali vo verzii 2008, sam som zacal truchlit nad starym dobrym Navigation Compass-om.
    Btw…zachytil som tvoje starosti s Vistou na maxarea.com. Chapem, co prezivas, sam som skoro mesiac mal Vistu, nez som stratil nervy a vratil sa na WinXP 64bit.

    Comment by MaxtorAG — March 30, 2008 @ 02:09

  14. Mimochodom, ak budes skriptovat v Mayi, jednoznacne odporucam jEdit http://www.jedit.org/ a uzitocne pluginy k nemu http://www.highend3d.com/maya/downloads/tools/syntax_scripting/

    Comment by MaxtorAG — March 31, 2008 @ 12:11

  15. Diky, ja k tomu pouzivam SciTE, jelikoz jsem si na nej zvyk z Maxe 2008.

    Comment by loocas — March 31, 2008 @ 14:02

  16. Ak si nahodou prehliadol http://code.google.com/p/pymel/

    Comment by MaxtorAG — April 2, 2008 @ 23:25

  17. Neprehlidnul, neboj ;)

    Comment by loocas — April 3, 2008 @ 11:25

  18. Btw…kedze si spominal, ze s dynamic particle expressions si este nerobil, ak chces, mozes si kuknut nove – zaverecne casti videonavodu, ktore som dnes dal na svoju stranku ;)
    http://www.maxtorag.sk/tut/tutorial01p5.html

    Comment by MaxtorAG — April 6, 2008 @ 15:57

  19. write comments in english pls guys!

    Comment by takezo — January 26, 2009 @ 19:32

  20. takezo: well, I don’t have any problems with having czech comments on my blog.

    Comment by loocas — January 26, 2009 @ 19:56

  21. re:”notice the difference, Python actually supports multiple variable assignments on a single line in order as opposed to MXS”

    What about this feature of MXS:
    local varA = 10, varB, varC = “string” –or just about the same with global …
    As for printing it out, for such tasks I still kind of prefer format:
    format “% % %\n” varA varB varC
    Certainly not as versatile but serves the purpose most of the time.

    Jinak souhlasim, prechod na python z maxscriptu je nadherne plynulej, po kratky nuceny exkurzi do C# to cloveka uplne nadchne :)

    Comment by Swordslayer — February 5, 2010 @ 11:21

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress | Theme by Roy Tanck