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

    Importing a Class file with Imports

    Cinema 4D SDK
    2
    3
    629
    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.
    • ?
      A Former User
      last edited by

      Hello,
      I'm trying to import a class of helper methods I found on GitHub here:
      https://gist.github.com/andreberg/888507#file-cinema-4d-python-helpers-py

      I renamed the Python class file PythonHelpers

      In the Python script to which I'd like to import the Helpers class, I've added the following lines:

      from PythonHelpers import Helpers as ph
      

      When I try using this class like so, I get an error.

      ph.select(obj)
      

      Error message:

        File "PythonHelpers.py", line 118, in select
      def select(op):
      NameError:global name 'c4d' is not defined
      

      I'm importing c4d into my script. I tried adding 'import c4d' to the PythonHelpers file also, but it didn't fix the issue. How do I use this Python Helpers class and get it to recognize the c4d module?

      Thank you!

      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by

        Hi @blastframe.

        First of all, I would like to point you to Where do I copy external Python libraries ? in our Python documentation.
        This is valid if you want to have some global helper that will be available in all your projects.
        So you can simply copy your python file in this folder and then import it in Cinema 4D with the usual import file_name.

        While it's nice I can understand the need to have the file in the same directory folder than your plugin. So for that, I will point you to Best Practise for import and especially the great localimport module from @NiklasR that is mentionned in the Best Practise for Import topic. It will let you import your python stuff in a proper way and also handle python reloading directly.

        Finally as a rule of thumbs, if you know that your class may use any call to a module make sure to include this module before to include this class. So always import c4d first and that should work (at least here is working, even doing it with the dirty way of directly modifying sys.path.

        I know we are lacking some information on the subject on our documentation, we are working on it.
        But if you have any issue, please let me know.
        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 3
        • ?
          A Former User
          last edited by

          Hi Maxime,
          Thank you for the reply. I will give your suggestions a try. 🙂

          -Kevin

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