35
$\begingroup$

Can I run a python script step by step with normal blender, a build, external software or an addon?

I'm developing a script (town generator) and I want to to execute the code step by step like a debugger. Is this possible?

Edit: it's possible with the eclipse software check this link

$\endgroup$
7
  • $\begingroup$ Can you explain the purpose? do you mean interactively step over the code as with a debugger?, or just that you execute code one line at a time (so a python script could be streamed from an external source, or to listen for commands and execute one line at a time, for eg). What kind of interaction with Blender are you wanting to do between each line execution? $\endgroup$
    – ideasman42
    Aug 22, 2013 at 9:00
  • $\begingroup$ I want to execute the code step by step like a debugger. I would that blender execute the code a bit like if i enter my pyhton script line by line in the python console and like that i can see what append. $\endgroup$
    – lucblender
    Aug 22, 2013 at 9:09
  • $\begingroup$ Please edit your question to make this more clear. $\endgroup$
    – ideasman42
    Aug 22, 2013 at 9:28
  • $\begingroup$ You could also use the Blender BreakPoint Addon, it stops at breakpoints and dumps local or global variables. The hosting site also provides video tutorials. $\endgroup$
    – stacker
    Nov 19, 2013 at 16:17
  • 2
    $\begingroup$ In 2020, the easiest way to do this is with this extension for Visual Studio Code: marketplace.visualstudio.com/… $\endgroup$
    – Justin
    Jan 10, 2020 at 23:59

2 Answers 2

36
$\begingroup$
  1. Launch blender from a terminal emulator.
  2. Add import pdb; pdb.set_trace() to your code where you wish to start debugging from.
  3. Run your code and you'll be put into the Python debugger, where you can use sEnter to step through the code.

See the pdb documentation for more info on usage. Also for the nicer ipython debugger (features syntax highlighting and tab completion), you can use ipdb.

$\endgroup$
16
$\begingroup$

No, Blender can't do this with the default feature set.

Edit: As Aldrik says, you can use pdb, however blender can't step over python code in its own interface.

There are however some things you can do that may be of interest to you.

Load as a python module

If you compile Blender as a Python module, you can use any Python IDE which supports debugging/stepping, The times I really wanted to step over Python code I did this, however it can't be used with a Blender window active.

See: How can I run blender from command line or a python script without opening a GUI?

Use the command line REPL

REPL, being (Read–eval–print loop). Python has a module that you can use to create a python interactive console, it can be used to execute scripts one line at a time.

See: http://docs.python.org/3/library/code.html

The example below executes 3 lines of python script, then prints a variable which the script defined:

import code
namespace = {}
console = code.InteractiveConsole(
    locals=namespace,
    filename="<blender_console>",
)

# Code - can be anything... 
# load an entire text file into this for eg.
my_code = [
    "a = 1",
    "b = 2",
    "c = a + b",
]

# execute the script line by line.
for line in my_code:
    console.push(line)

# get something back from the executed code
print(namespace["c"])
$\endgroup$

You must log in to answer this question.

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