Solved Python Script to Export multiple GLTF files (glTF Exporter Unexpected Error)

I want to change the value of a parameter and then export a .gltf file in C4D R23, and do this over and over again for many values. I'm trying to do it with this code, but it is not working:

import c4d
from c4d import documents, plugins
import c4d.documents as docs

def main():
    for ii in range(882):
        obj = doc.SearchObject("Selector")
        obj[c4d.ID_USERDATA,1] = ii
        docs.SaveDocument(c4d.documents.GetActiveDocument(), f'out{ii}.gltf', c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST, format=c4d.FORMAT_GLTFEXPORT)
        c4d.EventAdd()



if __name__=='__main__':
    main()

Unfortunately, I get this error:

5105c8fa-243c-4625-9dae-c284ec0eae98-image.png

How do I fix this? I think I need to specify the gltf export settings somehow, but I don't know how to do this.

Hi,

please for you next post, follow our guildelines, using the forum function to mark your thread as a question or solve once it's done help us a lot. Also, tags can help us to faster reproduce the issue.

we got an example on github about exporting obj. I've adapte the code to export gltf

And please don't ask the question on different forum, otherwise you will waste people time.

In your case, you are changing a userdata value. Using c4d.EventAdd() in a loop will (may not) update cinema4D on each loop. c4d.EventAdd() just push an event on a stack.

Instead, you may want to use ExecutePasses. This will update the scene without updating the editor. On some specific case, you must call it twice in a row to be sure the scene is updated.

import c4d

def main():
    # Retrieves a path to save the exported file
    filePath = c4d.storage.LoadDialog(title="Save File for OBJ Export", flags=c4d.FILESELECT_SAVE, force_suffix="gltf")
    if not filePath:
        return

    print (filePath)
    # Retrieves glft export plugin.
    objExportId = c4d.FORMAT_GLTFEXPORT
    plug = c4d.plugins.FindPlugin(objExportId, c4d.PLUGINTYPE_SCENESAVER)
    if plug is None:
        raise RuntimeError("Failed to retrieve the gltf exporter.")

    data = dict()
    # Sends MSG_RETRIEVEPRIVATEDATA to gltf export plugin
    if not plug.Message(c4d.MSG_RETRIEVEPRIVATEDATA, data):
        raise RuntimeError("Failed to retrieve private data.")

    # BaseList2D object stored in "imexporter" key hold the settings
    objExport = data.get("imexporter", None)
    if objExport is None:
        raise RuntimeError("Failed to retrieve BaseContainer private data.")

    # Defines gltf export settings
    objExport[c4d.GLTFEXPORTER_UNITSCALE] = 2.0
    objExport[c4d.GLTFEXPORTER_CURRENTFRAME] = True

    
    # Finally export the document
    if not c4d.documents.SaveDocument(doc, filePath, c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST, objExportId):
        raise RuntimeError("Failed to save the document.")

    print("Document successfully exported to:", filePath)


if __name__ == '__main__':
    main()

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

Hi,

please for you next post, follow our guildelines, using the forum function to mark your thread as a question or solve once it's done help us a lot. Also, tags can help us to faster reproduce the issue.

we got an example on github about exporting obj. I've adapte the code to export gltf

And please don't ask the question on different forum, otherwise you will waste people time.

In your case, you are changing a userdata value. Using c4d.EventAdd() in a loop will (may not) update cinema4D on each loop. c4d.EventAdd() just push an event on a stack.

Instead, you may want to use ExecutePasses. This will update the scene without updating the editor. On some specific case, you must call it twice in a row to be sure the scene is updated.

import c4d

def main():
    # Retrieves a path to save the exported file
    filePath = c4d.storage.LoadDialog(title="Save File for OBJ Export", flags=c4d.FILESELECT_SAVE, force_suffix="gltf")
    if not filePath:
        return

    print (filePath)
    # Retrieves glft export plugin.
    objExportId = c4d.FORMAT_GLTFEXPORT
    plug = c4d.plugins.FindPlugin(objExportId, c4d.PLUGINTYPE_SCENESAVER)
    if plug is None:
        raise RuntimeError("Failed to retrieve the gltf exporter.")

    data = dict()
    # Sends MSG_RETRIEVEPRIVATEDATA to gltf export plugin
    if not plug.Message(c4d.MSG_RETRIEVEPRIVATEDATA, data):
        raise RuntimeError("Failed to retrieve private data.")

    # BaseList2D object stored in "imexporter" key hold the settings
    objExport = data.get("imexporter", None)
    if objExport is None:
        raise RuntimeError("Failed to retrieve BaseContainer private data.")

    # Defines gltf export settings
    objExport[c4d.GLTFEXPORTER_UNITSCALE] = 2.0
    objExport[c4d.GLTFEXPORTER_CURRENTFRAME] = True

    
    # Finally export the document
    if not c4d.documents.SaveDocument(doc, filePath, c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST, objExportId):
        raise RuntimeError("Failed to save the document.")

    print("Document successfully exported to:", filePath)


if __name__ == '__main__':
    main()

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

@m_magalhaes This worked great, thank you.