I want to be able to run a function like Space in Loop Tools from the script editor. Only extracted functions related to
bl_idname = "mesh.looptools_space"
bl_label = "Space"
I have extracted only the functions related to
operator panel is not needed and is fixed with bpy.ops.mesh.looptools_space(influence=100, input='selected', interpolation='cubic', lock_x=False, lock_y=False, lock_z= False)
and converted it to run from a text editor, but it does not work correctly
Can you please tell me what I should do?
The images below are from the script and "Space" from the Loop tools. The edge from the "script" does not have the vertex position changed, and only the rightmost one has been shrunk I want to position the vertices evenly as when I execute the "Space" button
import bmesh
import mathutils
def move_verts(object, bm, mapping, move, lock, influence):
for i, (index, location) in enumerate(move):
vert = bm.verts[mapping[index]]
if lock:
if lock[0]:
location.x = vert.co.x
if lock[2]:
location.y = vert.co.y
if lock[2]:
location.z = vert.co.z
vert.co = vert.co.lerp(location, influence / 100)
def initialise():
object = bpy.context.object
bm = bmesh.from_edit_mesh(object.data)
return object, bm
def terminate():
bmesh.update_edit_mesh(bpy.context.object.data)
def space_calculate_t(bm_mod, points):
# Dummy implementation
return points, points
def calculate_splines(interpolation, bm_mod, tknots, knots):
splines = []
if interpolation == 'cubic':
for i in range(len(knots) - 1):
a = bm_mod.verts[knots[i]].co
b = bm_mod.verts[knots[i + 1]].co
d = b - a
t = tknots[i]
u = tknots[i + 1] - t
splines.append([[a.x, 0, 0, 0, t], [a.y, 0, 0, 0, t], [a.z, 0, 0, 0, t]])
else:
for i in range(len(knots) - 1):
a = bm_mod.verts[knots[i]].co
b = bm_mod.verts[knots[i + 1]].co
d = b - a
t = tknots[i]
u = tknots[i + 1] - t
splines.append([a, d, t, u])
return splines
def space_calculate_verts(bm_mod, interpolation, tknots, tpoints, points, splines):
move = []
for p in points:
m = tpoints[points.index(p)]
if m in tknots:
n = tknots.index(m)
else:
t = tknots[:]
t.append(m)
t.sort()
n = t.index(m) - 1
if n > len(splines) - 1:
n = len(splines) - 1
elif n < 0:
n = 0
if interpolation == 'cubic':
ax, bx, cx, dx, tx = splines[n][0]
x = ax + bx * (m - tx) + cx * (m - tx) ** 2 + dx * (m - tx) ** 3
ay, by, cy, dy, ty = splines[n][2]
y = ay + by * (m - ty) + cy * (m - ty) ** 2 + dy * (m - ty) ** 3
az, bz, cz, dz, tz = splines[n][2]
z = az + bz * (m - tz) + cz * (m - tz) ** 2 + dz * (m - tz) ** 3
move.append([p, mathutils.Vector([x, y, z])])
else: # interpolation == 'linear'
a, d, t, u = splines[n]
move.append([p, ((m - t) / u) * d + a])
return move
def space_operator():
object, bm = initialise()
# Example setup for testing purposes
input = 'selected'
interpolation = 'cubic'
lock_x = False
lock_y = False
lock_z = False
influence = 100
# Dummy implementation of cache and loops (to be replaced with actual logic)
loops = [[v.index for v in bm.verts if v.select]]
mapping = {v.index: v.index for v in bm.verts}
move = []
for loop in loops:
tknots, tpoints = space_calculate_t(bm, loop)
splines = calculate_splines(interpolation, bm, tknots, loop)
move.append(space_calculate_verts(bm, interpolation, tknots, tpoints, loop, splines))
if lock_x or lock_y or lock_z:
lock = [lock_x, lock_y, lock_z]
else:
lock = False
move_verts(object, bm, mapping, move[0], lock, influence)
terminate()
# Example call (to be executed in Blender's Text Editor)
space_operator()