Importing a Class file with Imports
-
Hello,
I'm trying to import a class of helper methods I found on GitHub here:
https://gist.github.com/andreberg/888507#file-cinema-4d-python-helpers-pyI renamed the Python class file PythonHelpers
In the Python script to which I'd like to import the Helpers class, I've added the following lines:
from PythonHelpers import Helpers as ph
When I try using this class like so, I get an error.
ph.select(obj)
Error message:
File "PythonHelpers.py", line 118, in select def select(op): NameError:global name 'c4d' is not defined
I'm importing c4d into my script. I tried adding 'import c4d' to the PythonHelpers file also, but it didn't fix the issue. How do I use this Python Helpers class and get it to recognize the c4d module?
Thank you!
-
Hi @blastframe.
First of all, I would like to point you to Where do I copy external Python libraries ? in our Python documentation.
This is valid if you want to have some global helper that will be available in all your projects.
So you can simply copy your python file in this folder and then import it in Cinema 4D with the usual import file_name.While it's nice I can understand the need to have the file in the same directory folder than your plugin. So for that, I will point you to Best Practise for import and especially the great localimport module from @NiklasR that is mentionned in the Best Practise for Import topic. It will let you import your python stuff in a proper way and also handle python reloading directly.
Finally as a rule of thumbs, if you know that your class may use any call to a module make sure to include this module before to include this class. So always import c4d first and that should work (at least here is working, even doing it with the dirty way of directly modifying sys.path.
I know we are lacking some information on the subject on our documentation, we are working on it.
But if you have any issue, please let me know.
Cheers,
Maxime. -
Hi Maxime,
Thank you for the reply. I will give your suggestions a try.-Kevin