1

Can anyone describe exactly how someone can create and execute a python zip package in Maya? A lot of tutorials and questions regarding this jump into the middle and assume certain knowledge. I need a simple example as a starting point.

Folder/
    scriptA.py
    scriptB.py
    scriptC.py

ScriptA:
    Import ScriptB
    Import ScriptC

Create zip of Folder

In Maya
    Code to run Folder as if not zipped
    ScriptA.foo()

In folder we have three scripts. ScriptA references the other two. I zip the folder with a program like winrar. In maya, I want to load from this zip as if the files inside were any other module sitting in my script folder (without unzipping preferably). How do I do this?

1 Answers1

1

Any zip on your python path is treated like a folder, so:

import sys
sys.path.append('path/to/archive.zip')

import thingInZip
thingInZip.do_something()

The only issue is depth: the zipImporter does not expect nested directory structures. So this is OK:

 ziparchive.zip
   +--- module1.py
   +----module2.py
   +----package_folder
     |
     +-- __init.__py
     +-- submodule1.py
     +-- submodule2.py
     +--- subpackage
       |
       +- __init__.py

But this is not:

ziparchive.zip
 + --- folder
    +- just_a_python_file_not_part_of_a_package.py

also the site module can't add paths inside a zip. There's a workaround here. You will also probably need to be careful about the order of your sys.path: you want to make sure you know if you are working from the zipped one or from loose files on your disk.

You can save space by zipping only the .pyc files instead of the whole thing, btw.

PS beware of using left slashes in sys.path.append : they have to be escaped \\ -- right slashes work on both windows and *ix

theodox
  • 12,028
  • 3
  • 23
  • 36
  • Thought this worked, but maya was grabbing the pyc file. After I deleted the pyc, it wouldn't load. I have a module called Timeline inside, and the function I want to call is timeliner. If I do the above and try Timeline.timeliner(), I get No module named Timeline. – Colin Knueppel Sep 17 '15 at 21:52
  • If you were seeing the pyc, it probably means your work files (outside the zip) were on the path. You need to make sure your zip has what you need and is inserted into sys.path as above. You can make sure the zip comes before loose files with 'sys.path.insert(0, 'path/to/zip.zip') – theodox Sep 17 '15 at 22:00
  • This is Maya 2011+ yes? Not older? – theodox Sep 17 '15 at 22:00
  • 2014 yes. Pyc was preexisting from a previous compile, and I overlooked it when first attempting this method. The script appeared to work, but when I updated my scripts, nothing changed, and that's when I realized it was there. I removed that and now trying through the zip, but not having luck. When you say sys.path, what do you mean? I was putting the zip into my documents/maya/scripts/ folder, where I was previously running scripts. I'm guessing then that there's a specific folder these zips need to go to be sys.path? – Colin Knueppel Sep 17 '15 at 22:29
  • see the example code: import the `sys` module and add the full path to your zip file to the variable `sys.path`, which is a list – theodox Sep 17 '15 at 23:59
  • Okay. That's what I did, then. I'm not certain why that's not working. I tried with the files directly zipped and with the files inside a python folder. I'm going to have to try making a simpler package to see if I can get this just to work. Does it matter what program you use to create the zip? – Colin Knueppel Sep 18 '15 at 14:25
  • It has has to be a .zip style LZW compression - I don't think it will work with .rar or .tar files – theodox Sep 18 '15 at 16:48
  • Got this to work using the windows right-click > Send To / Compression option over selected files. – Colin Knueppel Nov 03 '15 at 23:43
  • Could you add a note to your answer for future viewers that mentions you have to have use forward / not back \ in the sys.path.append? I wasted some time getting weird results, and it turns out that copying the path from Windows path field puts back slashes in the path, which don't work. Weirdly you can mix them, like c:\folder/file.zip works, but if you have back slash before the zip name, like c:\folder\file.zip, it does not work. – Colin Knueppel Jan 22 '16 at 15:09