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.
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