Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware 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

    Python script to Python tag hand over?

    Cinema 4D SDK
    2024 python
    2
    3
    631
    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.
    • gaschkaG
      gaschka
      last edited by

      I'm working on a script that creates a python tag with some code.

      Now I want the main script to talk to the python tag script and hand over some information.

      I do it in an incredibly hacky way, and like to know if this is appropriate, or if there is a more elegant/reliable way to do it?

      def add_python_tag(obj, crtl1, crtl2):
          if not obj:
              return
      
          pos_1 = get_global_position(crtl1)
          pos_2 = get_global_position(crtl2)
      
          distance_init = (pos_1 - pos_2).GetLength()
      
          python_tag = c4d.BaseTag(c4d.Tpython)
      
          python_code = """
      
      import c4d
      
      def main():
      
          #some more code ...
      
          pos_1 = get_global_position(ctrl_01)
          pos_2 = get_global_position(ctrl_02)
      
          distance_const = distance_init
      
          distance = (pos_1 - pos_2).GetLength()
          stretch_coefficient = distance_const / distance
      
          # and even more code ... 
      
      
      # Execution
      if __name__ == '__main__':
          main()
      
      """
          
          python_code = python_code.replace("distance_init", str(distance_init))
      
          python_tag[c4d.TPYTHON_CODE] = python_code
          obj.InsertTag(python_tag)
      
          c4d.EventAdd()
      
      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by

        Hi your way of doing is correct if you want to share only constant information.

        However its a bit unpractical, I would recommend you to use f-string instead of the replace, which is slow and can can have side effect since you can't pin-point a specific location in your code template.

        If you want something more dynamic, you will need to write data within the BaseContainer of the object (see Python tag initalization?) In this example this is the tag that write to the BaseContainer, but nothing prevent you in your script from the script manager, to write in the BaseContainer of the tag and in the tag read the value.

        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 1
        • gaschkaG
          gaschka
          last edited by

          Thanks for your help and nudging me into the right direction, I think I can work with that 🙂

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