Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python 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
    • Recent
    • Tags
    • Users
    • Login

    Saving the Current Document

    Scheduled Pinned Locked Moved PYTHON Development
    8 Posts 0 Posters 724 Views
    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.
    • H Offline
      Helper
      last edited by

      On 24/10/2014 at 10:17, xxxxxxxx wrote:

      Hi,

      Any tips on how to prompt the user to save the current document, and then make the saved document the active document?

      This is what I'm currently doing:

        
      def main() :   
          c4d.StopAllThreads()   
        
          if is_file_saved(doc) :   
              print doc.GetDocumentName() + " was saved."   
          else:   
              c4d.gui.MessageDialog("This document has not been previously saved, please save it now.")   
              default_filename = "SHOW_SEQ_Shot01_vA01.c4d"   
              doc_was_saved = c4d.documents.SaveDocument(doc, default_filename, saveflags=c4d.SAVEDOCUMENTFLAGS_0,   
                                         format=c4d.FORMAT_C4DEXPORT)   
              if doc_was_saved:   
                  print "Document was successfully saved."   
              else:   
                  print "Document failed to save."   
                  return   
      

      This will open up a save dialog, and allow the user to rename the file and save it where they want. However, after the document has been saved, the current document is still named "Untitled 1" or something to that effect. Some questions:

      1. How do I save the current file, and ensure that the saved file becomes the working file as opposed to the default "Save As..." / Make-a-copy behavior that seems to be occurring?
      2. Assuming #1 isn't possible, how do I retrieve the name/path of the document the user has saved so that I can kill the current doc and load the saved version?

      Thanks,

      Donovan

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 24/10/2014 at 11:39, xxxxxxxx wrote:

        Okay, it feels a little hacky (I hate using CallCommand), but this is my solution:

          
        def is_file_saved(doc) :   
            """Determines whether `doc` has been previously saved.   
            Returns `True` if saved, otherwise `False`"""   
          
            #Ensure there's a document   
            if (doc is None) or (not doc.IsAlive()) :   
                return False   
          
            #See if the document has been saved   
            doc_path = doc.GetDocumentPath()   
            if (doc_path is None) or (doc_path == "") :   
                return False   
          
            #The doc exists and has a name, return True   
            return True   
          
        def save_document(doc, file_name="SHOW_SEQ_Shot01_vA01.c4d") :   
            """Prompt user to save document. Returns `True` if saved, or `False` if the file wasn't saved."""   
          
            doc.SetDocumentName(file_name)   
            c4d.gui.MessageDialog("This document has not been previously saved, please save it now.")   
            c4d.CallCommand(12098) #Save Current Document   
          
            return is_file_saved(doc)   
        
        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 24/10/2014 at 12:19, xxxxxxxx wrote:

          Hi

          if you don´t like it hacky, you might use this solution:

            
          import c4d,os  
          from c4d import gui  
            
            
            
          def is_file_saved(doc) :  
            doc = c4d.documents.GetActiveDocument()  
            if doc.GetDocumentPath() != "" :  
                return True  
            
            
            
          def main() :  
            
            c4d.StopAllThreads()  
            
            
            
            if is_file_saved(doc) :  
            
                print doc.GetDocumentName() + " was saved."  
            
            else:  
            
                c4d.gui.MessageDialog("This document has not been previously saved, please save it now.")  
            
            
            
                path = c4d.storage.SaveDialog(type=c4d.FILESELECTTYPE_SCENES , title="Save", force_suffix="c4d", def_path="")  
                  
            
                c4d.documents.SaveDocument(doc, path, c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST,format=c4d.FORMAT_C4DEXPORT)  
            
                c4d.documents.LoadFile(path)                             
                c4d.documents.KillDocument(doc)  
                  
            
          if __name__=='__main__':  
            main()  
            
          

          Best wishes
          Martin

          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 24/10/2014 at 14:07, xxxxxxxx wrote:

            Thanks for that! Is this what Cinema 4D is doing behind the scenes? I'm wary of having two instances of a document in memory at the same time. I suppose the solution is to kill the document before loading it.

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              On 24/10/2014 at 17:09, xxxxxxxx wrote:

              Hi Donovan,

              first of all thanks for the hint with the order of loading and killing.
              vice versa as a gunshot;)
               
              I would be glad to know all the things Cinema 4D is doing behind the scene, but I´m pretty sure something similar is going on.

              I really prefere not using call commands, too.

              Best wishes
              Martin

              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

                On 24/10/2014 at 18:24, xxxxxxxx wrote:

                I don't understand why you'd want the user to be able to change the name of the current document if they're going to be still working on it?

                This makes more sense to me.
                The scene gets saved without the file browser opening. So the user is always working in the current scene:

                #This code checks if the document has been changed and not saved  
                #Then returns the correct file name string value depending if it was saved, or not saved  
                  
                import c4d  
                def main() :  
                    
                  name = doc.GetDocumentName()  
                  fn = doc.GetDocumentPath()   
                  docName = ""  
                    
                  #Checks if the document was changed since the last save operation  
                  unsaved = doc.GetChanged()  
                    
                  if(unsaved) :  
                      #name = name[ :-4 ] #Strips out the .c4d part if desired   
                      name += " *"   
                      docName = name  
                  
                      #You can make a message pop up here if you want  
                      #Or   
                      #you can automatically just save the document using c4d.CallCommand(12098)  
                            
                  else: docName = name  
                  
                  print docName   
                  
                if __name__=='__main__':  
                  main()
                

                -ScottA

                1 Reply Last reply Reply Quote 0
                • H Offline
                  Helper
                  last edited by

                  On 25/10/2014 at 01:59, xxxxxxxx wrote:

                  Hi Scott,

                  imagine a scenery where you want to set a scene management programmatically for the user.
                  Where you define all the render passes and rearrange  them after rendering.
                  Therefore the doc should be saved with a well defined name and path.
                  The user only have to choose where he want to place the folder structure.
                  That´s a way I´m using it.

                  Best wishes
                  Martin

                  1 Reply Last reply Reply Quote 0
                  • H Offline
                    Helper
                    last edited by

                    On 30/10/2014 at 16:09, xxxxxxxx wrote:

                    @ScottA - @monkeytack's explanation pretty much hits the nail on the head. I want the user to be able to specify a save location in those instances where the scene hasn't already been saved (which wasn't clear from some of the code I initially posted).

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