Page 1 of 1

How to force View3D update in the middle of Python script...

Posted: Wed Jul 24, 2013 9:15 pm
by dvochin
Hi all,

I need to create a python script that will select some objects, rotate the view to a set position and perform a bpy.ops.mesh.knife_project() from the my updated view position.

The script works perfectly but unfortunately my call to knife_project() does the cutting from whatever position the view was when I invoked the script, and its only when the script finishes that blender updates the view to my position.

I need to force blender to update its 3d view to my camera's position just before my call to knife_project()

Is there a way to do that? I've even tried dozens of calls to 'bpy.context.scene.update()' but that didn't do it.

Many thanks!


Posted: Wed Jul 24, 2013 9:17 pm
by dvochin
P.S. I've tried setting "Smooth View" to zero in User Preferences / Interface to enable immediate camera changes but the same thing happens.

Posted: Wed Jul 24, 2013 10:47 pm
by dvochin
Found it!

If this helps someone just insert a "bpy.ops.wm.redraw_timer(type='DRAW', iterations=1)" in the middle of your code and further statements will be able to act upon the updated scene.

I can now have knife_project() being purely programmatic and being correctly oriented to whatever position I want to cut from! Sweet! :)

Posted: Thu Jul 25, 2013 1:37 am
by CoDEmanX
you should rather try



bpy.context.area.tag_redraw() # granted the context area is the one you are working with

redraw_timer is really a hack and should be avoided if there are other ways

Posted: Thu Jul 25, 2013 2:20 am
by dvochin
Hi Codemanx, thanks for taking the time... your continued help in this forum is priceless!

I tried bpy.ops.scene.update() many times and it doesn't work. I just tried bpy.context.area.tag_redraw() just now and it also doesn't do it! So far the only thing I found that works is bpy.ops.wm.redraw_timer(type='DRAW', iterations=1)

Maybe the other two don't work in this particular case because I only changed the camera without changing any mesh?

If redraw_timer() is not recommended, is there a way to get either of the other two to work by (for example) tagging a mesh as dirty?

Thanks again! :)

Posted: Sun Jul 28, 2013 11:20 pm
by CoDEmanX
tagging the area you wanna update is the way to go. Make sure you tag the correct one!
Such iteration is shown in the 2nd code example here: ... on-context

Posted: Mon Jul 29, 2013 12:33 am
by dvochin
Very well, thanks for the tip, will try!