PDA

View Full Version : Python classes and module use.


MonteNero
04-13-2009, 03:32 AM
Hello everyone!
I feel pretty comfortable using MEL for my daily needs, but now I decided to make a step forward and start to learn Python. I bought the book "Learning Python" O'Reily third edition, in addition I've watched Digital tutors's "Python scripting " which i find a bit superficial.
I'm a bit confused cause I still can not find any use of classes, modules, dictionaries in Maya.
So please can someone post some simple examples where classes and modules can be useful?
And where can I get more information about Maya python API?

P.S. Really hope I will got the answers to my nooby questions :)

bloggs
04-14-2009, 06:08 AM
ok so it sound like your not much of a programmer nore am i, but basically you need to go through the book and understand what python is all about.

so first off a modal could be thought of as a script. it is a separate programme that you call up to get the function of that seperate script so for example.

"import string" is actaully calling the python script that deals with strings so you'll notice alot of python script have " Import" then the modual. you'll also notice "import mymodual as Mymod".

so im maya we use " import maya.cmds as mc". this is atually calling a maya modual into python. this modual has all the maya python commmands. and by calling this you activate it.
so now i can add a maya command into my script but i need to add the 'mc' before my command(note! you can use anything there instead of 'mc') because remember i did "import maya.cmds as mc" . eg so to use the setAttr comamnd i'd put mc.setAttr(myvarable, myvarable, mayvarable ). python has many modules and there is a list of them on the python.org website.
you can actaul do the importing of your own scripts to. so say i had a script called Myscript.py and all that was in that script was (print "Hello") and had it saved in the same dir. then i could just go "import Myscript" in my shell or maya script editor and it should print "Hello". there is afew python path issues here you need to read about but if you save your scripts in the maya/scripts directory you should be fine.

dictionarys are part of a script and simmilar to a python list. its basically and indexed array read the book to expain in detail. an array is just a group of values in a varable eg myvalue = [1,2,3,4,5,5]. and 'print myvalue[0]' should print 1. in a dictoionay you assign the index and the value so. mydict={'john':10,'bob':40,'steve':60} is my dictionary so if i want the value of 'bob' i need to do "print mydict['bob']" should return 40. this is great for data base stuff.

classes are to do with object oriented programming and its somthing alot of scripting languages in 3d dont have eg mel or maxscript. this is a huge thing and it's just one of the things why python is alot better. basically a class is an object or an instance of a script. read more on this but you can probably ignor this for most of you linner scripts as you won't use classes just get everythng else under your belt.
read this if you want its better than i can explain.
http://en.wikipedia.org/wiki/Class_(computer_science)


anyway have fun its rewarding if you put in the time.

cheers

john

Chadrik
04-14-2009, 03:33 PM
the reason you're having such difficulty knowing where to use classes and dictionaries in maya is becasue maya.cmds itself doesn't really make use of them. this is because maya.cmds is a direct translation of MEL into python.

If you want to completely harness the power of python in Maya you should check out PyMEL (http://www.highend3d.com/maya/downloads/tools/syntax_scripting/Pymel-Python-Module-4844.html).

for example, a dictionary is an object which uses named "keys" to store and retrieve bits of data.


>>> d = {} # create a dictionary
>>> d['name'] = 'Chad' # store a value under a key
>>> 'name' in d # test if an entry exists
True
>>> d.pop('name') # remove an entry

there are several examples of MEL commands that work this way. for example, here is the example from the docs on optionVar:


optionVar -iv "defaultTriangles" 4 -sv "defaultFileName" "buffalo.maya";
optionVar -exists "defaultTriangles";
// Result: 1 //
optionVar -q "defaultFileName";
// Result: buffalo.maya //
optionVar -list;
optionVar -remove defaultTriangles;
optionVar -exists "defaultTriangles";

so pymel utiizes python's powerful classes to solve the problem in a more readable and "pythonic" way. here is the same example rewritten using pymel:


from pymel import *
>>> 'defaultTriangles' in optionVar
True
>>> optionVar['defaultFileName']
'buffalo.maya'
>>> optionVar.keys()
['foo', 'bar', 'spangle' ... ]
>>> optionVar.pop('defaultTriangles')
>>> 'defaultTriangles' in optionVar
False

so, while there's nothing to stop you from using python classes, dictionaries, etc in your own code, don't expect to find them in maya.cmds. For that you'll need PyMEL.

-chad

MonteNero
04-14-2009, 04:30 PM
Hi John and Chad!
Thank you so much for your time and detailed explanations.
Its just I'am looking at Python with the eyes of a MEL user, and MEL is quite straightforward there are now classes, objects, modules.
So I'am trying to get used to the logic of Python, and find ways of application of it in Maya.

MonteNero
04-14-2009, 04:42 PM
I defenetly will take a look at Pymel, more I read it more clear it becomes

CGTalk Moderation
04-14-2009, 04:42 PM
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.