Skip to main content
added 244 characters in body
Source Link
ansonl
  • 125
  • 5

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:

  1. Select all vertices
  2. 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

  1. Import the model into the file as a mesh
  2. Switch to Scripting view
  3. Select the model in the 3D view or the model list
  4. 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.

selected all

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()

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:

  1. Select all vertices
  2. 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

  1. Import the model into the file as a mesh
  2. Switch to Scripting view
  3. Select the model in the 3D view or the model list
  4. 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.

selected all

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:

  1. Select all vertices
  2. 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

  1. Import the model into the file as a mesh
  2. Switch to Scripting view
  3. Select the model in the 3D view or the model list
  4. 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.

selected all

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()
Source Link
ansonl
  • 125
  • 5

Selecting vertices with Python only works sometimes

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:

  1. Select all vertices
  2. 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

  1. Import the model into the file as a mesh
  2. Switch to Scripting view
  3. Select the model in the 3D view or the model list
  4. 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.

selected all