Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

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

    Cinema 4D SDK
    python
    2
    3
    707
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      johntravolski
      last edited by Manuel

      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.

      1 Reply Last reply Reply Quote 0
      • ManuelM
        Manuel
        last edited by

        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

        J 1 Reply Last reply Reply Quote 0
        • J
          johntravolski @Manuel
          last edited by

          @m_magalhaes This worked great, thank you.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post