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

    Export selected objects to separate files

    Cinema 4D SDK
    3
    8
    4.4k
    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.
    • K
      klints2
      last edited by m_adam

      Hello guys,

      I wrote small script and got one problem. I can't export selected objects to separate files, after export operation i got obj files with duplicate meshes inside. Which the command do i must use for it?

      import c4d
      import os
      from os.path import join, normpath
      
      dirpath = "c:/usr/temp/mayablender/"
      
      def main():
          doc = c4d.documents.GetActiveDocument()
          selected = doc.GetActiveObjects(0)
      
       
          for ob in selected:
              ob.DelBit(c4d.BIT_ACTIVE) 
              doc.SetActiveObject(ob)        
                         
              
              full_dirpath = normpath(join(dirpath + ob.GetName() + ".obj"))
      
              # export to obj
              c4d.documents.SaveDocument(doc,full_dirpath,c4d.SAVEDOCUMENTFLAGS_0,1030178)
              
      
      
      if __name__=='__main__':
          main()
      
      
      1 Reply Last reply Reply Quote 0
      • K
        klints2
        last edited by

        Whos knows any solution for it?

        mikeudinM 1 Reply Last reply Reply Quote 0
        • M
          m_adam
          last edited by

          Hi @klints2, first of all, welcome in the plugincafe community.

          I would like to point you some rules on this forum regarding how to post questions in order to help us to help you!

          • How to Post Questions especially the tagging part and take care to post on the correct category.
          • Q&A New Functionality.

          Then additionally it's not necessary to bump a topic to put more pressure on us, we monitor this forum and try to answers as soon as we can due to our workload.
          Don't worry it's your first post you couldn't know, so I've setup your topic correctly. 😉

          Regarding your issue. Cinema 4D Obj exporter and all Cinema 4D exporters, does not offer a way to export only a list of objects.
          So you have to copy all the stuff needed into a temporary document and then export this temporary document
          the function c4d.documents.IsolateObjects does exactly that an returns the temporary BaseDocument.

          Then in order to properly setup, the obj exported I invite you to read the export_OBJ in our GitHub repository.

          If you have any questions, please let me know.
          Cheers,
          Maxime.

          MAXON SDK Specialist

          Development Blog, MAXON Registered Developer

          1 Reply Last reply Reply Quote 3
          • mikeudinM
            mikeudin @klints2
            last edited by

            @klints2
            Try to use IsolateObjects

            A helper routine to copy the objects of document to a new document (returned). All materials associated are also copied over and the links are corrected.

            Checkout my python tutorials, plugins, scripts, xpresso presets and more
            https://mikeudin.net

            K 1 Reply Last reply Reply Quote 2
            • K
              klints2 @mikeudin
              last edited by

              @mikeudin Thanks, but don't undestand how works this command, i was tried to get list from isolated = c4d.documents.IsolateObjects(doc, selected) but it's give me nothing and i can't use for for it.
              How do i can exported only selected list to obj, without dialog windows?

              Sorry this is my first script for c4d, i'm maya user and little confused. Thanks

              1 Reply Last reply Reply Quote 0
              • K
                klints2
                last edited by klints2

                Well, I think found it.
                Don't perfect but its works

                import c4d
                import os
                from os.path import join, normpath
                from c4d import documents
                
                def export():
                
                    dirpath = "c:/usr/temp/"  # temp directory
                    global tdoc
                    
                    sobj = doc.GetActiveObjects(0)
                    # isolate objects
                    tdoc = c4d.documents.IsolateObjects(doc, sobj)
                   
                    # get path with object name
                    full_dirpath = normpath(join(dirpath + (doc.GetActiveObjects(0)[0]).GetName() + ".obj"))
                   
                    # export to obj
                    documents.SaveDocument(tdoc,full_dirpath,c4d.SAVEDOCUMENTFLAGS_0,1030178)        
                
                if __name__=='__main__':
                    export()
                
                    documents.KillDocument(tdoc)
                
                1 Reply Last reply Reply Quote 1
                • M
                  m_adam
                  last edited by

                  Hi @klints2, may I ask you why this is not perfect? Moreover here just a few corrections in your code.

                  • In Cinema 4D (and in general), try to avoid as much as possible global variables. In your case you can call documents.KillDocument(tdoc) just after the SaveDocument.
                  • Make use of join as the function should behave.
                  dirpath = "c:/usr/temp/"  # temp directory
                  full_dirpath = normpath(join(dirpath, (doc.GetActiveObjects(0)[0]).GetName() + ".obj"))
                  
                  • Always check, you get object selected or you will get some errors in the console.
                  sobj = doc.GetActiveObjects(0)
                  if not sobj: return
                  

                  Cheers,
                  Maxime.

                  MAXON SDK Specialist

                  Development Blog, MAXON Registered Developer

                  1 Reply Last reply Reply Quote 3
                  • K
                    klints2
                    last edited by klints2

                    Hi @m_adam, thanks for you correction and advice, it's really usefull for me. I'm learn python just couple month and not sure in my code yet.

                    • yeah i'm missed this mistake
                      full_dirpath = normpath(join(dirpath, (doc.GetActiveObjects(0)[0]).GetName() + ".obj"))

                    Thanks one more time, always good to have help and support!

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