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.
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
-
Hello @Dave ,
Welcome to the Plugin Café forum and the Cinema 4D development community, it is great to have you with us!
Getting Started
Before creating your next postings, we would recommend making yourself accustomed with our Forum and Support Guidelines, as they line out details about the Maxon SDK Group support procedures. Of special importance are:
- Support Procedures: Scope of Support: Lines out the things we will do and what we will not do.
- Support Procedures: Confidential Data: Most questions should be accompanied by code but code cannot always be shared publicly. This section explains how to share code confidentially with Maxon.
- Forum Structure and Features: Lines out how the forum works.
- Structure of a Question: Lines out how to ask a good technical question. It is not mandatory to follow this exactly, but you should follow the idea of keeping things short and mentioning your primary question in a clear manner.
About your First Question
Your question is effectively out of scope of support because we do not debug code for users as lined out in our support guidelines. So, you cannot just post your code and then ask us to fix it, this especially applies when things like Chat GPT are involved.
With that being said, there are two (three) major problems with your code:
show_script_list_dialog
: This is pure nonsense regarding creating and running a dialog. Dialogs must be implemented, see here for our dialog examples.run_script
: This does not set the runtime environment, theglobals
, of the script to run. Which will cause many scripts to fail, as script manager scripts are prepopulated withdoc
andop
.- Plugin ID: The plugin ID
1009792
you are using does not seem to be a valid ID, the current plugin ID counter is at1062027
. You can do whatever you want to do locally, but you cannot ship any plugins with such made up IDs.
You also do not really need a full-blown dialog here, you could just get away with
ShowPopupDialog
. Find a simple version below. Please understand that we cannot support ChatGPT nonsense output in future postings of yours.Cheers,
FerdinandResult:
Running the twicehello_doc.py
script with the wrapper scriptrun_scripts.py
. And then for the lulz run the wrapper with the wrapper to run thehello_doc
script.