Cinema 4D - Python scripts

From NoskeWiki
Jump to: navigation, search

About

NOTE: This page is a daughter page of: Cinema 4D


This page is to help you get started creating your own python scripts and plugins for Cinema 4D (C4D). Python was added to Cinema 4D in R11.5 and is the way to go - better than COFFEE and... unlike C++, doesn't require your own compile and reloading of Cinema 4D each time.


All My Examples


Quickstart Example: "Simple Object Renamer" Script

Below is a simple but useful python script which has its own dialog and lets you rename all selected objects in the Object window.

The easiest way to test and get it working:

  • Go: Script > Script Manager then under the "Python" tab click File > New.
  • Copy and paste the script below.
  • Go: File > Save, and call it "simple_object_renamer.py"
  • Click Execute (for this particular script you should create and select a few cubes first)
  • To see console output (error messages and print statements), click: Script > Console
  • To execute the saved version of the script (without needing the Script Manager) click: Script > User > simple_object_renamer


simple_object_renamer.py

# Simple script for doing a Find/Replace over all selected object names.
 
import c4d
from c4d import gui
 
# Unique id numbers for each of the GUI elements
LBL_INFO1 = 1000
LBL_INFO2 = 1001
TXT_FIND = 10001
TXT_REPLACE = 10002
GROUP_OPTIONS = 20000
BTN_OK = 20001
BTN_CANCEL = 20002
 
# Dialog for renaming objects
class OptionsDialog(gui.GeDialog):
  def CreateLayout(self):
    self.SetTitle('Simple Object Renamer')
    self.AddStaticText(LBL_INFO1, c4d.BFH_LEFT, name='Find:') 
    self.AddEditText(TXT_FIND, c4d.BFH_SCALEFIT)
    self.SetString(TXT_FIND, 'Cube')  # Default 'find' string.
    self.AddStaticText(LBL_INFO2, c4d.BFH_LEFT, name='Replace with:') 
    self.AddEditText(TXT_REPLACE, c4d.BFH_SCALEFIT)
    self.SetString(TXT_REPLACE, 'Square')  # Default 'replace' string.
    # Buttons - an Ok and Cancel button:
    self.GroupBegin(GROUP_OPTIONS, c4d.BFH_CENTER, 2, 1)
    self.AddButton(BTN_OK, c4d.BFH_SCALE, name='OK')
    self.AddButton(BTN_CANCEL, c4d.BFH_SCALE, name='Cancel')
    self.GroupEnd()
    self.ok = False
    return True
 
  # React to user's input:
  def Command(self, id, msg):
    if id==BTN_CANCEL:
      self.Close()
    elif id==BTN_OK:
      self.ok = True
      self.option_find_string = self.GetString(TXT_FIND)
      self.option_replace_string = self.GetString(TXT_REPLACE)
      self.Close()
    return True
 
#This is where the action happens
def main():
  # Get the selected objects, including children.
  selection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
 
  if len(selection) <= 0:
    gui.MessageDialog('Must select objects!')
    return
 
  # Open the options dialogue to let users choose their options.
  dlg = OptionsDialog()
  dlg.Open(c4d.DLG_TYPE_MODAL, defaultw=300, defaulth=50)
  if not dlg.ok:
    return
 
  doc.StartUndo()  # Start undo block.
  num_renamed = 0
  for i in range(0,len(selection)):
    sel = selection[i]
    new_name = sel.GetName().replace(
        dlg.option_find_string, dlg.option_replace_string, 1)
    if (sel.GetName() != new_name):
      # NOTE: to see print output open: menubar > Script > Console window.
      print ' - ' + sel.GetName() + ' > ' + new_name
      doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, sel)
      sel.SetName(new_name)
      num_renamed += 1
  doc.EndUndo()   # End undo block.
  c4d.EventAdd()  # Update C4D to see changes.
  gui.MessageDialog(str(num_renamed) + ' of ' + str(len(selection)) +
                    ' objects renamed')
 
if __name__=='__main__':
  main()

Note: For a slightly fancier find/replace dialog see Cinema 4D - my Python script - find replace object names

The STK

Recording Actions with "Script Log"

Instead of SDK and Google search the right Python commands, try opening the menubar > Script > Script Log. Once open, any button you click, command or change you apply will appear in this log in Python - think of it as a "Python Record Action" history. Then you just have to work out which recorded lines to copy and paste directly to your own Scripts. It's brilliant!


Tutorials for Creating Python Plugins in Cinema 4D


Other Pages

  • Cinema 4D - C++ plugins - here (unlike this page) I've actually written a fairly good tutorial on using the C4D C++ API.
  • Cinema 4D - COFFEE - COFFEE is Cinema4D's own scripting language which has a much smaller learning curve than the C++, and lets you attach code to objects in your scene for dynamic simulations.


Links

  • Py4D videos - has some nice videos showing what's possible with Python in Cinema 4D (for R11.5).
  • Plugin Cafe - search the official discussion forum
  • Cinema 4D cafe - C4D users community and tutorials page