how to program plugins with python?
-
Hi @love_me_render ,
Thank you @baca for an extensive answer! Although there's not much to add here, I'll just leave here a couple more comments.
@baca is completely right that our examples on github together with the documentation is the correct place to start with python plugins. Have a look at Plugin Structure Overview and available plugin hooks listed in the Plugins section. In the github repo you'll be mostly interested in the plugins section.
(Almost) the same API as for C++ would be available for you in python plugin as well (e.g. Message() function and others).
You're right the scripts reside in the %APPDATA%\Maxon\Maxon Cinema 4D 2023_BCDB4759\library\scripts. For plugins there're some more options nicely listed by @baca . Another extra option to include you plugin is to pass it with the g_additionalModulePath command line argument, e.g. g_additionalModulePath={YourPath}.
If you changed the plugin registration code of your plugin (e.g. c4d.plugins.RegisterToolPlugin() function or plugin resource, you'd need to restart cinema, there's no other way around. However, @baca is right, you can use "Reload Python Plugins" command to reload "the logic" of your plugin. You can find this command in the Extensions -> Tools -> Reload Python Plugins, or the shortcut button for it is placed in the Console windows on the Scripting layout (check the screenshot below).
The *.pyp files are just normal Python files that contain code for python plugins. I personally prefer using IDE (VSCode) for this. The syntax highlighting is discussed in the thread: Set PYP file syntax highlighting in VS Code?
You need to implement it (likely the same way you have it in the C++ version of your plugin)
Cheers,
Ilia -
Thank you very much for the interesting solutions.
I will try it out.
Klaus -
Hi,
has someone a simple example of a python-plugin-script with "GetVirtualObjects" that creates a cylinder or other, whose radius can be changed by user data?
Thanks, Klaus -
@love_me_render said in how to program plugins with python?:
Hi,
has someone a simple example of a python-plugin-script with "GetVirtualObjects" that creates a cylinder or other, whose radius can be changed by user data?
Thanks, KlausThis should be simple double_circle
-
hi baca, thank you very much for this example.
But when I execute it in the script-manager, also comes the error: "cannot find pyp file"
Or am I thinking completely wrong again?
Do I have to use instead of the plugin-manager a development-environment such as Visual Studio and generate a .pyp file with it?
Cheers, Klaus -
@love_me_render said in how to program plugins with python?:
hi baca, thank you very much for this example.
But when I execute it in the script-manager, also comes the error: "cannot find pyp file"
Or am I thinking completely wrong again?
Do I have to use instead of the plugin-manager a development-environment such as Visual Studio and generate a .pyp file with it?Scripts are run just once.
While plugins works in runtime, and mostly used to dynamically generate/modify some geometry.So it needs be registred as a plugin, and that's happen at Cinema4D startup.
Few messages above there're plenty ways to do that was described.
There are no differences between c++ and python plugins in terms of placement.
Just put python plugin where you'll put c++ plugin. -
thank you again.
But why modifying objects with timeline or userdata works in "python-tag" and "python-generator", but not in "script-manager"? -
@love_me_render said in how to program plugins with python?:
thank you again.
But why modifying objects with timeline or userdata works in "python-tag" and "python-generator", but not in "script-manager"?I don't know the answer, I just deal with it.
If you want that level of in-app editability β go with Python Generator, Python Tag, Python Field, Python Effector, an so on.
Nothing wrong with it.However python plugins in C4D are quite versatile.
So you can develop with IDE, and have python syntax highlight, and C4D API auto-completion.
Moreover for VS Code there's extension to debug plugins.And the penalty is just you have to execute "Reload Python Plugins".
Not a big tradeoff. -
Firstly, thank you very much for the detailed tips!
Now I understand. I need an external developement-environment to compile a Python-Plugin.
The basic idea was to make my tree generator upwards and downwards more compatible. In C++ I would have to write a new plugin for almost every C4D version, but that's difficult because you only ever have one version.
As a C++-programmer for decades, I have fundamental difficulties understanding Python plugins.
I can still manage Python scripts, but I can't get my head round Python plugins.
How do you compile them? Are the C4D on-board tools enough? Or do you need an external Python development-environment? If so, which one is best? Is there a gigantic SDK for it like for C++?
Klaus -
Hi @love_me_render,
It seems like you're a bit overthinking the process.
But it's much simpler.Python plugin executes in runtime, and there's no compilation needed.
It's required thatpyp
file implements a class and registers that class as a plugin (usually in the footer of thepyp
file). That's it.
Also plugin file structure should follow same scheme as c++ plugin, so Cinema would handle resources automatically.Once again β you just need to download any python plugin (or all of them) from the official GitHub, put them into
Cinema4D/plugins
folder (or whatever appropriate place) and start Cinema4D.
Switch to "Script" layout (not necessary, but it's comfortable to see the console).You'll see those plugins in the "Extension" menu -> click on any item, and it should generate an instance in object manager (if it's an object plugin obviously).
Then you can use any text editor to edit corresponding
pyp
file.
Just add some print to the console on Init, or on Update class callback,
save it,
hit "Reload Python Plugins" in Cinema4D,
and create new plugin instance once again
β you'll see message(s) in the python console.And that would be a time to create new subject with more specific questions
As a side note,
Python computation is relatively slow, and it doesn't support multiprocessor execution in Cinema. Also there're some limitations comparable to c++ api.In the defence of c++ plugin development β with new Maxon subscription model, you might assume most of users are on the latest release.
So there might be an option for you to maintain latest release versions only. -
Thank you baca, this is very helpful information.
So I "only" have to write a pyp-text-file and save it in a plugin folder. Brilliant.
But how are the "res" folders and the like created?
I have the last permanent version with R25, therefore I haven't the newest version, I don't want a subscription. -
@love_me_render those are manually created,
luckily all of them also text files.I'm not really familiar with cpp development, thought it's same manual process...
Btw there's plugin structure overview in official doc: developers.maxon.net/docs/py/2024_0_0a/misc/pluginstructure.html
-
This post is deleted! -
Thank you all. Now I have understood
-
Hi,
after several weeks of working on porting my tree generator plugin from C++ to Python, I have now realised that the result in Python is much too slow compared to C++.
Even a single tree is calculated far too slowly. With several clones of trees, the result is completely unusable.
If there is no solution to increase the computing speed of the generated Python plugin by a factor of tens, I will revert to C++.
Is there perhaps a way to compile the Python plugin text file into machine code?My conclusion: Python is well suited for small calculations and objects with a few polygons.
Not for larger, more complex projects.Klaus
-
Hi @love_me_render ,
That's true.
Python is good to operate with several objects PSRs, or to apply built-in commands onto geometry.
But since it's not only slower comparable to c++, but also single-processing β it's not worth to deal with Python for intense computations.If you pursue easier delivery, install and support β check if Scene Nodes suit you.
Those have great performance to build geometries.
That's not a coding, however your logic skills will be required to build nodes setups. -
Hi Klaus,
@baca is again completely right!
Although python has lots of advantages (really fast start for coding, no need in compilation, reloading changes without restarting cinema etc just to mention a couple), however, this doesn't come with no cost. It is quite slow especially in highly loaded parts of code execution (like scene hooks or GetVirtualObjects()).
There's an opportunity to offload expensive parts of your code into C++ library and then call these functions from within your python execution environment. This approach is called Python bindings, you can search more information about it yourself, e.g. here: Python Bindings: Calling C or C++ From Python.
However, this tend to speed things up when you have an expensive task that is executed not too frequently. I'm not aware of what you're doing in your plugin, but it sounds like you have to deal with numerous amount of C4D objects, which drastically slows down your plugin. If that's the case, then I assume the speed up you would gain from the python bindings wouldn't be significant.
Cheers,
Ilia -
Hi,
thank you for the tips.Here is my C++ -treegenerator-plugin, written for R25:
http://www.klausfilm.bplaced.net/TreeGenerator/Website_Treegenerator.html
Cheers
Klaus