2
$\begingroup$

I'm currently using VS code to write a Blender script in Python. The script places objects, creates materials, unwraps objects, and finally renders an image. I'm using the "Blender Development" extension to make debugging easier. When debugging I can step through the code and see the variables change but no changes are made in the Blender UI until the script ends. Is there a way to have the blender UI change as I step through the code?

Example:

bpy.ops.mesh.primitive_cube_add(size=1)

*Breakpoint* The UI updates to show the created cube

cube_obj = bpy.context.active_object
cube_obj.location.x = 5

*Breakpoint* The UI updates to show the cube move

...

I'm not sure this is possible since I haven't found any information about it. The closest thing I can think of is to create an add-on consisting of a series of buttons that I press sequentially to run the code piece by piece.

All suggestions are appreciated, thanks!

$\endgroup$
2
  • 1
    $\begingroup$ I guess this section from the docs is what you're looking for? In short it seem to be possible, but discouraged. I have not tried this myself. Hope it helped. $\endgroup$
    – Sindri
    Commented Jan 22 at 15:25
  • $\begingroup$ @Sindri Thank you for the suggestion, this was what I was looking for! $\endgroup$ Commented Jan 24 at 16:34

2 Answers 2

2
$\begingroup$

The docs state that you can force an update from your script. It is discouraged because it can lead to instabilities. Expect crashes. However I don't think it's a problem if you only use it in a debugging environment. Just make sure you delete the lines when you release your addon.

Add this line between the two breakpoints :

bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)

$\endgroup$
1
  • $\begingroup$ Thank you, this was what i was looking for. I have been testing it a bit and Blender does crash a lot when I add too many breakpoints and redraws, but if I'm restrictive it works fine! $\endgroup$ Commented Jan 24 at 16:33
0
$\begingroup$

When you run a script, the Blender interface hangs until the script finished. You cannot, therefore, pause the execution of the script and update the UI. What you can do, however, is: load the script state from previous execution if available, move to the next step, execute this step, save the state of the script, and somehow schedule to run it again. Take a look here:

How to hide_viewport object per execution?

There the way you "schedule to run again" is simply having the user click a ▶ Play button repeatedly. But you could subscribe to a timer instead, or to any other event...

Likewise remember editing self is probably not the best way to miantain state - use a separate text block, or a custom property or a global variable...

$\endgroup$

You must log in to answer this question.

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