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

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

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

Post 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!

Dan,

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

Post 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.

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

Post 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! :)

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX »

you should rather try

bpy.context.scene.update()

or

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
I'm sitting, waiting, wishing, building Blender in superstition...

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

Post 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! :)

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post 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:
http://www.blender.org/documentation/bl ... on-context
I'm sitting, waiting, wishing, building Blender in superstition...

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

Post by dvochin »

Very well, thanks for the tip, will try!

Post Reply