How to use c4dpy for Cinema4D R20 SP1



  • Hi,
    Apologies if this is painfully obvious and i'm missing something, but...

    Could you please let me know how to use the new c4dpy for Cinema4D R20 SP1?
    the IDE I will be using is JetBrains - PyCharm.

    Thanks for the help.


  • Global Moderator

    Hi,

    in fact we have to apologize. It is not painfully obvious. The documentation for c4dpy is currently lacking in the Python SDK documentation. It's being worked on and will follow very soon. Sorry!

    Cheers,
    Andreas



  • Thanks for the quick response.

    Ok ill keep my eye on the forum and announcements.

    Look forward to using it!!

    Alfie



  • Okay, I can't promise this is the correct way to do this, but it seems to have worked for me:

    Install C4D Py

    1. Ensure that you have Cinema 4D R20+ installed, I don't think this will work without it.
    2. In your C4D install directory, unzip the sdk.zip file. (Note: this step might not be strictly necessary, not sure.)
    3. Download the latest c4dpy, ensure that it corresponds with your version of C4D.
      • You may need to download the latest C4D Service Pack via the Online Updater (C4D > Help > Check for Updates...)
    4. Unzip the c4dpy .zip file and copy the contents into your C4D install directory. C4dpy seems to rely on your C4D install's library files and will spit out errors if you don't do this.
      0_1536168428071_2de2a374-802d-4356-900b-bf6376bd4300-image.png

    Verify the Installation

    Mac

    Note: I haven't tested on mac, so I'm not sure this works.

    1. Cmd + Space then Terminal
    2. Drag the c4dpy.app file into the terminal - you should get the path to the file.

    PC

    1. Open terminal on Mac or Command Prompt on PC
    2. Drag the c4dpy.exe file into the Command Prompt

    Both

    1. Press enter/return
    2. You should see something like:
    C:\Users\donovan>"C:\Program Files\MAXON\Cinema 4D R20\c4dpy.exe"
    Welcome to the world of C4D and Python 2.7.14 (default, May  3 2018, 18:05:57) [MSC v.1500 64 bit (AMD64)] (c) 2018
    

    Setup your IDE

    Instructions will vary a bit on a per IDE basis.

    Create a test script (All IDEs)

    1. Open C4D
    2. Script > Script Manager
    3. Script Manager > File > New.... You should see something like:
    import c4d
    from c4d import gui
    # Welcome to the world of Python
    
    
    # Script state in the menu or the command palette
    # Return True or c4d.CMD_ENABLED to enable, False or 0 to disable
    # Alternatively return c4d.CMD_ENABLED|c4d.CMD_VALUE to enable and check/mark
    #def state():
    #    return True
    
    # Main function
    def main():
        gui.MessageDialog('Hello World!')
    
    # Execute main()
    if __name__=='__main__':
        main()
    
    1. Script Manager > File > Save...
    2. Save the script as helloworld.py.
    3. Open your scripts folder: C4D > Script > User Scripts > Script Folder...
    4. Open the helloworld.py in your IDE.

    PyCharm

    0_1536094384719_8cdcbe43-0e13-4d8c-9de7-98521145bbc0-image.png

    Configure your Intepreter

    Additional C4D Py Prep

    1. PyCharm requires that Python Interpreters be named python.exe so rename the c4dpy.exe file in your C4D Directory to python.exe. (Note: I'm not sure if this breaks any of C4D Python's functionality).

    In PyCharm

    1. File > Settings > Project > Interpreter
      0_1536094776230_769af828-0d7a-4753-beaa-94301fe0e2eb-image.png
    2. Click on the Project Interpreter dropdown (probably says Python 2.7) and select "Show All".
      0_1536094837673_40027c3a-638a-4a41-bc6c-132bcecd46bc-image.png
    3. Click on the + to add a new interpreter.
    4. Click on System Interpreter
    5. Browse to the location of the renamed c4dpy.exe (now called python.exe).
    6. Choose OK. It will probably take a minute for the Interpreters list to update to include the c4dpy interpreter.
    7. Choose OK to select the Python 2.7 (1) interpreter (Or however your renamed c4dpy.exe interpreter is listed).
    8. In the text editor type from c4d import then hit ctrl + space trigger auto-complete.
      • You may see a status update about "Updating skeletons" this can take a few minutes the first time as PyCharm has to build up a map of the C4D sdk (<- I think that's what it's doing, not certain).
    9. Eventually, you should see something like:
      0_1536095233162_0aa181f4-664c-4abd-9aab-951251f4e47b-image.png

    Congratulations, you've now got AutoComplete working in PyCharm!

    Microsoft VS Code

    0_1536099876801_ed15bae1-6a61-4343-acf7-9074ec817b47-image.png

    Prerequisites

    If you can't get IntelliSense (autocomplete) working with a default Python install, it's probably not going to work with a C4D Py. If you can do the following, you're in good shape to work with C4D Py in VS Code.

    1. Install the Python VS Code Extension
    2. Follow the VS Code Python Tutorial.

    Configure VS Code

    1. Open VS Code
    2. File > Open Folder > ... Navigate to your C4D Scripts folder (You can use C4D > Scripts > User Scripts > Scripts Folder... to easily find it).
    3. Ctrl + Shift + P to search for : "workspace settings" (Note: this setting is only being adjusted for this folder).
      0_1536100794561_44027c89-7521-4f55-83f0-49acf9be3b4b-image.png
    4. In the worspace settings panel...
      0_1536100773150_8cedc76b-9a0f-4a94-a1da-b0a8ff83d601-image.png
    5. Start typing python.pythonPath then hit enter
      0_1536100876197_6817d75d-0406-4b13-867a-d2c559626c13-image.png
    {
        "python.pythonPath": "python"
    }
    
    1. Set the python path to the location of your c4dpy.exe file.
    {
        "python.pythonPath": "C:\Program Files\MAXON\Cinema 4D R20\c4dpy.exe"
    }
    

    Windows paths use a \ which needs to be escaped \\ or reversed / for the path to properly register in the VSCode Settings.

    {
        "python.pythonPath": "C:/Program Files/MAXON/Cinema 4D R20/c4dpy.exe"
    }
    
    1. File > Save Workspace (This will ensure the settings get remembered.
    2. Restart VS Code
    3. In helloworld.py in main(): type gui. and then press ctrl + space. You'll see:
      0_1536101234399_ab8f03a9-d9d7-4021-bb78-5be47a1596ac-image.png
    4. Wait a minute as VS code builds up its understanding of C4D's API, eventually, you should see:
      0_1536101334378_8ff2854d-979c-4f96-9ea3-395bd00d69aa-image.png

    Congratulations, you've now got AutoComplete working in VS Code!

    To the C4D Devs: If these instructions are accurate, and you haven't alread written something similar, feel free to include it in the SDK / C4D Py Docs.

    -- Edits --

    2018/09/05 10:27 - Added screenshot to clarify that you should copy the contents of the Unzipped file to your C4D directory.
    2018/09/04 15:50 - Added instructions for VS Code.



  • This post is deleted!


  • You total legend.

    Thank you so much for the time and care you took on this. I'll try it out now.

    Alfie



  • Worked like a charm. Thank you.

    The only possible clarification I would add (because despite your fantastic directions, this still got me for a few mins) is that you need to take the contents OUT of the unzipped folder, so that the c4dpy.exe sits in the same folder as the actual cinema4d.exe.

    I'm aware you did explain this.. its just been a slow morning.

    Alfie



  • @alfieb Thanks for the heads up. Added a screenshot to clarify.


  • Global Moderator

    Thanks Donovan for your elaborate description.
    I'll notify in this thread as soon as the section about c4dpy got added to the Python SDK docs.

    One reminder:
    Please help us to keep this place tidy and make use of the available tags, when posting. For this thread I already added some.



  • Hello everyone,
    Someone have tested this workflow on mac? I'm trying to do it on a mac but i can't setup the IDE. Probably the problem is my lack in terminal shell usage but i don't know if possible to execute a .exe file via shell on a mac system. When i drag the c4dpy.app file into the shell and after pressed enter i don't see this "Welcome to the world of C4D and Python 2.7.14 (default, May 3 2018, 18:05:57) [MSC v.1500 64 bit (AMD64)] (c) 2018" but this " -bash: /Applications/MAXON/Cinema 4D R20/c4dpy.app: is a directory". If i drag the c4dpy.exe the result is: -bash: /Applications/MAXON/Cinema 4D R20/c4dpy.exe: cannot execute binary file
    Going on i can't set up the environment properly. Someone could help me please? thanks in advance

    G.L.



  • Hi Giuseppe,
    After the instruction from dskeithbuck, i set up on both windows and mac. With mac its a little different due the way the os packages things. You are getting the error message because the .app is indeed a directory. Open it by right click -> Show package contents -> Contents -> MacOS and in there is the executable you need.

    Hope this helps.

    Alfie



  • @alfieb Thank you so much for your kindly reply! I have solved thanks to your suggestion :-)!


  • Global Moderator

    Hello,

    we have updated the Python SDK documentation including c4dpy instructions.
    Let us know, if you are missing something in there.

    Cheers,
    Andreas



  • I haven't been able to get c4dpy.exe to run without throwing an 0xC5 access violation error. I have the latest .exe downloaded and placed next to all the other C4D R20 executables, and my R20 instance is running R20.028. Does c4dpy.exe need to be updated for R20.028? I see lots of "UNKNOWNBUILDID" messages in stdout.


  • Global Moderator

    Hi,

    I'm terribly sorry for the inconvenience caused. You are right c4dpy needs to be updated for the latest hotfix (Cinema 4D R20 SP1, 20.028). Unfortunately due to our travels I had failed to upload the file. Sorry!
    The updated version is now available on our blog's download page.

    Bye,
    Andreas



  • Cool! Thanks very much. That's working for me now.