Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search

Additional files for plug-in dev

Author
Message
  Topic Search Topic Search
Chris Smith View Drop Down
Member
Member
Avatar

Joined: 2011 Jan 20
Location: Santa Monica
Online Status: Offline
Posts: 36
Direct Link To This Post Topic: Additional files for plug-in dev
    Posted: 2011 Dec 05 at 1:36pm
I'm doing a plug-in framework based on ObjectData.  The "framework" I'm making involves quite a bit of extra classes that are really support to whatever the plugs end up being.  That being said, Like in C development I want to keep all those classes in a separate file and import them into the primary plugin file to keep things tidy.  I tried a quick test saving a .pyp file in the same folder as my main pyp and tried to import it.  C4D was not "seeing" that other file.  Is it possible to do this workflow and I skipped something?
Back to Top
tcastudios View Drop Down
Member
Member


Joined: 2005 Jan 21
Location: Sweden
Online Status: Offline
Posts: 559
Direct Link To This Post Posted: 2011 Dec 05 at 3:26pm
The external libs I use (not written by me) I've placed in the Maxon/user area:
/user/library/python/packages/osx folder (or win I suppose for win system)
Back to Top
Chris Smith View Drop Down
Member
Member
Avatar

Joined: 2011 Jan 20
Location: Santa Monica
Online Status: Offline
Posts: 36
Direct Link To This Post Posted: 2011 Dec 05 at 3:38pm
Thanks, Lennart.  I suppose my question should be qualified by saying I want it to be portable with the plug folder to other systems.  I was hoping to have everything contained.
Back to Top
tcastudios View Drop Down
Member
Member


Joined: 2005 Jan 21
Location: Sweden
Online Status: Offline
Posts: 559
Direct Link To This Post Posted: 2011 Dec 05 at 3:52pm
I see. Maybe it's worth trying have your file in your plugin folder and upon start
of Cinema have it check if that file is present in the lib folder and if not have it
copied. I'm not sure of the loading order and a worst case would need a restart
of Cinema (with a Info popup).
I use this way for browser lib files copied by some of my plugins and it works fine.
Back to Top
Chris Smith View Drop Down
Member
Member
Avatar

Joined: 2011 Jan 20
Location: Santa Monica
Online Status: Offline
Posts: 36
Direct Link To This Post Posted: 2011 Dec 05 at 3:56pm
Very interesting.  It's worth a try for sure.  Thanks, Lennart.
Back to Top
ScottA View Drop Down
Member
Member


Joined: 2011 Jan 07
Online Status: Offline
Posts: 2288
Direct Link To This Post Posted: 2011 Dec 05 at 7:01pm
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:
import.os
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.
Example:
    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                  
            
        return c4d.EXECUTIONRESULT_OK



FYI:
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.

-ScottA


Edited by ScottA - 2011 Dec 15 at 5:37pm
Back to Top
Chris Smith View Drop Down
Member
Member
Avatar

Joined: 2011 Jan 20
Location: Santa Monica
Online Status: Offline
Posts: 36
Direct Link To This Post Posted: 2011 Dec 05 at 7:08pm
Wow!  Thanks a ton for that , Scott.  I will try it thusly!
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2011 Dec 06 at 1:30am
Originally posted by ScottA

sys.path.append("C:/Program Files/MAXON/your CINEMA 4D version/plugins/MyPlugin")

Please use this instead, as it is more safe when the user moves you plugin to another folder or he is on another platform, etc.
import os
import sys

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


Cheers,
Niklas


Edited by nux95 - 2011 Dec 08 at 7:31am
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2012 Jan 18 at 2:21pm
@ScottA:
Originally posted by ScottA

-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.
Example:
    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                  
            
        return c4d.EXECUTIONRESULT_OK
Why are you creating a subclass for MyClass? This is neither called "creating an instance" nor it is necessary. Just instantiate MyClass like this:
    def Execute(self, tag, doc, op, bt, priority, flags):
        s = MyClass()
        print s.name

Note: Overriding __init__ for calling the super-classes constructor isn't necessary, too.


Edited by nux95 - 2012 Jan 18 at 2:24pm
Back to Top
ScottA View Drop Down
Member
Member


Joined: 2011 Jan 07
Online Status: Offline
Posts: 2288
Direct Link To This Post Posted: 2012 Jan 18 at 3:47pm
When you see me do something strange. There is usually no good reason for it other than I don't know a better way to do it.
Do German's know what the term "sky hook" means?
I make lots of them.

I'm self taught. And I tend to make up my own rules sometimes.Smile
Like Charles Barkley says: "I am not a role model"Wink

-ScottA


Edited by ScottA - 2012 Jan 18 at 3:51pm
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2012 Jan 18 at 9:39pm
Hm, actually never heard of it.. ;)

So I am. I often learn something from your posts, but this time I taught you. Smile
Cheers,
Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.110 seconds.