Thursday, January 31, 2013

Using Variables with Maya Commands


import maya.cmds;
sphereNodes = maya.cmds.polySphere(); //create a sphere
sphereShape = sphereNodes[1]; //store the name of the polySphere node ("polySphere1") in another variable.

rad = maya.cmds.polySphere(
sphereShape, q=True, radius=True
); // use variable rad in conjunction with the polySphere command to query radius' value and store in rad
maya.cmds.polySphere(sphereShape, e=True, radius=rad*2); multiply the sphere's radius by 2

rad = maya.cmds.polySphere(
sphereShape, q=True, radius=True
); //reread the radius attribute from the sphere and store it in rad
maya.cmds.polyCube(
width=rad*2, height=rad*2, depth=rad*2
); //create a cube the same size as your sphere

RESULT: 


Variables and DATA

1) you cannot easily intermix different types of variables like
    But you could use str() function to recast contents as a string like this:

2) Python provides a built-in function, type(), which allows you to determine the type of a variable at any point:

3) Convert the variable to a string: contents = str (contents);
4) Python can change variable types on-the-fly, MEL cannot.
5) There have some keywords you cannot use for variable names, you could find all the keywords by execute: import keyword;
             for kw in keyword.kwlist: print(kw);
6) In Python, all data are objects, and each object has:
    Identity: describes its address in memory
    Type (which is itself an object): describes the data type it references
    Value: describes the actual contents of its data
7) When you change the type of the variable, you are not actually altering the data's underlying type, but are point to some different piece of data altogether. You can confirm this fact by using the built-in id() function, which provides the address to the data. 
8) Tuples, strings and numbers are all immutable which means cannot have their values changed (mutated).
    Two immutable variables with the same assignment (eg. data with the same type and value) may in fact be pointing to the same data in memory.
    Two separate assignments to mutable objects with the same type and value are always guaranteed to be different.
9) del(), allows you to delete variables, nit the same as deleting the data referenced by the variable.
10) The none type: var = None;
      One use of this type is to initialize a name without wastefully allocating memory if it is unnecessary.

Stupid Mistake

When I try to basic programming in Maya using Python, There always says error like this:
I recheck every single word, but still cannot find any different between my script and book's script. When I almost give up, I found that I keep using MEL programming. And after I switch to Python, it works:
What a stupid mistake I made! Hope I won't have that mistake again!!!

Wednesday, January 30, 2013

Introduction to Python Commands

1) import maya.cmds;//import a Python module that allows you to use any Maya command available to Python.

2) shorthand flags: n=name, w=width, h=height, d=depth, sx=subdivisionsX, sy= subdivisionsY, sz=subdivisionsZ, as=axis, cuv= createUVs, ch=constructionHistory

3) import maya.cmds;
   print(maya.cmds.help('polyCube'));//then ctrl+enter

4) Three Command Modes:
   Create Mode: import maya.cmds;
                maya.cmds.polyCube();//create cube
 
   Edit Mode: maya.cmds.polyCube('pCube1', edit=True, width=10);//edit it
 
   Query Mode: maya.cmds.polyCube('pCube1', query=Ture. width=True);//pull up information about its current state



Thursday, January 24, 2013

Executing Python in Maya

Command Line: It could be used any time you need to quickly execute a command. The command line will only let you enter one line of code at a time though, which will not do you much good if you want to write a complicated script. To preform more complex operations, you need the Script Editor.


Script Editor: It is an interface for creating short scripts to interact with Maya.Hold right mouse button (RMB) anywhere in the Script Editor window could access the Script Editor's marking menu.


Maya Shelf: Display --> UI Element --> Shelf
              Window --> Setting/Preferences --> Shelf Editor


Tips:
1) Open a new scene: Ctrl+N
2) Press Ctrl+Enter to command script at Script Editor
3) Highlighting a portion of code and then pressing Ctrl+Enter will execute only the highlighted code.
4) Highlighting code before executing it prevents the contents of the Input Panel from emptying out.

Maya's Programming Interface

There have four different programming interfaces to interact with Maya, using three different programming languages. The flowing picture illustrates how these interfaces interact with Maya.


