Access C4D Objects outside C4D?



  • Hi,

    Is there a way to access C4D Objects outside C4D?

    For example,

    1. Open a file
    2. Access object
    3. Modify Object

    Here's a code mock-up executed through a command line (outside C4D)

    c4dpy.exe teapot.py

    import os
    import c4d
    
    # Open File
    file = r"D:\teapot.c4d"
    os.startfile(file_path) 
    
    # Acces object. 
    teapot = doc.SearchObject("teapot") 
    
    # Modify object. 
    teapot.SetRelPos(c4d.Vector(10,10,10))
    
    c4d.EventAdd()
    
    

    This gives me an error of
    AttributeError: 'NoneType' object has no attribute 'SetRelPos'

    Regards,
    Ben



  • Hi @bentraje just to be sure we are on the same page, c4dpy is nothing more than c4d lunched without GUI, but with a Python Interpreter, but in the end it's still a C4D standalone, so you need c4d to be installed.
    If you really need to open a c4d file without Cinema 4D then you will need the Cineware Engine, which is a C++ library with his limitation since you can't execute or evaluate stuff only retrieve cached data when you save a project for Cineware. For more information look at Cineware Information Page.

    Regarding your code this is kind of normal this is not working because as said previously c4dpy is a Cinema 4D stand-alone, so when you do os.startfile(file_path), this will execute the default application for a c4d file so Cinema 4D. Then just after that, you do a searchObject, but you didn't load the document in the current context so this is normal nothing is found.
    So instead of os.startfile you should do a doc = c4d.documents.LoadDocument(file, c4d.SCENEFILTER_OBJECTS | c4d.SCENEFILTER_MATERIALS)

    Then everything should work.
    Cheers,
    Maxime.



  • @m_adam

    Thanks for the response.
    I tried replacing the line as you suggested but it gives an error

    D:\>c4dpy.exe teapot.py ('Error Line : 64', 'error', error(10048, 'Only one usage of each socket address (protocol/network address/port) is normally permitted'))

    Interestingly, my teapot.py doesn't actually reach 64 lines.

    Is there way around this?


Log in to reply