When I use Blender Python to loop through all vertices to select only vertices with a Z coordinate <= 0, the script seems to randomly do one of the following:
- Select all vertices
- Select the vertices with a Z <= 0
Does the object need to be selected a certain way in the UI or through Python before looping through the vertices to get the correct selection behavior?
The Python I am using is
import bpy
for v in bpy.context.active_object.data.vertices:
v.select = v.co.z <= 0
The steps I do are
- Import the model into the file as a mesh
- Switch to Scripting view
- Select the model in the 3D view or the model list
- Run the script
When tabbing the 3D view to Edit Mode, sometimes the wrong set of vertices is selected like seen in the screenshot.
The current object
returns the correct object when typed into the console in the screenshot.
Bmesh version of the script
import bpy, bmesh
me = bpy.context.object.data
bm = bmesh.new()
bm.from_mesh(me)
bm.select_flush(True)
for v in bm.verts:
v.select_set(v.co.z <= 0)
bm.to_mesh(me)
bm.free()