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
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    AddUndo() for MergeDocument() ?

    Cinema 4D SDK
    3
    16
    2.2k
    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.
    • ferdinandF
      ferdinand
      last edited by

      Hi,

      @m_magalhaes solution should work just fine with obj files too. If you are getting no output you probably haven't escaped special characters in your file path properly. Alternatively you could also pass a raw string (then you don't have to escape special characters).

      file_path = r"C:\myfile.c4d
      

      The reason why your script is not working as expected is probably because you are only adding Undos for the selected objects. However only the last object that has been merged into the document will be selected after the merge operation.

      Cheers
      zipit

      MAXON SDK Specialist
      developers.maxon.net

      1 Reply Last reply Reply Quote 1
      • A
        arsen @Manuel
        last edited by

        @m_magalhaes

        here is my code

        import c4d
        from c4d import utils, documents, plugins
        
        import time
        from threading import Thread
        
        
        class WatchThread(Thread):
            def __init__(self, name, doc):
                Thread.__init__(self, name=name)
                self.doc = doc
        
            def run(self):
                while True:
                    time.sleep(2.5)
                    obj_importer(self.doc, r'f:\01.obj')
                    print 'Loaded.',
                    break
        
        
        def obj_importer(doc, obj_path):
            if obj_path is None:
                return
        
            plug = plugins.FindPlugin(1030177, c4d.PLUGINTYPE_SCENELOADER)
            if plug is None:
                return
        
            op = {}
            if plug.Message(c4d.MSG_RETRIEVEPRIVATEDATA, op):
        
                if "imexporter" not in op:
                    return
        
                obj_import = op["imexporter"]
                if obj_import is None:
                    return
        
                # Parameters
                obj_import[c4d.OBJIMPORTOPTIONS_POINTTRANSFORM_SWAPXY] = False
                obj_import[c4d.OBJIMPORTOPTIONS_POINTTRANSFORM_SWAPXZ] = False
                obj_import[c4d.OBJIMPORTOPTIONS_POINTTRANSFORM_SWAPYZ] = True
        
                doc.StartUndo()
                doc.AddUndo(c4d.UNDOTYPE_CHANGE, doc)
        
                # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
                c4d.documents.MergeDocument(doc, obj_path,
                                            c4d.SCENEFILTER_OBJECTS |
                                            c4d.SCENEFILTER_MATERIALS |
                                            c4d.SCENEFILTER_MERGESCENE, None)
        
                # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
                doc.EndUndo()
        
                obj_import[c4d.OBJIMPORTOPTIONS_PRESETS] = 0
        
            c4d.StatusClear()
            c4d.EventAdd()
        
        
        if __name__ == '__main__':
            doc = c4d.documents.GetActiveDocument()
            wt = WatchThread('ht', doc)
            wt.start()
        
        
        1 Reply Last reply Reply Quote 0
        • ferdinandF
          ferdinand
          last edited by

          Hi,

          first of all: If you want to use threads, you should use c4d.threading.C4DThread and not Pythons builtin thread type or weird things might happen. Also note that threading comes with certain limitations:

          For all threaded functions it is forbidden to:

          • Add an event.
            [...]
          • Change the structure of objects attached to the document.
            [...]
          • Create undos.

          I am also not quite sure what the purpose of your thread is, since you are loading into the active document. If you want to "load in the background" you will have to use a temporary document. But for loading just one file I do not see any performance benefits, since you have to merge that temporary document into the active document at the end.

          Cheers
          zipit

          MAXON SDK Specialist
          developers.maxon.net

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

            hello,

            well as @zipit said it's forbidden to change the structure of a document outside the main thread.
            watch this page and the warning about threading

            I did checked the StartUndo and it's using GeIsMainThreadAndNoDrawThread and just get out.
            So it's pretty clear that you can't add undo on a thread.

            why are you using thread here ?

            Cheers,
            Manuel

            MAXON SDK Specialist

            MAXON Registered Developer

            A 1 Reply Last reply Reply Quote 0
            • A
              arsen @Manuel
              last edited by

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • A
                arsen
                last edited by

                @m_magalhaes @zipit
                I use thread to manipulate external pipes(subprocess etc).

                ferdinandF 1 Reply Last reply Reply Quote 0
                • ferdinandF
                  ferdinand @arsen
                  last edited by ferdinand

                  @arsen said in AddUndo() for MergeDocument() ?:

                  @m_magalhaes @zipit
                  I use thread to manipulate external pipes(subprocess etc).

                  Hi,

                  although I am aware of the concept of pipes, this does not really clear anything up for me 🧐. What do you mean by external and manipulate? Just for clarification: Under pipes I understand a data IO interface to communicate between different processes, provided in Python for example by os.pipe().

                  And I don't really get why this forces you to use a thread to import an obj file 🤔.

                  Cheers
                  zipit

                  MAXON SDK Specialist
                  developers.maxon.net

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    arsen @ferdinand
                    last edited by

                    @zipit
                    Hi
                    I use thread to track the external file. When a file is changed, it is imported.
                    When using c4d.threading, subprocess.Popen() / time.sleep() freezes C4D ( until the process is complete )

                    1 Reply Last reply Reply Quote 0
                    • ferdinandF
                      ferdinand
                      last edited by ferdinand

                      Hi,

                      ah, ok, I do understand your problem better now ;). Well as explained in this thread your cannot load files other than from the main thread of c4d.

                      You could do however the following:

                      1. Use processes, threads or whatever you would like to use to monitor the status of your file(s).
                      2. Use some cross-process communication interface like os.pipe() to indicate when your data has changed.
                      3. Implement 1. and 2. within a MessageData plugin.
                      4. Use MessageData.GetTimer and MessageData.CoreMessage to periodically peek into your pipe and then react to its status from the main thread/ the c4d process.

                      Cheers
                      zipit

                      MAXON SDK Specialist
                      developers.maxon.net

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

                        hello,

                        @zipit already make another nice answer.

                        I can just add that in c++ you have also a filemonitor manual where you can use the function WatchDirectory

                        Cheers,
                        Manuel

                        MAXON SDK Specialist

                        MAXON Registered Developer

                        1 Reply Last reply Reply Quote 0
                        • A
                          arsen
                          last edited by

                          @zipit @m_magalhaes
                          Thanks for the answers! I figured out how to solve the problem.

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

                            hi,

                            nice, feel free to share your solution and mark this thread as solved.

                            Cheers,
                            Manuel

                            MAXON SDK Specialist

                            MAXON Registered Developer

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