Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    how to program plugins with python?

    Cinema 4D SDK
    r25 python
    3
    20
    4.5k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • love_me_renderL
      love_me_render
      last edited by

      Hello,
      so far I have written some scripts in Python, as "tag", "Python-generator" and also in the "script manager".
      Now I can't get any further.
      Question: how to program plugins with python?
      Example: I want to reprogram my "tree-generator" written in C++ as a plugin with Python and I can't get it to work.
      1.
      One problem in the script-manager is the modification of the generated polygon object or other by user data. After I press "execute" in the script-manager, the script executes but does not respond to any user data afterwards. Is there a solution? Using "message" or something?
      2.
      Next problem:
      The script manager can't save .pyp files, only py. What is the script manager for anyway?
      And the files must all be in the special folder "C:\Users\AppData\Roaming\MAXON\Maxon Cinema 4D R25_1FE0824E\library\scripts" or something like that.
      But these are then py files, so no .pyp plugin files.
      Plugins must, at least from R20 or so, be in a directory that is specified in the program settings.
      But even if you write a .pyp file into this directory, how can you test it while the C4D program is running? Or do you have to restart C4D every time to test it, like with C++?
      3.
      In C++ and also in Python there is a function "GetVirtualObjects()". Do I need it? But if I use it, I have to register the plugin. This doesn't work for me, it always says "pyp-file not found".
      Knows someone advice?
      Thanks,
      Klaus

      bacaB 1 Reply Last reply Reply Quote 0
      • bacaB
        baca @love_me_render
        last edited by

        Hi nightmate @love_me_render

        Question: how to program plugins with python?
        Example: I want to reprogram my "tree-generator" written in C++ as a plugin with Python and I can't get it to work.

        You can start from Python samples from official GitHub
        and documenation.

        One problem in the script-manager is the modification of the generated polygon object or other by user data. After I press "execute" in the script-manager, the script executes but does not respond to any user data afterwards. Is there a solution? Using "message" or something?

        Assume you want to make a polygon generator.
        Prototype can be done as Python Generator, with some User Data as configuration parameters.
        And as a next step you can go with Python plugin.
        It's regular python script file, but extension has to be .pyp rather than .py with a Python class and plugin registration code — look for RegisterObjectPlugin. You'll need to generate unique plugin id
        Just refer to GitHub samples.

        Next problem:
        The script manager can't save .pyp files, only py. What is the script manager for anyway?
        And the files must all be in the special folder "C:\Users\AppData\Roaming\MAXON\Maxon Cinema 4D R25_1FE0824E\library\scripts" or something like that.
        But these are then py files, so no .pyp plugin files.
        Plugins must, at least from R20 or so, be in a directory that is specified in the program settings.
        But even if you write a .pyp file into this directory, how can you test it while the C4D program is running? Or do you have to restart C4D every time to test it, like with C++?

        There're multiple ways to load plugins into Cinema4D:

        1. Put it into Cinema4D installation folder, under C:\Program Files\Maxon Cinema 4D R25\plugins subfolder
        2. Put it into preferences folder, like C:\Users\AppData\Roaming\MAXON\Maxon Cinema 4D R25_1FE0824E\plugins
        3. Just add your plugin folder into Cinema4D preferences at Plugins/Search Paths

        Once plugin is loaded and registred, you'll be able to reload python plugin with Reload Python Plugins command.
        But this would reload logic only, so if you changed description files — you'll neeed to restart Cinema.

        In C++ and also in Python there is a function "GetVirtualObjects()". Do I need it? But if I use it, I have to register the plugin. This doesn't work for me, it always says "pyp-file not found".

        You'll need to implement that method.

        1 Reply Last reply Reply Quote 1
        • i_mazlovI
          i_mazlov
          last edited by i_mazlov

          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

          869ef9cf-609f-4b01-8e00-d8196ee18946-image.png

          MAXON SDK Specialist
          developers.maxon.net

          1 Reply Last reply Reply Quote 1
          • love_me_renderL
            love_me_render
            last edited by

            Thank you very much for the interesting solutions.
            I will try it out.
            Klaus

            1 Reply Last reply Reply Quote 0
            • love_me_renderL
              love_me_render
              last edited by

              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

              bacaB 1 Reply Last reply Reply Quote 0
              • bacaB
                baca @love_me_render
                last edited by

                @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, Klaus

                This should be simple double_circle

                1 Reply Last reply Reply Quote 0
                • love_me_renderL
                  love_me_render
                  last edited by

                  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

                  bacaB 1 Reply Last reply Reply Quote 0
                  • bacaB
                    baca @love_me_render
                    last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • love_me_renderL
                      love_me_render
                      last edited by

                      thank you again.
                      But why modifying objects with timeline or userdata works in "python-tag" and "python-generator", but not in "script-manager"?

                      bacaB 1 Reply Last reply Reply Quote 0
                      • bacaB
                        baca @love_me_render
                        last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • love_me_renderL
                          love_me_render
                          last edited by

                          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

                          1 Reply Last reply Reply Quote 0
                          • bacaB
                            baca
                            last edited by

                            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 that pyp file implements a class and registers that class as a plugin (usually in the footer of the pyp 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.

                            1 Reply Last reply Reply Quote 1
                            • love_me_renderL
                              love_me_render
                              last edited by

                              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.

                              bacaB 1 Reply Last reply Reply Quote 0
                              • bacaB
                                baca @love_me_render
                                last edited by

                                @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

                                1 Reply Last reply Reply Quote 1
                                • love_me_renderL
                                  love_me_render
                                  last edited by

                                  This post is deleted!
                                  1 Reply Last reply Reply Quote 0
                                  • love_me_renderL
                                    love_me_render
                                    last edited by

                                    Thank you all. Now I have understood šŸ™‚

                                    1 Reply Last reply Reply Quote 0
                                    • love_me_renderL
                                      love_me_render
                                      last edited by

                                      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

                                      bacaB i_mazlovI 2 Replies Last reply Reply Quote 0
                                      • bacaB
                                        baca @love_me_render
                                        last edited by

                                        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.

                                        1 Reply Last reply Reply Quote 0
                                        • i_mazlovI
                                          i_mazlov @love_me_render
                                          last edited by i_mazlov

                                          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

                                          MAXON SDK Specialist
                                          developers.maxon.net

                                          1 Reply Last reply Reply Quote 2
                                          • love_me_renderL
                                            love_me_render
                                            last edited by

                                            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

                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post