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
    1. Maxon Developers Forum
    2. Dave
    D
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 1
    • Best 0
    • Controversial 0
    • Groups 0

    Dave

    @Dave

    0
    Reputation
    3
    Profile views
    1
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Dave Unfollow Follow

    Latest posts made by Dave

    • Simple plugin to dynamically list python scripts

      Hi there,

      I'm trying to make my first ever simple plugin that will just list a collection of Python Scripts I've made over the years (for easy sharing as a plugin).

      With a little help from Chat GPT I've managed to get the plugin to show inside Cinema 4D Extensions but it fails to show the icon or list the scripts.

      Here's an image showing my file structure with all my python scripts in the 'res' folder.

      b39c92c1-321e-45f6-bfa4-62e31ca16e47-image.png

      And here's my code:

      import c4d
      import os
      import importlib
      import c4d.bitmaps
      
      class TestPlugin(c4d.plugins.CommandData):
          def Execute(self, doc):
              # Display a dialog with a list of available scripts
              selected_script = self.show_script_list_dialog()
      
              # Execute the selected script
              if selected_script:
                  self.run_script(selected_script)
      
              return True
      
          def show_script_list_dialog(self):
              # Get the path to the 'res' folder within the plugin directory
              plugin_folder = os.path.dirname(__file__)
              res_folder = os.path.join(plugin_folder, 'res')
      
              # List all Python scripts in the 'res' folder
              script_files = [f for f in os.listdir(res_folder) if f.endswith('.py')]
      
              # Create a dialog to display the script list
              dlg = c4d.gui.GeDialog()
              dlg.SetTitle('Select Script to Run')
      
              for script_file in script_files:
                  dlg.AddChild(1000, c4d.DTYPE_STATIC, '')  # Spacer
                  dlg.AddChild(1001, c4d.CID_FILEBUTTON, os.path.splitext(script_file)[0])
      
              # Show the dialog
              dlg.Open(c4d.DLG_TYPE_ASYNC, 1039792, -1, -1, 400, 150)
      
              # Handle dialog events
              result = dlg.DoWhile(c4d.DLGRESULT_OK)
              if result:
                  return os.path.join(res_folder, dlg.GetFilename(1001) + '.py')
              else:
                  return None
      
          def run_script(self, script_path):
              # Load and execute the selected script
              script_name, script_extension = os.path.splitext(os.path.basename(script_path))
              script_module = importlib.load_source(script_name, script_path)
              script_module.main()
      
      plugin_folder = os.path.dirname(__file__)  # Define the plugin folder variable
      
      if __name__ == "__main__":
          plugin = TestPlugin()  # Change the plugin name to TestPlugin
      
          icon_path = os.path.join(plugin_folder, "icon.png")
          icon_bitmap = c4d.bitmaps.BaseBitmap(icon_path)
      
          c4d.plugins.RegisterCommandPlugin(
              id=1009792,  # Use the specified ID
              str="TestPlugin",  # Change the plugin name to TestPlugin
              info=0,
              dat=plugin,
              icon=icon_bitmap,  # Pass the BaseBitmap object
              help="This is a description of the TestPlugin"  # Provide a description for your plugin
          )
      
      

      Any idea where I'm going wrong?

      Cheers

      posted in Cinema 4D SDK python
      D
      Dave