How to Convert a Cinema 4D Script to a CommandData Plugin - nrosenstein-c4d/c4d-prototype-converter GitHub Wiki

How to Convert a Script to a CommandData plugin

This process should work with any script. For our purposes, we'll create one from scratch.

Start with a Saved Script

  1. In Cinema 4D go to Script > Script Manager
  2. In the Script manager go to File > New
  3. A new script will appear:
  4. Add a DocString to the top of the script, and remove the #Welcome to... line.
  5. Save the script as hello-world.py by choosing File > Save...
"""Name-US: Hello World
Description-US: Simple plugin that opens a dialog.
"""
  • Name-US will become your plugin's name by default.
  • Description-US will become your plugin's help text by default.

HelloWorld.py

"""Name-US: Hello World
Description-US: Simple plugin that opens a dialog.
"""

import c4d
from c4d import gui

def main():
    gui.MessageDialog('Hello World!')

if __name__=='__main__':
    main()

Convert the Script

  1. Open the Script Converter by going to Plugins > Script Converter
  • The Script Converter dialog will open
  1. The Script * pull-down is auto-populated with all of your scripts. Select hello-world.py from the list.

  • Note that a Plugin Name and Plugin Help have been auto-filled with the info from your DocString.
  1. Every C4D plugin needs a unique ID, click on the Get Plugin ID button. This will take you to Maxon's Developer page. If you're logged in, you should see this. If you aren't, you'll need to log in (and possibly create an account if you don't have one yet.)
  2. Enter Hello World as your Working Plugin Title and press Get Plugin ID
  3. Double-click on the plugin ID to select it. Copy & Paste it into the Plugin ID * field in Script Converter.
  • If you want, you can further customize this plugin by selecting a custom icon or choose to save your plugin somewhere other than your C4D Prefs folder.
  1. Press Create. Your plugin will be generated and a file browser should open to its location.

  2. Congratulations, you've successfully converted your plugin! If you restart Cinema 4D, you'll see it in your Plugins pull-down.

  3. You can stop here. But we recommend that you...

Customize Your Plugin

  1. Open hello-world.pyp and complete the #TODO: items.
# Copyright (c) <year> <author>
"""Name-US: Hello World
Description-US: Simple plugin that opens a dialog.
"""

# TODO: Remove redundant `if __name__ == '__main__':` check if it was in your script
# TODO: Remove redundant imports
# TODO: Update Copyright information
# TODO: Add a README file
# TODO: Keep in mind that the variables `doc` and `op` are no longer globally available

import c4d
import os


def load_bitmap(path):
    path = os.path.join(os.path.dirname(__file__), path)
    bmp = c4d.bitmaps.BaseBitmap()
    if bmp.InitWith(path)[0] != c4d.IMAGERESULT_OK:
        bmp = None
    return bmp


import c4d
from c4d import gui



class HelloWorld(c4d.plugins.CommandData):

    PLUGIN_ID = 1040748
    PLUGIN_NAME = 'Hello World'
    PLUGIN_INFO = 0
    PLUGIN_ICON = load_bitmap('res/icons/hello-world.tiff')
    PLUGIN_HELP = 'Simple plugin that opens a dialog.'

    def Register(self):
        return c4d.plugins.RegisterCommandPlugin(
            self.PLUGIN_ID, self.PLUGIN_NAME, self.PLUGIN_INFO, self.PLUGIN_ICON,
            self.PLUGIN_HELP, self)

    def Execute(self, doc):
        gui.MessageDialog('Hello World!')

        return True


if __name__ == '__main__':
    HelloWorld().Register()

The file should more-or-less look like this when you're done:

"""Name-US: Hello World
Description-US: Simple plugin that opens a dialog.

Copyright (c) 2018 Donovan Keith
"""

import c4d
from c4d import gui
import os

def load_bitmap(path):
    path = os.path.join(os.path.dirname(__file__), path)
    bmp = c4d.bitmaps.BaseBitmap()
    if bmp.InitWith(path)[0] != c4d.IMAGERESULT_OK:
        bmp = None
    return bmp

class HelloWorld(c4d.plugins.CommandData):

    PLUGIN_ID = 1040748
    PLUGIN_NAME = 'Hello World'
    PLUGIN_INFO = 0
    PLUGIN_ICON = load_bitmap('res/icons/hello-world.tiff')
    PLUGIN_HELP = 'Simple plugin that opens a dialog.'

    def Register(self):
        return c4d.plugins.RegisterCommandPlugin(
            self.PLUGIN_ID, self.PLUGIN_NAME, self.PLUGIN_INFO, self.PLUGIN_ICON,
            self.PLUGIN_HELP, self)

    def Execute(self, doc):
        gui.MessageDialog('Hello World!')

        return True

if __name__ == '__main__':
    HelloWorld().Register()
⚠️ **GitHub.com Fallback** ⚠️