Solved xRef file path is broken if created via python

Hello,

I made a simple python plugin for automated project generation in C4D 2023.1.3.
It basically loads a 3D model, creates a xRef that points to a master project file which includes all materials and then saves the document. The idea is, to have one file where I can edit materials, which automatically transfer to all my generated projects.

Everything works fine, except the xRef connection.
When I open my generated project, all xRef settings are greyed out and seem to have a broken file path (even though I inserted an absolute path, it displays just the filename). The weird thing is, that all the xRef materials and objects are imported successfully, but the connection to the file is lost and won't update. When I do it manually everything works as expected.

This post helped me a lot to set it up: https://plugincafe.maxon.net/topic/14319/does-xref-or-xref-simple-are-now-accessible-with-python

So my code looks like this (only the important parts):

# loading the master project
doc = c4d.documents.LoadDocument("C:/path/to/master/project.c4d", c4d.SCENEFILTER_NONE)

# creating the xref for the materials
xref_materials = c4d.BaseObject(c4d.Oxref)
doc.InsertObject(xref_materials)
materialFileId = c4d.DescID(c4d.DescLevel(c4d.ID_CA_XREF_FILE, c4d.DTYPE_FILENAME, 0))
xref_materials.SetParameter(materialFileId, "C:/path/to/material/project.c4d", c4d.DESCFLAGS_SET_USERINTERACTION)
c4d.EventAdd()

# loading the 3d model
c4d.documents.MergeDocument(doc, "C:/path/to/model.obj", c4d.SCENEFILTER_OBJECTS)

# saving the project to another location
c4d.documents.SaveDocument(doc, "C:/path/to/target/file.c4d", c4d.SAVEDOCUMENTFLAGS_NONE, c4d.FORMAT_C4DEXPORT)

I also tested different path formats like "file:///" and relative paths, but the problem persists.
Pre configuring the xRef in a seperate C4D project file and merging that into the current project, instead of creating the xRef directly, didn't work either.
Am I missing something?

Thank you very much for your help,
David

@BackendKG Hello @user,

Welcome to the Plugin Café forum and the Cinema 4D development community, it is great to have you with us!

Getting Started

Before creating your next postings, we would recommend making yourself accustomed with our Forum and Support Guidelines, as they line out details about the Maxon SDK Group support procedures. Of special importance are:

About your First Question

You said that you

[...] made a simple python plugin for automated project generation in C4D 2023.1.3.

What do you mean by that? Are you using c4dpy or are you using Cinema 4D? When I run my code from the old thread, I do not have any problems crating such Xref, saving it as a file, and loading that file back.

760107d1-96e8-4f6e-85d3-d6482a976fb0-image.png

  1. Please share which application you are using.
  2. Please share executable code, in your snippet at least the import statements are missing.
  3. Check if your file paths are correct. You use there forward slashes for a Windows path which should work, but it is better to escape paths when you write them manually. E.g., r"e:/test.c4d" or "e://test.c4d". Finally, you could also make sure that your Urls are being interpreted as being in the file scheme with r"file:///e:/test.c4d".

When you use c4dpy, you probably have to execute the passes on that document before saving it, or do some message magic, given how custom Xrefs are with being bound to the GUI in form of DESCFLAGS_SET_USERINTERACTION. Or in the worst case, it might be impossible to create Xref instances in a headless Cinema 4D instance, i.e., c4dpy.

Cheers,
Ferdinand

MAXON SDK Specialist
developers.maxon.net

Sorry for not making my post as clear as it should have been, next time I will do better :)

The good thing is, I fixed it! The problem was how I loaded my initial master project.

# instead of using
c4d.documents.LoadDocument()

# i replaced it with
c4d.documents.LoadFile()

... and now my xRef works just fine!

Thank you again!