Dumb question, I am working on a Python Plugin and I would like to split the code into multiple files. How should I structure my data to avoid having all the .py files show up in the Plugin Manager?
Thanks.
Dumb question, I am working on a Python Plugin and I would like to split the code into multiple files. How should I structure my data to avoid having all the .py files show up in the Plugin Manager?
Thanks.
See if you can add your common files to the path using site.addsitedir() and then just import them. I’m not certain that works because I dont know how much of the python infrastructure is running when plugins are first loaded - but since site is a builtin it seems like a likely candidate
would would be the difference between site.addsitedir(), and just adding a directory with sys.path.append()? I tend to use the sys.path.append() is it cleaner to use site.addsitedir()?
They more or less do the same thing. I think site does some things to avoid duplicates in the path.
so let me know if this is a bad idea…
this is the scheme i’ve got working at the moment (i’m sure there are caveats or gotchas lurking somewhere…):
../mypipeline/plugins
../mypipeline/plugins/myPlugin.py
../mypipeline/plugins/myPlugin/__init__.py
../mypipeline/plugins/myPlugin/myCode.py
../mypipeline/plugins/myPlugin/myMoreCode.py
inside myPlugin.py:
from myPlugin.myCode import myFunction
from myPlugin.myMoreCode import myClass
@rgkovach123 do you got a directory and a python file named myPlugin, might be hard to import with modual and a class with the same name, but i think that is a typo.
@Theodox Thanks so more or less a safer method than directly adding to sys.path
the plugin folder isn’t on the python path - the “myPlugin” module and “myPlugin” package are not known to maya. the plugins folder is on the maya plugin path, and the only file it sees is myPlugin.py which contains all the initiailzing and registering.
for example, it isn’t possible to type:
import myPlugin
in maya’s script editor - maya isn’t aware of it. But you can type:
cmds.loadPlugin(myPlugin)
@Theodox was curious about the difference between site.addsitedir() vs just appending to sys.path, so i looked at the source code, sys.path.append() is more simple, and only adds the specified directory to the pythonpath, vs site.addsitedir() will add the directory, than search it for *.pth files, and recursively add the paths in them.
I’m not a big fan of reinventing the wheel, I would recommend structuring the project following python packaging conventions and let setuptools/distutils handle the paths.
the real problem with all the alternatives to single file plugins is that they introduce a dependency on multiple files - it maks distribuing the plugin much harder unless you completely control the user’s environment. If the user has a nonstandard setup, out of date files, etc you can get problems.
Just for completeness - this is a tougher solution than just messing with paths – you could do this:
1 ) include all the dependencies into zip file,
2 ) append the bytes that make up the zip into a big triple cuoted string at the end of the plugin code
3 ) on oplugin load, write those bytes out to a zip in the users temp directory if there is not already a copty of the zip
4) add the zip to the path
This is a lot of work - obviously - but it does make the plugin completely self contained for cleaner distribution. I’d only recommend it if the plugins go outside a single team / company environment and need to live on their own