Skip to main content

Basic Scripting in Cinema 4D

By February 6, 2013July 14th, 20203D Modeling, Tutorials

A lot of times when I’m modeling, I find myself using the Split command quite a bit. The Split command enables you to take a selection of polygons from an object and create a separate object based on your selection. A lot of times I end up deleting the old selected polygons from the original object, as it leaves them behind when creating the new object. When you delete polygons in Cinema 4D it leaves behind the points/vertices as well, making you perform an Optimize command to get rid of the stagnant points. With my experience, this ends up being a bit tedious when all I want to do is take one object and split it into two separate objects.

Cinema 4D Split + Delete + Optimize in one step

I did some digging and discovered the Script Manager in Cinema 4D. At first it scared me, as I thought I was going to have to learn how to program/script, and personally that’s just not my thing! Upon finding the Script Manager, I also found the Script Log. The Script Log basically records your actions every time you perform a new command, switch modeling tools, make selections, etc. It’s very similar to the way Macros in Microsoft Word and Actions in Adobe Photoshop work.

Script Log

If you open up the Script Log, found in ExtensionsScript Log…, you’ll see the above dialog. With this dialog open, select the different modeling modes (Model, Points, Edges, Polygons). You’ll notice that each time you select a new mode, it records the action in the script log. With these actions recorded, it’s very easy to create a new script. Myself for example wanted a quick way to split a selection of polygons, delete the old ones, select point mode, select all the points, perform an optimize command, and switch back to model mode. As you can see, this is a lot of steps to go through. As I went through the above steps, and watched the Script Log, it recorded the following:

c4d.CallCommand(14046) # Split
c4d.CallCommand(12109) # Delete
c4d.CallCommand(1011982) # Center Axis to
c4d.CallCommand(12298) # Model

At this point, the only thing you have to do is copy the above code from the Script Log, open the Script Manager found in ExtensionsScript Manager… and paste it into a new script.

Script Manager

The above image shows that I have pasted the code into the manager, created a name for my script (Split + Delete), and inserted a custom icon. You can use a custom icon by going to FileLoad Icon… Icons in Cinema 4D are 64×64 pixels. The icon I used is actually the split icon with a green X through the selected polygon:

Split + Delete

From here, all you have to do is select your object, select your polygons that you want to separate, and execute the script. If you save the script, it will be added to the Customize Commands dialog, where you can then add it to your interface, or create a keyboard shortcut. The Customize Commands dialog is found under WindowCustomizationCustomize Commands. You can also click on the Shortcut button in the Script Manager to quickly open the Customize Commands dialog.

Customize Commands

That’s pretty much all there is to it! It has sure saved me a few extra steps in the modeling process! If you have any questions, feel free to leave your comments below and I will try to answer them as quickly as I can!

Split + Delete + Select Next Object + Center Axis to

After doing a bit of research online, I made a small update to the Split+Delete script. The below code will now select the newly split object in the object manager and center its axis.

import c4d
from c4d import gui
# Welcome to the world of Python

# Main function
def main():
    c4d.CallCommand(14046) # Split
    # Selects Previous object in Object Manager
    ActiveObject = doc.GetActiveObject()
    if ActiveObject == None: return 
    PrevObject = ActiveObject.GetPred()
    if PrevObject == None: return 
    doc.StartUndo()
    doc.AddUndo(c4d.UNDOTYPE_BITS, ActiveObject)
    ActiveObject.DelBit(c4d.BIT_ACTIVE)
    doc.AddUndo(c4d.UNDOTYPE_BITS, PrevObject)
    PrevObject.SetBit(c4d.BIT_ACTIVE)
    doc.EndUndo()
    
    c4d.CallCommand(12109) # Delete split polygon(s)
    
    # Selects Next object in Object Manager
    ActiveObject = doc.GetActiveObject()
    if ActiveObject == None: return 
    NextObject = ActiveObject.GetNext() 
    if NextObject == None: return 
    doc.StartUndo() 
    doc.AddUndo(c4d.UNDOTYPE_BITS, ActiveObject) 
    ActiveObject.DelBit(c4d.BIT_ACTIVE) 
    doc.AddUndo(c4d.UNDOTYPE_BITS, NextObject)
    NextObject.SetBit(c4d.BIT_ACTIVE) 
    doc.EndUndo() 

    c4d.CallCommand(1011982) # Center Axis to
    c4d.CallCommand(12298) # Model Mode
    
    c4d.EventAdd()

# Execute main()
if __name__=='__main__':
    main()

15 Comments

  • Shir says:

    Hey I’m trying to link a button to a custom Spline I created, I would like to click the button and have the object load into my project straight away

    How can I do this?

    • Hey man, haha it’s actually been a while since I’ve messed around with the macro stuff in C4D. So you want a click a button and have a custom spline load into your scene?

      • Shir says:

        Yeah exactly! I’ve made custom splines already and i can drop them in from my presets folder but i want to just click a button like you do with c4d base splines, any ideas?

        • So I did some research and figured it out!
          Open the Content Browser. Go to File → New Preset Library…
          I named mine matthew. Then just drag your object you want into that library. I just used spline for the name.
          Then I made a custom script (and by made, I mean I found bits and pieces of other scripts I found online and made it work lol). Open the Script Manager and import this script: Import from Content Browser.

          The two lines that have “preset://matthew.lib4d/spline”… change that to the name of your preset library and the name of the object you dragged into the Content Browser.

          When you hit Execute, it should drop it into your scene! Then as outlined in the main tutorial, you can use the Customize Commands window in C4D to drag this in as a button into the C4D interface for one click operation. This should do it! Let me know if you run into any problems!

        • shir says:

          Oh Genious, I thought it was something like that but i wasn’t sure how to piece the code together, I found that mergedocuments script on the maxonSDK and knew it was something along those lines!

          Champion, no I can generate custom rig controls from my UI

        • You’re very welcome man! That’s what this community is all about! Glad I could help 🙂

  • Rafael says:

    Hi. Great info. One question though, when I open the script manager, there is a script already loaded in there. How do I create a new one and clear what´s already there? Thanks

    • Hey Rafael, they may have changed this somewhat from when I first wrote this haha. If you open the Script Manager and make sure you’re on the COFFEE tab, just click File > New. Then if you select the script that’s already there, you should be able to delete it, then add your own in. Let me know if you still have issues!

      Thanks,
      Matt

  • jonty says:

    Thank you very much for the help!!!

  • Rich Hallsworth says:

    Thanks for sharing this man, just what I was looking for.

  • marc says:

    This makes me want to throw up…. I can puke better macros then this!

Leave a Reply

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.