Here's another example, for when you need to load a module from a specific subfolder in your plugin:
This way is a bit more complex, but the path to the module could be made platform-specific if needed.
On a general note: it is better not to put your plugins in the program bundle, but instead put it in the plugins folder in the configuration folder. The less you mess with the program bundle, the easier things are to restore if you make a mistake. And as a bonus, your plugin will be carried over to future versions of Cura.
- 1
Recommended Posts
ghostkeeper 105
Cura is shipped with its own Python environment (which is CPython 3.5.2). When processing the import command in a Cura plug-in, it will look in three places:
So to add a dependency that's not yet shipped with Cura, you need to supply it in your plug-in folder. There are two types of libraries you can import then: Pure python libraries or compiled binary libraries. I have two examples for this.
Pure python libraries are the easiest. You can just put the source code in the plug-ins folder. To see this in action, you can install the Settings Guide plug-in from the Marketplace. If you then go into Help -> Show configuration folder and browse into the plugins folder you'll find the installed plug-in with simply a folder called "Mistune" with a file "mistune.py". Then in the main plug-in code I can just go "from .Mistune import mistune" and it'll import that mistune.py file. Just like any other Python file you need to use a relative import.
If your library is pre-compiled it becomes harder. If you're just using the library itself it's still easy. You put the .so file (the actual compiled library) in your plug-in's folder and you can import it similar to Mistune above. However if you want to publish the plug-in, you're going to need to import different libraries depending on whether the user is on Windows, MacOS or Linux. For an example of this, you can look at the X3GWriter plug-in that executes a binary depending on the platform. That's done here: https://github.com/Ghostkeeper/X3GWriter/blob/b55d893ca59b1b50becd372adb9a171ec9e5cf77/X3GWriter.py#L97-L105
Instead of an executable as in that plug-in, you'd be doing an "import something_something" depending on the platform. Very similar.
Link to post
Share on other sites
Hanabi-san 1
@ghostkeeper Thank you very much for your quick and detailed answer. I've now added the Python library source code folder to my plugin folder and imported the needed files like you described. It works well for library files that do not import anything from the same library folder. However, e.g. if I want to import a file x.py from the library "abc" and the file x.py includes the import statement "from abc import y", then it does not work anymore. Do you know why this happens and what I can do against it?
Edited by Hanabi-sanLink to post
Share on other sites