Modular plugin programming



  • Hi All,

    I'm writing a plugin that imports, processes and exports 3d files (whatever cinema4d supports). It has two way of working:

    1. using parameter -scene, it checks merges all the top level objects and check if there are duplicates, and then checks if the object is already stored in a database
    2. using parameter -object, it just merges everything in the scene (removing cameras lamps etc) and then exports the object

    I wanted to keep the code in modules, storing the first part in a scene_exporter.py and the second one in object_exporter.py and here comes the problems. I don't know if it's because the plugin is in a .pyp file, but the import just doesn't work.
    What should I do to keep the code modular?


  • Global Moderator

    Hello,

    there should be no problem importing *.py files form a *.pyp file. What do you mean with "import just doesn't work."?

    For example: you can have a formulas.py file (next to your *.pyp) that looks like this:

    def SomeCalculation():
    
        return 5
    

    you can import int in your *.pyp just with

    import formulas
    

    and then use

    number = formulas.SomeCalculation()
    

    It gets a little bit more complicated if you want to register plugins in your sub-modules. In that case you would have to hand over the __res__ structure to these modules.

    best wishes,
    Sebastian



  • Let me share you an example.

    This is a simple hello world program.
    In 'hello.pyp' I have (how do I format the text to code?):

    import c4d
    import sys
    import hello_function
    
    def PluginMessage(id, data):
        if id==c4d.C4DPL_COMMANDLINEARGS:
            hello_function.say('Hello World')
            return True
        return False
    

    In 'hello_function.py' I have:

    def say(word):
        print(word)
    

    When I run this from commandline, in the console I get the error that the hello_function package has not being found. They're on the same folder. I don't see any reason why this is not working


  • Global Moderator

    Hello,

    you find information on how to format code in this thread: How to Post Questions.

    best wishes,
    Sebastian



  • Thanks

    Any hint why the hello world code I wrote is not working?


  • Global Moderator

    Hello,

    you might have to add the pyp file path to the system path using:

    sys.path.append(os.path.dirname(__file__))
    

    so

    import sys
    import os
    sys.path.append(os.path.dirname(__file__))
    
    
    import hello_function
    

    best wishes,
    Sebastian