5
$\begingroup$

I have an issue where I can’t find the current location of a cubes’ global vertex location. The cube is moved and key framed through code, but when I try to find the location (of any vertex) at each subsequent frame, it always returns the location of the cubes’ vertex at the start frame. Follow these steps to replicate the error:

Create a cube called Cube.001


import bpy
import random
Scene_Name = bpy.context.scene.name

def Move(Name):
    bpy.context.object.location[0] = bpy.context.object.location[0] + 2  

def MovingVertex(): 
    C = bpy.context
    aObj = C.active_object

    if aObj == None:
        print('No Mesh seleted')
        return
    vts = aObj.data.vertices
    for count in range(0, 8):
        x = [i.index for i in vts if i.select==True][3]
        if x == None:
            print('1 Vertex selected at least')
            return
        wm = aObj.matrix_world.copy()
        aP = wm * vts[x].co

    print(bpy.context.scene.frame_current)
    print(aP) 

for frame in range(1, 100):
    bpy.data.scenes[Scene_Name].frame_current = (bpy.data.scenes[Scene_Name].frame_current + 1)
    for Count in  range(1, 2):
        Name = "Cube." + str(format(Count, '03'))
        ob = bpy.data.objects[Name]
        bpy.context.scene.objects.active=ob
        Move(Name)
        MovingVertex()
        cf = bpy.data.scenes[Scene_Name].frame_current
        ob.keyframe_insert(data_path='location', frame=(cf))

Output of frame number and cubes’ vertex location:

1682
<Vector (3168.8081, -1.3707, -2.5711)>
1683
<Vector (3168.8081, -1.3707, -2.5711)>
1684
<Vector (3168.8081, -1.3707, -2.5711)>
1685
<Vector (3168.8081, -1.3707, -2.5711)>
1686
<Vector (3168.8081, -1.3707, -2.5711)>

What am I doing wrong, how can I get the location of the cubes’ vertex at a given frame?

$\endgroup$
5
$\begingroup$

Instead of setting scene.frame_current = frame, run scene.frame_set(frame),

The docs mention this since its a common issue.

Updating all objects animation state in a scene can be very heavy with complex scenes, so it was decided not to do this on setting the frame attribute.

$\endgroup$
  • $\begingroup$ if the animation is not keyframed this still won't work, see the answer to blender.stackexchange.com/questions/28031/… $\endgroup$ – rfabbri Sep 8 '15 at 7:13
  • $\begingroup$ @rfabbri, its orthogonal, weather you access input loc/scale/rot or final transformation is up to you, and unrelated to frame changing. $\endgroup$ – ideasman42 Sep 8 '15 at 7:35
  • $\begingroup$ (I'm a noob trying to understand this) I meant the animation type; if it were using constraints instead of keyframes, frame_set alone wouldn't work. See the comments by Chebhou to this question blender.stackexchange.com/questions/33377/… $\endgroup$ – rfabbri Sep 8 '15 at 7:57
  • $\begingroup$ @rfabbri, your confusing 2 unrelated issues. You can get the input trasform, or final output transform. Which you access depends on what you do. But to say "frame_set alone wouldn't work" is incorrect. Since you imply one value is wrong and the other is right. If you need the final transformation, then you better access that, irrespective on the data being animated or not. $\endgroup$ – ideasman42 Sep 8 '15 at 8:03

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.