0

I have a mesh object by different points and How to change the shape of a mesh object by handlers? The mesh object did not change the shape by below script.

vert_list=[(3,5,1),(3,5,5),(3,5,7),(3,5,9),(3,5,11),(3,5,13),(3,5,15)]
edges = [[i - 1, i] for i in range(1, len(vert_list))]
m = bpy.data.meshes.new('line_mesh')
curve_obj = bpy.data.objects.new('line_chart_curve', m)
bpy.context.scene.collection.objects.link(curve_obj)
m.from_pydata(vert_list, edges, [])
m.update()
curve_obj.select_set(True)
bpy.context.view_layer.objects.active = curve_obj

vertslist=[] edgeslist=[]

for i in range(len(vert_list)): verts = vert_list[:i] edges = [[i - 1, i] for i in range(1, len(normalized_vert_list))]
vertslist.append(vert_list.copy())
edgeslist.append(edges.copy())

degp = bpy.context.evaluated_depsgraph_get()

def my_handler(scene,degp): scene = bpy.context.scene i_frame = scene.frame_current if not (0 <= i_frame < len(vert_list)): return

mycurveline=bpy.data.objects['line_chart_curve']    
curveline_eval = mycurveline.evaluated_get(degp)    
me = curveline_eval.data

bm = bmesh.new()
bm.from_mesh(me)

bm.edges.ensure_lookup_table()  

for i,v in enumerate(vertslist[i_frame]):
    bm.verts.new(v)

print(bm.verts[:])
bm.to_mesh(me)
me.update()

bpy.app.handlers.frame_change_post.clear() bpy.app.handlers.frame_change_post.append(my_handler)

Derekcbr
  • 99
  • 1
  • 8
  • Hello, you forgot to add your handler to the handlers. Add bpy.app.handlers.frame_change_post.append(my_handler) – Gorgious Jul 28 '20 at 14:22
  • Thanks for the help! I forgot to post it and actually I did add my_handler and still not working! – Derekcbr Jul 29 '20 at 00:41
  • Is this the intended behaviour ? Your script will add n new verts (n depending on the frame number) every time you scrub along the timeline between frames 1 and 10 ? – Gorgious Jul 29 '20 at 06:33
  • Thank you for your help! I plan to animate the curve object from frames 1 to 10 and the curve will grow as a result. The total frames can be setup by the length of vertslist. Adding new verts is not ok. But I can not figure out how to update the vertices. – Derekcbr Jul 29 '20 at 10:19

1 Answers1

2

Update the mesh of the object, not of the evaluated object

As demonstrated here Updating objects parameters using python creates multiple objects

update the mesh of the object, not its evaluated (for that frame) object.

In addition do not mix context with handlers. The scene is passed as the first argument. This may or may not be the context scene when "handled"..

Only handle objects in the scene being handled..

ob = scene.objects.get("some_name")
if ob:
    ...

bpy.data.objects is a list of all objects in the blend file, which may or may not be linked to any scene, all scenes or none.

Related

How to get the updated vertex coordinates of a cube when rotating?

batFINGER
  • 84,216
  • 10
  • 108
  • 233