0
$\begingroup$

Is there a way to terminate a script in Blender python that is somewhat equivalent to the Bash exit command? I found this article but the suggestions did not work for Blender python. I also found the thread Python: Conditional to stop the execution of a script [closed] but there was nothing discussed about stopping the script execution. I've tried the exit(), quit(), sys.exit(), and os._exit(0) functions but they behave like bpy.ops.wm.quit_blender() and close the entire Blender application. The exit (without parenthesis) command does nothing. I sometimes need to do some testing and have to get the script to terminate at some point in the execution and adding a simple one-liner terminate code would be really convenient.

In bash I would just have to call exit

#!/bin/bash

echo "do some stuff"
exit 0
echo "don't execute stuff after exit coz i'm still testing the stuff above"

But in Blender python how do I do this?

print("do some stuff")
exit # exit(), sys.exit(), bpy.ops.wm.quit_blender()
print("don't execute stuff after exit coz i'm still testing the stuff above")

I don't like the inconvenient string ''' usage for pseudo-comments because it requires me to add 2 lines of ''' instead of a one-liner exit/terminate command.

EDIT:

I found a useful workaround that doesn't require me to touch the closing pair of the ''' since it is commented out at the bottom and only becomes active if i insert ''' that would represent the pseudo-"exit" statement.

print("do some stuff")
''' # i can add/remove this without needing to touch the closing pair
print("don't execute stuff after exit coz i'm still testing the stuff above")
#'''
$\endgroup$
6
  • 1
    $\begingroup$ If you just want to skip code for testing purposes you also could create a function and use the return statement to jump out at a certain line. $\endgroup$
    – Blunder
    Commented Jul 12, 2022 at 13:18
  • $\begingroup$ yeah that's another possible workaround. i like the comment trick though. looks like there is no "exit" in python blender. $\endgroup$
    – Harry McKenzie
    Commented Jul 12, 2022 at 13:22
  • 1
    $\begingroup$ There is an exit, but, as you found out, it does what Python spec says it should do and completely exists the program. That is, by the way, how bash exit works as well, except bash forks a subprocess and waits for it to exit. I don't recommend using subprocesses for this sort of debugging. FWIW, I just paste segments of code into the Python window when I'm doing this sort of debugging, but @Blunder suggestion is a good one if you don't want the hassle of pasting. $\endgroup$ Commented Jul 12, 2022 at 15:26
  • $\begingroup$ @MartyFouts yes that is correct. i like the comment trick though its much better coz that way i dont have to indent the code. $\endgroup$
    – Harry McKenzie
    Commented Jul 12, 2022 at 16:10
  • 1
    $\begingroup$ Sure. As soon as I get back to my desk. $\endgroup$ Commented Jul 13, 2022 at 13:38

1 Answer 1

1
$\begingroup$

At Harry McKenzie's request I've created this answer from comments on the question.

I pointed out:

There is an exit, but, as you found out, it does what Python spec says it should do and completely exists the program. That is, by the way, how bash exit works as well, except bash forks a subprocess and waits for it to exit.

and mentioned

I just paste segments of code into the Python window when I'm doing this sort of debugging.

Blunder suggested one useful workaround:

If you just want to skip code for testing purposes you also could create a function and use the return statement to jump out at a certain line.

Harry McKenzie described a way to effectively "comment out" code. It's a two step process:

  1. Place the line #''' (a comment hash-tag (octothorpe) followed by three single quotes) as the last line of the script. If there is no matching set of three single quotes earlier, it will be treated by the parser as a comment and ignored.

  2. "comment out" starting with the first line you don't want executed by adding ''' # (three single quotes followed by a comment hash-tag (octothorpe) at the start of that line.

This works by overloading the doc-string convention. Blender treats everything between the first three single quotes and the last three as one long string that it reserves as a documentation string.

Note that there is one small issue with this approach. If there is an actual doc string in the "commented out" region, it will cause an error. Consider this contrived script:

print("run this")
print("ignore this")
'''
this is a real doc string
'''
print("ignore this")

Now consider wanting to only run the first line. Using the doc-string trick gives this code

print("run this")
''' #print("ignore this")
'''
this is a real doc string
'''
print("ignore this")
#'''

but that has resulted in the real doc string being outside the string and running this will give a syntax error at line 4.

$\endgroup$
1
  • $\begingroup$ excellent! thank you! $\endgroup$
    – Harry McKenzie
    Commented Jul 13, 2022 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .