A new version of Cineversity has been launched. This legacy site and its tutorials will remain accessible for a limited transition period

Visit the New Cineversity

Navigation

 ·   Wiki Home
 ·   Categories
 ·   Title List
 ·   Uncategorized Pages
 ·   Random Page
 ·   Recent Changes
 ·   RSS
 ·   Atom
 ·   What Links Here

Search:

 

Create or Find Page:

 

View Python: Including Classes

When creating a CINEMA 4D Python plugin, you can organize your project by placing certain classes in an external file. In this case, you must import the class file and create a subclass instance of the class.

 

First, instatiate the class in a separate file with the same name as the class (MyClass.py in this instance):

 

class MyClass():                          
    def __init__(self, name = 'KeyframeHandler'):   #The constructor with a required name parameter  
        self.name = name                  #Sets up the name parameter to work

 

 

In the main plugin file, add the plugin path and import the class:

 

import os
import sys

__currdir__ = os.path.dirname(__file__)
if __currdir__ not in sys.path:
    sys.path.insert(0, __currdir__)
from MyClass import MyClass

 

Now you can reference the class instance within your plugin methods:

 

    def Execute(self, tag, doc, op, bt, priority, flags):
        s = MyClass()         #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                  
             
        return c4d.EXECUTIONRESULT_OK

 

 

Thanks to ScottA and nux95 for their description of this technique on plugincafe.com

 

Category:Scripting

Categories: