Solved 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?

Hi,

Just wondering if you got an update on this.

Thank you

Hi @bentraje here it does work without issue with the next code:

import os
import c4d

# Open File
file = r"D:\teapot.c4d"
doc = c4d.documents.LoadDocument(file, c4d.SCENEFILTER_OBJECTS | c4d.SCENEFILTER_MATERIALS)

# Acces object. 
teapot = doc.SearchObject("teapot") 

# Modify object. 
teapot.SetRelPos(c4d.Vector(10,10,10))

c4d.EventAdd()

On which OS/ Os Version are you?
Are you able to reproduce on R23?

Cheers,
Maxime.

@m_adam

RE: Are you able to reproduce on R23?
I'm using R21. It's also on the tag above.

RE: OS/ Os Version are you?
Win10.

Anyhow, I run it again and I narrowed the problem:
If I have Cinema4D already open, it errors out.
If I haven't, it doesn't error out.

But I need the Cinema4D to be open to work with it.
I'm thinking of a functionality of the Megascans Bridge (a separate program) where you right click and import mesh into C4D.

I was asking about the Win version, there are a ton of Win 10 version 😉

I think you get the concept of c4dpy wrong. c4dpy is a cinema 4D Standalone, just run as the python interpreter.
So it's a completely independent instance of Cinema 4D with its UI a Python console.
You can execute c4dpy.exe and Cinema 4D.exe those are two independent software. The issue you are seeing is because you also need 2 licenses if you want to run both c4dpy and Cinema 4D.exe since as said previously they are two independent executable and this is the error you are seeing.

If you want to reproduce what Quixel, does with their bridge, it's just a regular Python plugin that executes a TCP server running on a background thread, and when something poping via TCP from their main host they update Cinema 4D from it.

Cheers,
Maxime.

@m_adam

Ah you are right. I had a wrong concept c4dpy lol.

RE: it's just a regular Python plugin that executes a TCP server running on a background thread
First time hearing about this. I'll probably create another thread just for this one. lol

For the current thread, I'll tag this as close.

Thank you again!

Hi,

I did encounter a similar behaviour on R21 and c4dpy. I do not think that this has something to do with your script at all and I think this is more a license problem.

c4dpy just like Cinema itself is quite chatty on startup with Maxons license server. When I did run R21 and its c4dpy it sometimes did the same thing and complained about used sockets (I assume these are used by the license client). I did only happen though when I did run c4dpy with SublimeText (Visual Code never did this, but that was probably luck since both apps just interface with the command line to run Python).

Cheers,
zipit

MAXON SDK Specialist
developers.maxon.net

VS Code and Sublime Text are not so well designed and simply spawn a bunch of python interpreters to retrieve the information they need, In contrast, pycharm does use only one and the same python interpreter its spawn.

So that's why you got these issues.