0
$\begingroup$

I am writing a Python script, and I need to repeat one line of code 24 times. How can I do that? I tried a for and while loop, and still cannot get it to repeat.

import bpy

def selectView():
    bpy.context.area.type = 'VIEW_3D'
    screen = bpy.context.window.screen
    for area in screen.areas:
        if area.type == 'VIEW_3D':
            for region in area.regions:
                if region.type == 'WINDOW':
                    override = {'window': bpy.context.window, 'screen': screen, 'area': area, 'region': region}
                    for i in range(0, 23):               
                        bpy.ops.view3d.view_orbit(override, type='ORBITDOWN')
                        bpy.ops.view3d.select_border(override, gesture_mode=3, xmin=-1200, xmax=1000, ymin=-1000, ymax=1000, extend=True)
                        print(i)
selectView()
bpy.context.area.type = 'TEXT_EDITOR'

What this is supposed to do is go in a circle and select the vertices on the outside of a model. Unfortunately, it is only moving the camera once and selecting everything.

Thanks for the help!

$\endgroup$
  • $\begingroup$ This question looks like a great candidate for StackOverflow.com, which is another StackExchange site, but is specific to programming. I mention this because the answer to your question is about Python in general, and doesn't seem to have much to do with using Python in Blender specifically. They have some great answers over there, and I wouldn't be surprised if your question has already been answered there ;-) Good luck! $\endgroup$ – Matt Dec 26 '13 at 15:46
  • 4
    $\begingroup$ The Python actually looks pretty solid, I think it's just the specific way that you are accessing the API that's causing the problem. $\endgroup$ – Mike Pan Dec 26 '13 at 17:01
  • 1
    $\begingroup$ If all you want to do is select the vertices on the outside of the mesh, you could select inner faces (3D view > Header > Select > Select inner) and then invert the selection Ctrl+I $\endgroup$ – gandalf3 Dec 26 '13 at 20:36
  • $\begingroup$ ...additionally, if you want to do it programmatically, perform @gandalf3's suggestion in the GUI, and the Info window should give you some information about what functions were called. I.e. don't reinvent the wheel...unless you're just having fun ;-) $\endgroup$ – Matt Dec 26 '13 at 23:10
  • $\begingroup$ Probably a better solution to this would be to cast rays from each vertex away from the meshes center and check if they hit anything (if not the vertices are on the outside and can be selected), another solution could be to use BMesh's convex hull operator and find all points which make up the hull. Its not clear exactly what you do but this seems like a very odd solution. $\endgroup$ – ideasman42 Dec 26 '13 at 23:11
3
$\begingroup$

While I think there must be a more elegant way to accomplish what you are trying to do, the problem lies in the fact that Blender isn't updating the scenegraph when it's deep inside the Python loop.

You can force an update and redraw with:

# bpy.types.Scene.update()
bpy.context.scene.update()

and

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

Note: calling bpy.ops.wm.redraw_timer is very much a hack, since scripts are not supposed to be forcing redraws, however it does work.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.