Stop long running python script

On 30/04/2013 at 03:32, xxxxxxxx wrote:


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?


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.


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

Hi Niklas,

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