How to add "scripts" to a menu?
-
Hi,
is there a way to insert scripts (*.py) into a menu ?by the way there are still spelling errors in the docu "MENURESOURCE_SEPARATOR vs MENURESOURCE_SEPERATOR"
https://developers.maxon.net/docs/py/2024_3_0/consts/MENURESOURCE.html?highlight=menuresource_separatorkind regards
mogh -
Hey @mogh,
Thank you for reaching out to us. Please open a new thread for questions that do not exactly match an existing thread, I have forked your question.
Thank you for pointing out the issue in the documentation, I have fixed it and the other occurrence in Plugin Structure. Regarding your question, it depends a bit on what you mean with "script". I assume you are talking about a Script Manager script? But long story short, to add something to a menu it must be a command (or a submenu). A random Python file on disk does not satisfy this condition. But the Script Manager creates commands for all its scripts. You can look them up using the Command Manager:
The problem with this is that the Script Manager script command IDs are assigned dynamically and might change on restart when you add new scripts (the IDs are assigned to the scripts in alpha-numerically order on startup). But there is c4d.GetDynamicScriptID with which you can figure out the ID of a script. @m_adam once wrote this code example where he demonstrated the usage of the function and the other Script Manager related functions.
With such IDs you could then populate a menu using the
f"PLUGIN_CMD_{value}"
pattern shown in the other thread. I would however be careful withc4d.C4DPL_BUILDMENU
, there is a good chance that Script Manager script command IDs have not yet been established at this point. So, you might have to inject your menu at a later point. I did not test that, this is just a hunchWhen you just want to run your own Python scripts which are not in the search path of the Script Manager, you will have to write your own CommandData wrappers to insert them into menus.
Cheers,
Ferdinand -
My intention was not to highjack but add to the topic - anyway back to the topic ...
Thanks for your quick summary of the pitfalls of this problem. While it is good to know that scripts get dynamical IDs, in my opinion it might juts be more reasonable to create small plugins for each script to insert them Properly.
@ferdinand said in How to add "scripts" into a menu?:
this is just a hunch
the joke's on me I guess
cheers mogh
-
Hey @mogh,
I know that you did not meant any harm and the word hijack was a bit strong - which is why reworded that before you answered. But topics should remain focused on a singular thing, the specific question of the original poster, rather than being a collection of questions from multiple people which roughly align with whatever broader topic they see embodied in a thread.
Cheers,
Ferdinand