Align Vertex Position

I am surprise there isn’t a script for this yet. I did do a search but didn’t really get anything that matched.

I need a simple script. I have 3 vertices in this example but in reality I’ll have many vertices selected. I select the first 2 vertices and then select the last vertex. I want to move the first 2 vertices to the last selected vertex Z position.

A second variation of this script I need is the same requirements, but not just Z but also X position.

Set your pivot point to Active Element. Select all the relevant vertices, selecting the one at the target position last. Press S → Z → 0.

2 Likes

This works and I am aware of it but I am trying to script this…ChatGPT didn’t work out lol.

import bpy
import bmesh

def vert_selected_to_active(bm, axis):
    
    av = bm.select_history.active
    
    if(not isinstance(av, bmesh.types.BMVert)):
        return
    
    for v in bm.select_history:
        if(isinstance(v, bmesh.types.BMVert)):
            match axis:
                case 'x':
                    v.co.x = av.co.x
                case 'y':
                    v.co.y = av.co.y
                case 'z':
                    v.co.z = av.co.z
         
    bmesh.update_edit_mesh(mesh)
    

object = bpy.context.object
mesh = object.data
bm = bmesh.from_edit_mesh(mesh)

vert_selected_to_active(bm, 'z')
1 Like

Thank you it works! I’m going to study it and see how it works :slight_smile: I wish there was more video tutorials for artist on bmesh. Hell I would even pay for such a tutorial!

Edit: A caveat, this is more related to the nature of selections in Blender than the script. I am using lasso selection by default, for this script to work you have to click on the vertices, lassoing the vertices will cause the script to not work.

If you lasso select, you don’t have an active vertex. In that case, where would you expect / want to snap to?

Sometimes I have many vertices I lasso select first, then even if I click and select the last vert(active vertex(maybe it’s not consider a active selection?)), the script won’t work. Not a big deal, just something people need to be aware of. I guess it’s more of a Blender thing I am not used to coming from Modo.

1 Like

In that case, try this ammended script. It should always work as long as there is an active vert. However, it will be slower as the mesh gets larger.

import bpy
import bmesh

def vert_selected_to_active(bm, axis):
    
    av = bm.select_history.active
    
    if(not isinstance(av, bmesh.types.BMVert)):
        return
    
    selected_verts = [v for v in bm.verts if v.select]
    
    for v in selected_verts:
        match axis:
            case 'x':
                v.co.x = av.co.x
            case 'y':
                v.co.y = av.co.y
            case 'z':
                v.co.z = av.co.z
         
    bmesh.update_edit_mesh(mesh)

object = bpy.context.object
mesh = object.data
bm = bmesh.from_edit_mesh(mesh)

vert_selected_to_active(bm, 'z')
1 Like

Thank you, this is a good compromise :partying_face: and it works.