3
$\begingroup$

I have a vector and vertices, and I'm trying to align the vertices to that vector using 'Translation Matrix', unfortunately multiplying the matrix with the vertices' coordinates do not change anything.

here is the code:

import bpy
import bmesh
import mathutils
import math

msh = bpy.context.object.data
mesh = bmesh.from_edit_mesh(bpy.context.active_object.data)

vec = mathutils.Vector((0.7071, -0.7071, 0.0, 1.0))
trans = mathutils.Matrix.Translation(vec)

m1 = mesh.verts.new((1, 0, 0))
m2 = mesh.verts.new((1.5, 0, 0))
m3 = mesh.verts.new((2, 0, 0))

m1.co = m1.co * trans
m2.co = m2.co * trans
m3.co = m3.co * trans

bmesh.update_edit_mesh(msh)

enter image description here

Thanks in advance.

$\endgroup$
2
$\begingroup$

Unit direction vector.

Suggest doing this without the transform matrix. The direction vector

>>> d = Vector((1, -1, 0))

can be normalized, ie have a length of one, to

>>> d.normalize() # in place normalize
Vector((0.7071067690849304, -0.7071067690849304, 0.0))

Now, given a source location point p can move from p x units in the direction d using

new_loc = p + x * d  # x is a scalar.

In the context of your original question

import bpy
import bmesh
from mathutils import Vector

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

p = Vector() # using origin as source point.

vec = Vector((1, -1, 0))  # direction vector
d = vec.normalized()  # unit direction vector

newverts = [bm.verts.new(p + x * d) for x in (1, 1.5, 2)]

bmesh.update_edit_mesh(me)
$\endgroup$
  • $\begingroup$ Note: if the vertices did not align with the vector after using this code, then you need to multiply the 'Unit direction vector' noted as 'd' in the code above with the World matrix of the object: bpy.context.active_object.matrix_world $\endgroup$ – user48739 Aug 15 '18 at 8:15
  • $\begingroup$ @user48739 vertices use local coordinates. The code above assumes the vector, and points along it, are defined in local space. To place verts along a vector with global coordinates convert direction and position vectors to local space via local_space = mw.inverted() * global_space Calculate the new local direction vector via the local space difference using two known global points $\endgroup$ – batFINGER Aug 15 '18 at 9:13
0
$\begingroup$

Put the matrix to the left of the multiply -

m1.co = trans * m1.co

Note that you are translating each vertex by the same amount, that will move them parallel to their original positions, not place them on the line based on the vector.

Given the same starting position, you can apply the same transform multiple times to get the vertices into a line.

vec = mathutils.Vector((0.7071, -0.7071, 0.0))
trans = mathutils.Matrix.Translation(vec)

m1 = mesh.verts.new((1, 0, 0))
m2 = mesh.verts.new((1, 0, 0))
m3 = mesh.verts.new((1, 0, 0))

m1.co = trans * m1.co
m2.co = trans * 2 * m2.co
m3.co = trans * 3 * m3.co
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.