THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 05/12/2011 at 19:01, xxxxxxxx wrote:
Yes. It is possible to import multiple .py files (your classes) from within your plugins folder.
Here's the jist of it:
-Create a .py file with your class in it.
For this simple example. The external class will be called "MyClass"
-Make your class code look like this:
class MyClass() :
def __init__(self, name = 'Scott') : #The constructor with a required name parameter
self.name = name #Sets up the name parameter to work
-Name the .py file "MyClass.py"
-Put the file in your plugins folder
-At the top of your plugin code. Add these three lines of code:
sys.path.append("C:/Program Files/MAXON/your CINEMA 4D version/plugins/MyPlugin")
from MyClass import MyClass
*Note: Make sure you edit that code to your specific path names!!!!
-Next. In your plugin code. You'll need to instantiate your external class like this:
class Sub(MyClass) : #The parent class this class is connected to is listed in the parentheses
def __init__(self) : #The sub class's constructor
MyClass.__init__(self) #Call to the super class called "MyClass" in the external file
-Now you can call to that external class from withing any of your plugin's methods.
def Execute(self, tag, doc, op, bt, priority, flags) :
s = Sub() #Create an instance of the sub class..Which is also inheriting things from the external MyClass.py file
print s.name #Prints the default name parameter value that exists inside of the MyClass.py file
Be aware that you'll sometimes have to shut down C4D and restart it when adding your external stuff.
Using: print sys.path will let you know whether or not the folder you're targeting is actually set up as a valid path.
I've only done this a couple of times. And I haven't really tested it that much yet. So no guarantees that it's bug free.
But so far it seems to work fine for me.