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

    how to close c4dpy inside a script?

    Cinema 4D SDK
    python
    3
    3
    665
    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
      JACK0319
      last edited by

      I am using command-line to execute a C4D script through c4dpy.exe. I want to close c4dpy after the script execution completes. How can I do this?

      def main():
          parser = argparse.ArgumentParser()
          parser.add_argument('--args', type=str, help='argument to be passed')
      
          args = parser.parse_args()
          if args.args:
              print(f'Received argument: {args.args}')      
      
          # quit c4d?
          c4d.CallCommand(12104)
      
      

      In addition, I am rendering a octane scene in this script, but sometimes the rendering process keeps running and it is unclear whether it is a bug in c4dpy or if I am using c4dpy incorrectly.

      import c4d 
      import os
      
      class RenderAgent:
      
          def __init__(self):
              self.progress = 0
              
          def render(self, doc, save_path=None, thread=None):
              self.progress = 0
              rd = doc.GetActiveRenderData()
              rd[c4d.RDATA_ALPHACHANNEL] = 1
              bmp = c4d.bitmaps.MultipassBitmap(int(rd[c4d.RDATA_XRES]), int(rd[c4d.RDATA_YRES]), c4d.COLORMODE_RGB)
              bmp.AddChannel(True, True)
              # Renders the document
              if thread:
                  thread = thread.Get()
              
              if c4d.documents.RenderDocument(doc, rd.GetData(), bmp, c4d.RENDERFLAGS_EXTERNAL, thread, prog=self.PythonCallBack, wprog=self.PythonWriteCallBack) != c4d.RENDERRESULT_OK:
                  #print("Failed to render the temporary document.")
                  return False
              if save_path:
                  bmp.Save(save_path, c4d.FILTER_PNG, data=None, savebits=c4d.SAVEBIT_ALPHA)
              return bmp
      
          def PythonCallBack(self, progress, progress_type):
              self.progress = float(int(progress*100))
              if self.progress>100:
                  self.progress = 100
              print(self.progress)
              # !!
              # When i print self.progress  inside octane renderer scene (Actually, I am using a logging module. It can redirect the print output to an external log.txt file.)
              # it will still return 0 after it return 100 
              # It seems that the renderer has entered an infinite loop.
      
          def PythonWriteCallBack(self, mode, bmp, fn, mainImage, frame, renderTime, streamnum, streamname):
              ...
      
      # call this in c4dpy
      # simple code
      RA = RenderAgent()
      save_path = r"..."
      RA.render(doc, save_path)
      
      

      my log file looks like this:

      
      LEVEL:1 - 2023-05-06 16:10:23
      rendering start
      
      LEVEL:1 - 2023-05-06 16:10:23
      rendering:0.0
      
      # ... (rendering
      
      LEVEL:1 - 2023-05-06 16:10:23
      rendering:100.0  # it seems like render finished but
      
      LEVEL:1 - 2023-05-06 16:10:23
      rendering:0.0
      
      LEVEL:1 - 2023-05-06 16:10:23
      rendering:0.0
      
      ... 
      
      # (it keeps output 0.0
      
      
      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @JACK0319
        last edited by ferdinand

        Hello @JACK0319,

        Thank you for reaching out to us. As announced here, Maxon is currently conducting a company meeting. Please understand that our capability to answer questions is therefore limited at the moment.

        Your question is a bit ambiguous since you do not disclose how you invoke your code, but I assume you are passing the script as an argument to the c4dpy executable.

        # Content of test.py
        import c4d
        
        print (f"I am a script using the '{c4d.__name__}' module.")
        

        cmdline.gif

        In this case, the c4dpy executable will terminate itself once the script has exited, just as a vanilla CPython would. If you use c4dpy as a REPL interpreter instead, you must use the common exit() function. You can also use this function in a script, but some people (me included) would consider using exit() in a script bad style.

        cmdline2.gif

        But in your case the problem seems to be that c4d.documents.RenderDocument never terminates the render loop. I would recommend to:

        1. Put a print or log statement after your RenderDocument so that you can unambiguously see if the function is ever exited or not.
        2. Check if the same problem does also occur when your invoke the script from the Script Manager.
        3. Check if the same problem does also occur when you use the Standard Renderer as the render engine (or Redshift).

        Cheers,
        Ferdinand

        MAXON SDK Specialist
        developers.maxon.net

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

          Hello @JACK0319,

          without further questions or postings, we will consider this topic as solved by Monday 05/06/2023 and flag it accordingly.

          Thank you for your understanding,
          Maxime.

          MAXON SDK Specialist

          Development Blog, MAXON Registered Developer

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