Use pathlib in Maya (Python 2.7) for upcoming Python 3.7

Introduction

In 2020, the timer for python clock has stopped.

However, the CG industry is still using Python 2.7. (Because Maya, 3ds MAX, Houdini, etc. haven't been migrated yet, it can't be helped ...

The DCC tool is still Python2.7, but the standalone Python tool doesn't have to be Python2.7, so I'm using Python3.7.

I'm using Python3.7 and thinking that pathlib is convenient, but when I looked it up, a package was prepared for Python2.7, so I tried to put it in Maya and use it. think.

Installation

If you want to use pathlib with Python 2.7, just do pip install pathlib When using with Maya, install in PYTHONPATH recognized by Maya with the -t flag

In the case of I Create a site-packages folder in% USERPROFILE% \ Documents \ maya \ scripts and install it there. The module file recognizes PYTHONPATH. I prepare a bat for easy installation.

pip.bat


echo off
set PIP_INSTALL_PACKAGE=
set /P PIP_INSTALL_PACKAGE="enter the package to install:"
py -2.7 -m pip install %PIP_INSTALL_PACKAGE% -t site-packages
pause

Try using

Now that the installation is complete, I'll try using it.

import pathlib
pathlib.Path
# Result: <class 'pathlib.Path'> #

I can import it without any problems !!

pathlib.Path(os.getenv("MAYA_LOCATION")).parent
# Result: WindowsPath('C:/Program Files/Autodesk') #

list(pathlib.Path(os.getenv("MAYA_LOCATION")).iterdir())
# Result: [WindowsPath('C:/Program Files/Autodesk/Maya2020/assets'),WindowsPath('C:/Program Files/Autodesk/Maya2020/bin'),WindowsPath('C:/Program Files/Autodesk/Maya2020/brushImages'),WindowsPath('C:/Program Files/Autodesk/Maya2020/brushShapes'),WindowsPath('C:/Program Files/Autodesk/Maya2020/cmake'),WindowsPath('C:/Program Files/Autodesk/Maya2020/devkit'),WindowsPath('C:/Program Files/Autodesk/Maya2020/docs'),WindowsPath('C:/Program Files/Autodesk/Maya2020/Examples'),WindowsPath('C:/Program Files/Autodesk/Maya2020/ExternalWebBrowser'),WindowsPath('C:/Program Files/Autodesk/Maya2020/icons'),WindowsPath('C:/Program Files/Autodesk/Maya2020/include'),WindowsPath('C:/Program Files/Autodesk/Maya2020/lib'),WindowsPath('C:/Program Files/Autodesk/Maya2020/mkspecs'),WindowsPath('C:/Program Files/Autodesk/Maya2020/modules'),WindowsPath('C:/Program Files/Autodesk/Maya2020/plug-ins'),WindowsPath('C:/Program Files/Autodesk/Maya2020/plugins'),WindowsPath('C:/Program Files/Autodesk/Maya2020/presets'),WindowsPath('C:/Program Files/Autodesk/Maya2020/Python'),WindowsPath('C:/Program Files/Autodesk/Maya2020/PYTHON_LICENSE'),WindowsPath('C:/Program Files/Autodesk/Maya2020/PYTHON_README'),WindowsPath('C:/Program Files/Autodesk/Maya2020/qml'),WindowsPath('C:/Program Files/Autodesk/Maya2020/resources'),WindowsPath('C:/Program Files/Autodesk/Maya2020/scripts'),WindowsPath('C:/Program Files/Autodesk/Maya2020/support'),WindowsPath('C:/Program Files/Autodesk/Maya2020/synColor'),WindowsPath('C:/Program Files/Autodesk/Maya2020/translations')] #

You can find the contents of the directory with parent and the contents of the directory with ʻiterdir`.

list(pathlib.Path(os.getenv("MAYA_LOCATION")).glob("Python/DLLs/*.pyd"))
# Result: [WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/bz2.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/pyexpat.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/select.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/unicodedata.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/winsound.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_bsddb.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_ctypes.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_ctypes_test.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_elementtree.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_hashlib.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_msi.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_multiprocessing.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_socket.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_sqlite3.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_ssl.pyd'),WindowsPath('C:/Program Files/Autodesk/Maya2020/python/dlls/_testcapi.pyd')] #

You can also use glob!

in conclusion

Since I was able to use pathlib safely, I will add it to the in-house library and actively use it. It would be nice if it was Python3.7 by the time Maya2021 came out, but I think that the festival of putting in the existing tools will start after the migration, so it's a shame ...

I'll get used to it a little in preparation for the upcoming Python 3.7.

Recommended Posts

Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
Use config.ini in Python
Use dates in Python
mayapy --Python in Maya
Use Valgrind in Python
New in Python 3.4.0 (1)-pathlib
Use profiler in Python
Don't use readlines () in your Python for statement!
Let's use def in python
[For beginners] How to use say command in python!
Use let expression in Python
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Search for strings in Python
Use MongoDB ODM in Python
Techniques for sorting in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
About "for _ in range ():" in python
Check for memory leaks in Python
Check for external commands in python
Use fabric as is in python (fabric3)
How to use SQLite in Python
Next, use Python (Flask) for Heroku!
Use rospy with virtualenv in Python3
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
Use OpenCV with Python 3 in Window
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
Wrap (part of) the AtCoder Library in Cython for use in Python
[Introduction to Python] How to use the in operator in a for statement?
Install Networkx in Python 3.7 environment for use in malware data science books
A memo for those who use Python in Visual Studio (me)
[Introduction to Python] How to use class in Python?
Use print in a Python2 lambda expression
Use DeepL with python (for dissertation translation)
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
Easily use your own functions in Python
Tips for dealing with binaries in Python
Summary of various for statements in Python
Type annotations for Python2 in stub files!
Template for writing batch scripts in python
Easy way to use Wikipedia in Python
[Python] Organizing how to use for statements
Process multiple lists with for in Python
Get a token for conoha in python
Sample for handling eml files in Python
Python interpreter in Maya, Houdini, blender, Nuke
I searched for prime numbers in python
How to use "deque" for Python data
Notes for using python (pydev) in eclipse
Tips for making small tools in python
How to use regular expressions in Python
How to use is and == in Python
Use Resource API rather than Client API in AWS SDK for Python (Boto3)