0

My goal here is to select three vertices and manually create a face using bpy.ops.mesh.edge_face_add(). However, when I run the following code, no vertices are actually selected. I have been using this (How do I select specific vertices in blender using python script?) StackExchange question and answer to no avail. Sometimes my script will select one vertex, sometimes it will not. It never selects more than one. current_min_vert and the others are MeshVertex class objects, and should be equivalent to obj.data.vertices[n], where 'n' is any prescribed integer. Here is the code:

# Join meshes into one        
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.join()

Set selection mode

bpy.ops.object.mode_set(mode = 'EDIT') bpy.ops.mesh.select_mode(type="VERT") bpy.ops.mesh.select_all(action = 'DESELECT')

Select vertices to form a triangle

bpy.ops.object.mode_set(mode = 'OBJECT') current_min_vert_copy.select = True current_min_vert.select = True current_max_vert.select = True bpy.ops.object.mode_set(mode = 'EDIT')

The following image is my end goal. The top right vertex is current_min_vert_copy, the bottom right is current_min_vert, and the bottom left is current_max_vert.

The end-goal.

Any assistance is greatly appreciated.

Frances
  • 3
  • 2
  • 1
    Where do you define current_min_vert? The problem might lie here. – BlenderEnthusiast Oct 12 '23 at 19:35
  • @BlenderEnthusiast I define my current_max and current_min verts while looping through all the vertices of my mesh object. When I print out the value of current_min_vert and others, a MeshVertex class object is printed to the command window, indicating that Blender probably recognizes it as a vertex of my mesh. current_max = np.abs(vertex.co.x); current_max_vert = mesh_object.vertices[i]

    EDIT: My for loop was ill-formatted, so I replaced it with a simpler version.

    – Frances Oct 12 '23 at 19:51
  • Please always provide a minimal working example, otherwise it's hard to give a definitive answer. – Markus von Broady Oct 12 '23 at 20:09

1 Answers1

0

Blender Python objects are short-lived and can be invalidated by various things like ✲ CtrlZ undo or switching between Edit and Object modes…

Rather than assigning chosen vertices to your variables, assign their indices to your variables, and then change these lines:

current_min_vert_copy.select = True
current_min_vert.select = True
current_max_vert.select = True

to

me = D.objects["name_of_object"].data
me.vertices[current_min_vert_copy].select = True
me.vertices[current_min_vert].select = True
me.vertices[current_max_vert].select = True
Markus von Broady
  • 36,563
  • 3
  • 30
  • 99