Maya Embedded Language (MEL): MEL scripts fundamentally define and create the Maya GUI. MEL is relatively easy to create, edit and execute, but it is also only used in Maya and has a variety of technical limitations. Namely, MEL has no support for object-oriented programming. MEL can only communicate with Maya through a defined set of interfaces in the Command Engine (or by calling Python).

Python: It can execute the same Maya commands as MEL using Maya's Command Engine. However, Python is also more robust than MEL because it is an object-oriented language.Moreover, Python has an extensive library of built-in features as well as a large community outside of Maya users.

C++ Application Programming Interface: The Maya C++ API is the most flexible way to add features to Maya. However, because of its compilation requirements, the c++ API cannot be used interactively with the Maya user interface. so it can be tedious to test even small bits of code. C++ also has a much steeper learning curve than MEL or Python.


Sunday, January 20, 2013

Introduction

This afternoon, I read the Introduction: Welcome to Maya Python for about 2 hours. Actually, I usually pass the introduction part when reading a book. But after I read this introduction, I found it is quite useful, at least for me.

It firstly explains why we need Python: using Maya's built-in scripting languages to avoid repeating works.Secondly, it compares Python versus MEL to indicate Python is much better than MEL. Thirdly, it explains the syntax of the code examples in this book, including white-space, line breaks, semicolon, quotation marks, comments. Finally, it gives two short code examples. They have same result, but one is wrote by Python and another one is MEL.

 This two pictures are the code and result by using Python version of this script.It only need to run 0.81 seconds in my computer.
 This two are the MEL version of this script. It made me surprised because Maya stopped responding after executing the MEL script. But finally, it shows the result in stead of crush (thank god!). The execution time is 57.52 seconds.
This is the distorted sphere in Maya viewport after I executed (Ctrl+enter) the two scripts. 

Then, the book point out that no artist would want to use a script that takes a few minutes to execute. It would make the tool very difficult for artists to experiment with and most likely it would be abandoned. In the end, the author says you should be able to create scripts that are more complicated than even the example from this chapter by the end of this book, which makes me excited to get started leaning how to use Python in Maya, even I cannot read that code example right now.

Friday, January 18, 2013

Topic & Milestones

1)Selected topic:
   Use Python programming in Maya

2)Reason for selecting the topic:
   I want to pick up my programming skill and learn Maya deeper

3)Research materials:
   Book: Maya Python for games and film

4)Metrics of success:
   Finish reading the book and do at least one sample every week

5)Introduction to your blog:
   Blogger: http://mengxiemaya.blogspot.com/

6)Milestones and the final goal:
   Week  1: Settle down topic and goal

   Week  2: Read Chapter 1,  page 3   to 28  (25)
            Maya Command Engine and User Interface

   Week  3: Read Chapter 2,  page 29  to 62  (33)
            Python Data Basic

   Week  4: Read Chapter 3,  page 63  to 96  (23)
   Week  5: Read Chapter 3,  page 96  to 109 (23)
            Writing Python Programs in Maya

   Week  6: Read Chapter 4,  page 110 to 145 (35)
            Modules

   Week  7: Read Chapter 5,  page 147 to 175 (32)
            Object-Oriented Programming in Maya

   Week  8: Read Chapter 6,  page 175 to 193 (18)
            Principles of Maya Tool Design

   Week  9: Read Chapter 7,  page 193 to 232 (19)
   Week 10: Read Chapter 7,  page 212 to 232 (20)
            Basic Tools with Maya Commands

   Week 11: Read Chapter 8,  page 233 to 258 (25)
            Advanced Graphical User Interfaces with Qt

   Week 12: Read Chapter 9,  page 261 to 284 (23)
            Maya Python API Fundamentals

   Week 13: Read Chapter 10, page 285 to 306 (21)
   Week 14: Read Chapter 10, page 306 to 326 (20)
            Programming a Command

   Week 15: Read Chapter 11, page 327 to 350 (23)
            Data Flow in Maya

   Week 16: Read Chapter 12, page 351 to 375 (24)
            Programming a Dependency Node

   Final goal: Eligible to be a technical artist and could build a professional portfolio.