Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Reuse code from helper class

    Scheduled Pinned Locked Moved PYTHON Development
    3 Posts 0 Posters 1.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      On 11/08/2018 at 08:27, xxxxxxxx wrote:

      Hi

      I'm just learning python for Cinema 4d and have a really beginner question.

      How do I import a helper class from another file?

      ----- Helper.py ---------------

      class Helpers(object) :  
          def \__init\_\_(self, arg) :  
              super(Helpers, self).\__init\_\_()  
        
          @staticmethod  
          def myFunction() :  
              print("My helper function")
      

      ---- Main script.py -------------

      import c4d  
      import Helpers  
        
      def main() :  
          Helpers.myFunction()  
        
      if \__name\_\_=='\__main\_\_':  
          main()
      

      When I try this, I get an error "ImportError: No module named Helpers"

      What am I doing wrong?

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 13/08/2018 at 03:32, xxxxxxxx wrote:

        Hi Fnoller.

        First of all welcome in the plugin cafe community.

        As you may know or not, import function in python looks for folders which are in the python import path and checks if there is a file with this name. 
        By default plugins folder, or the folder where script are saved are not added to this list of paths.
        But there is a package folder where you should put all your python module if you want to have something global which will work for all plugins/scripts.
        Package folder can be found in c4d temp folder

        AppData\Roaming\MAXON\XX.XXX_XXXXX\library\python\packages\win64\yourModule or python file
        

        If you want something more local to your script I suggest you to read this topic Best practice for Imports?.

        I would like to point you, in order to make your post more easy to readable for future reader,s please make sure to use the [ code] [ /code] markdown.

        If you have any question please, let me know!

        Cheers,
        Maxime. 🙂

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 18/08/2018 at 21:22, xxxxxxxx wrote:

          Hi Maxime

          Thank you very much for the answer 🙂

          Fnoller

          1 Reply Last reply Reply Quote 0
          • First post
            Last post