0
$\begingroup$

I tried to use handlers to update the vertex coordinates but failed. Please have a look at below code:

bpy.ops.mesh.primitive_cube_add(size=1,location=(0,0,1))
current_scene = bpy.context.scene
#default_cube = current_scene.objects['Cube']
default_cube = bpy.context.object  

degp = bpy.context.evaluated_depsgraph_get()

def update_scene(current_scene):
    cube_eval = default_cube.evaluated_get(degp)
    cube_mesh_eval = cube_eval.data  
    current_frame_value = current_scene.frame_current
    default_cube.rotation_euler.z=180/120*current_frame_value
    print(current_frame_value)

    for v in  cube_mesh_eval.vertices:
        print(v.co) 
#clear the post frame handler
bpy.app.handlers.frame_change_post.clear()
   bpy.app.handlers.frame_change_pre.append(update_scene)
$\endgroup$

1 Answer 1

2
$\begingroup$

Changing verts to match those of other objects in handler.

To linearly rotate an object in a scene, suggest that a driver is possibly the simplest. How do I make animations a steady speed?

Given your prior question How to update the connection shortest line between the vertice of a cube and an Ico sphere? and my answer to it

I have added a simple 2 vert object named "Edge" for example below it is parented to the cube (not vertex parented)

The frame change handler sets the location of the edge object to a random cube vert, and sets the other end to a randomly chosen vert on the depsgraph evaluated ico sphere. Since the edge mesh has been object parented to the cube mesh, will work in the cube object's object coordinate space. .

When using get to get an object it will return None if no object with that name exists in the objects of the scene passed to the handler.

An evaluated depsgraph is also available as the second argument of a post frame change handler.

enter image description here Scrubbing the timeline to test handler, notice both ends are always at vert locations. The cube is animated via keyframe (as per prior)

import bpy
from random import choice

def update_scene(scene, depsgraph):
    edge = scene.objects.get("Edge.004")
    #edge = edge.evaluated_get(depsgraph)
    cube = scene.objects.get("Cube")
    ico = scene.objects.get("Icosphere")
    imw = ico.matrix_world
    cmwi = cube.matrix_world.inverted()
    # if wish to use evaluted 
    ico = ico.evaluated_get(depsgraph)
    v0 = choice([v.co for v in cube.data.vertices])
    v1 = choice([v.co for v in ico.data.vertices])
    edge.location = v0
    #edge.data.vertices[1].co = v0
    edge.data.vertices[1].co = cmwi @ (imw @ v1) -  v0

Replace the random choice with the kdtree code from your other question. Could make the kdtree outside the handler method. Recommend tho don't reference objects from outside and use within the handler, always get from the scene objects via some test.

Rather than altering vertex coordinates, another method would be to make a unit length edge, arbitrarily in Z direction, and position and rotate based on end points... more akin to a stretch_to bone constraint.

$\endgroup$
1
  • $\begingroup$ You are the legend!Thanks a lot! $\endgroup$
    – Derekcbr
    Jun 22, 2020 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .