cosmogonies.net Blog of cosmogonies.net

feedbackhome
6May/121

APyTA_11 : Referencing your work

APyTA_11 : Referencing your work

Referencing is crucial to fully express python usability and re-usability.
import #Record external reference (will execute rooted-code)
as #Allow you to overwrite a namespace(module) path
from #Allow import to access specific classes

Python allow you to create references to scripts, the same way you create external files in reference in Maya.
To do that, the notion of class and objects are extended to:
Modules : The file .py , which contains your code.
Packages : The folder containing the modules (therefore python files), but only if it have a specific file called __init__.py
Theses notions will helps you to setup a full framework hierarchy very easily ! (cf notions of Namespaces in other languages).

When referencing to your external script file, you have to specify the relative path of all your packages (folder), concatenated by '.'
Python gives you four main ways of doing that, here for example creating a polyCube:

import pymel.core.modeling as pmc
pmc.polyCube()
from pymel.core.modeling import polyCube
polyCube()
from pymel.core.modeling import *
polyCube()
polyTorus()
import pymel.core.modeling
pymel.core.modeling.polyCube()

Personally, I recommend the last one, (the uglier I know), for more explanations please refer to my post on Acronyms are evil.
Be precise. And anyway, do NOT use import *, as it really is evil (collapse all paths and is a great risk of overwriting functions or misunderstanding).
Remember the notion of scope in the previous lesson ?
Well, you can also consider a module like a scope too, and the same for the package (containing in the __init__.py, often empty)
__init__.py can store also piece of codes, more often used for putting recurrent import in the modules of your package.

If you want to import again a module (for example after modifying your module), you can use the reload() statement.
But, your old objects, variables, will not be updated (be aware of retro-compatibility glitches).
Sadly I must confess that I advice you to quit Maya (or any context that embed python) when you change your code.
Could be boring, but believe me, you will avoid some issues like that !
When I was alpha-testing videogames, the "teleporting" cheat-tools to avoid hours of gameplay while testing a end of level was strictly forbidden.
I kept this experience in mind, and my advice is to test a feature in REAL condition.
It's boring, it's often a long process, but you will avoid so many bugs and unexpected context if you do not "play the game"
or re-generate each time a user roleplay (and interface interaction).

With the previous snippet, you may have noticed that the path to references scripts are not OS-formatted path, like the windows "C:\MyFolder".
This is because python have some preferences, as starting points to do its searches.
You can print them, and add yours with that lines:

import sys
print sys.path
sys.path.append("YourPackageFullPath")
#Now you can type "import YourModule" (at condition that file was inĀ  YourPackageFullPath\YourModule.py

print sys.modules #Show all modules loaded in RAM (import)

In windows, you can also change that value before python starts using Environment Variable
(From StartMenu, or typing the command "SETX PYTHONPATH YourPackageFullPath")
In Maya, if a module called "usersetup.py" are in sys.path, Maya will execute the code inside
(unless there is also a script called userSetup.mel, which always have priority).
Also, if there is a module called "sitecustomize.py", python will execute its code at startup.

Notice that if you want to execute a module outside of Maya (just with a python commandline), for batching process for example,
You have to execute this line (still in windows): C:\Program Files\Autodesk\Maya2012\bin\mayapy.exe "C:\...\yourFile.py"
But, in yourFile.py, you have to but a specific block of code to tells python what to execute inside:

if __name__ == "__main__":
  print("Launching application")
  #INSERT here what you want to execute when running your command.

Notice that mayapy.exe is the python executable embedded with maya, but can be run separately.
But you can't do the same thing in MotionBuilder for example, and sometimes you will need to have your own independent python on an "empty" client machine.
So you will have to install it from downloading in the website. But be sure (avoid issues) to have the same version (in your Maya for example).
You can know its version by printing the sys.version

Comments (1) Trackbacks (0)
  1. Please Notice that is how you can easily install external packages/modules.
    You have in your python install folder a sub-Folder “site-package”, already in your sys.path.
    In most-cases, installer will self-deploy .py and .pyd files here.


Leave a comment

No trackbacks yet.

report
content