I'm new to bpy coding and I'm now trying to move some of the vertices on my object. I've tried with directly assigning new coordinates to vertex.co. But whatever I tried, the nodes just stuck there without any change.
Here's my code, running on blender 3.6. Just assigning values from .npy file to the vertices. Something very basic, right? (by the way, please ignore the non-ASCII Chinese tags. I'm sure they have nothing to do with the problem)
import bpy
import numpy as np
obj = bpy.data.objects['裙子4-1']
start_idx = 0
group_idxs = [vert.index for vert in obj.data.vertices if obj.vertex_groups["腰松紧"].index in [g.group for g in vert.groups]]
vert_coors = [[i, list(obj.data.vertices[i].co), 0.] for i in group_idxs]
vert_coors_pos, vert_coors_neg, vert_coors_pos_, vert_coors_neg_ = [], [], [], []
for i in vert_coors:
if i[1][0] == 0.:
if i[1][1] > 0: vert_coors_pos_.append([i[0], i[1]])
else: vert_coors_neg_.append([i[0], i[1]])
continue
tg = i[1][1] / i[1][0]
if i[1][0] > 0:
vert_coors_pos.append([i[0], i[1], tg])
else:
vert_coors_neg.append([i[0], i[1], tg])
vert_coors_pos = sorted(vert_coors_pos, key = lambda item: item[2])
vert_coors_neg = sorted(vert_coors_neg, key = lambda item: item[2])
vert_coors = [i[:2] for i in vert_coors_pos] + vert_coors_pos_ + [i[:2] for i in vert_coors_neg] + vert_coors_neg_
for i, item in enumerate(vert_coors):
if item[0] == start_idx: break
vert_idxs = np.array([item[0] for item in vert_coors[i:] + vert_coors[:i]])
tar_co = np.load("腰线.npy")
n = len(tar_co)
tar_co = list(tar_co)
tar_co.append(tar_co[0])
tar_co = np.array(tar_co)
'''The changing operation starts here. Lines above are for data preparation. '''
for i in range(n):
obj.data.vertices[vert_idxs[4 * i]].co = tuple(tar_co[i])
obj.data.vertices[vert_idxs[4 * i + 1]].co = tuple(tar_co[i] * .75 + tar_co[i + 1] * .25)
obj.data.vertices[vert_idxs[4 * i + 2]].co = tuple(tar_co[i] * .5 + tar_co[i + 1] * .5)
obj.data.vertices[vert_idxs[4 * i + 3]].co = tuple(tar_co[i] * .25 + tar_co[i + 1] * .75)
I've found this answer: How can I reassign or move vertexes of a vertex group. I don't prefer the bmesh method for I don't want to enter edit mode, if possible. I really think there's nothing different in method between my code and the "vgVerts" method in the first answer, but the fact is that my code isn't working.
So again, my questions are: Is it posible to move the verticies directly by changing their .co variables? And, can anyone clarify what's the mistake in my code (to avoid me from stucking in it again)?