How to select vertices when 'select' is not allowed in script due to context?

I was intending to create a script to build a slightly complex shape, so that I could edit the parameters manually to get the dimensions how I want exactly, and try different sizes. The process of building the shape manually involves selecting vertices and then using Extrude_region, selecting vertices, extruding the region again and so-on. I switched on Debug so I could see all the commands including the vertex selection ones, and as carefully as I could, copied the relevant commands into a script file.

However, I found that it runs the first couple of commands, to create a cube and enter edit mode. But when it comes to the first command for selecting a vertex, I get:

RuntimeError: Operator bpy.ops.view3d.select.poll() failed, context is incorrect.

And it does that even when I directly paste the command into the console window.

if I can’t use that command, how do I select vertices in a way that is acceptable to the python script?

Thanks in advance,

Mike Hersee

My bet is you are either new to Python programming in Blender altogether, or a former Maya Python programmer. I say that because, been there, done that. Blender works using context, and there is no way around it, so, what you have to do, is change context, make your selections, then change back to the context you want to be in. It seems a bit daffy, but Blender outperforms Maya by a longshot in my opinion, and I worked with Maya for years before coming to Blender.

Your insight is correct, in fact I’m new to python programming completely (although I used to be a commercial programmer in RPG/400 - chalk and cheese compared to modern programming languages and environments). Thanks for that, I’ll look up how to change context.

Mike

Ok, I’ve had a look about understanding contexts, and I’m not really sufficiently enlighted. I also found this description from Blender documentation and I wonder if this is a limiting factor in this context:
I can’t edit the mesh in edit-mode!Blenders EditMesh is an internal data structure (not saved and not exposed to python), this gives the main annoyance that you need to exit edit-mode to edit the mesh from python.The reason we have not made much attempt to fix this yet is because we will likely move to BMesh mesh API eventually, so any work on the API now will be wasted effort.With the BMesh API we may expose mesh data to python so we can write useful tools in python which are also fast to execute while in edit-mode.For the time being this limitation just has to be worked around but we’re aware its frustrating needs to be addressed.
Does this apply to what I want to be able to do?

Also, I have to say that what I found about setting context so far has not left me with sufficient understanding and knowledge to try specific code. Are you able to make an educated stab at what I might need?

Your support much appreciated.

Mike

I know some ways to do it with Bmesh
have not work a lot with old API in a while now !
but should be possible with bpy.data

can you stick with Bmesh instead
again depends what your are doing
might have to use polygon instead

there are some examples given in the template menu in text editor
also look at released script forum and at blender there is the addons section
and API description it is not a tut but gives some basic examples

happy bl

dont use “bpy.ops.”. They are for use in the GUI of Blender.
For selecting of verts better write something like these:

import bpy

for v in bpy.data.objects["Cube"].data.vertices:
    if v.co.z > 0:
        v.select = 1

When context is incorrect but you really need to use bpy.ops:

bpy.context.area.type = "VIEW_3D" 

and then back to text editor:

bpy.context.area.type = "TEXT_EDITOR"