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

    Stop long running python script

    Scheduled Pinned Locked Moved PYTHON Development
    3 Posts 0 Posters 330 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 30/04/2013 at 03:32, xxxxxxxx wrote:

      Hi,

      I am working on a script that :
      - changes materials
      - resizes objects
      - renders images

      It can run for several days.

      I need to way to stop it in case something goes wrong.

      I've tried :
      - an async dialog with an "stop" button but it seems dialogs cannot be run asynchronously in a python script
      - a thread but gui and material changes are forbidden according to documentation

      Any ideas?

      Alex

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

        On 30/04/2013 at 04:23, xxxxxxxx wrote:

        Hi Alex,

        Scene modifications should not be done from a script. So while your script runs, you will not
        be able to do any further modifications to the scene.

        While your script is running, you can frequently check if the script should be stopped, and then,
        just stop. This can be achieved in several ways. Two ideas:

        1. Listen for a key to be pressed

        This is usually the escape key on the keyboard. You can check if the escape key is pressed
        like so

        import c4d
        from c4d.gui import GetInputState
          
        def escape_pressed() :
            bc = c4d.BaseContainer()
            rs = GetInputState(c4d.BFM_INPUT_KEYBOARD, c4d.KEY_ESC, bc)
            if rs and bc[c4d.BFM_INPUT_VALUE]:
                return True
            return False
        

        Make sure to frequently check for for the escape key. If you only check about each second, you
        can easily miss a short press on the key.

        2. Accept the break message over network

        You can open a socket and recieve a break "command" over the network. This would allow you
        to stop the script from anywhere in your local network (or even from the outside when the
        required ports etc. are set up). This approach may be a little to overkill for what you are aiming for.

        Best,
        -Niklas

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

          On 30/04/2013 at 05:33, xxxxxxxx wrote:

          Hi Niklas,

          Your first solution works great. Thanks for your fast and efficient help.

          Alex

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