FG tv
FG tv

Reputation: 93

Blender scale value not updating

When I run this script:

import bpy
import math

s = bpy.context.scene.frame_start 
e = bpy.context.scene.frame_end

values = []
print(s)
print(e)

for i in range(s,e):
    bpy.context.scene.frame_current = i
    print(i)
    v = (bpy.context.object.scale[1])
    bpy.context.object.scale[0] = i
    print('At frame ',str(i), ' value ' ,str(v))
    values.extend([bpy.context.object.scale[1]])

It gives me the right number of frames, but the value is always the same, even if the scale[1] of the object is baked to a sound, so it always change between the frames.

It looks like Blender doesn't update the value and it always take the value of the frame during the which the text is ran.

Any way to update the value during the running of the code in real time?

Upvotes: 0

Views: 1007

Answers (1)

sambler
sambler

Reputation: 7079

You are looking at the wrong values.

You have v = scale[1] then set scale[0] = i and then print(v) so your reading scale.y and changing scale.x then looking at scale.y

It is better to use scene.frame_set() to change frames via python.

Another approach to getting a keyed value is to use fcurve.evaluate(frame)

import bpy

s = bpy.context.scene.frame_start 
e = bpy.context.scene.frame_end

values = []
f = bpy.context.object.animation_data.action.fcurves.find('scale', index=1)

for i in range(s,e):
    v = f.evaluate(i)
    print('At frame ',str(i), ' value ' ,str(v))
    values.extend([v])

And if you are planning to keyframe the values, you don't need to change frames as you can specify the frame in keyframe_insert(data_path, frame=f)

obj.keyframe_insert('scale', frame=2)
obj.keyframe_insert('scale', index=1, frame=5) # key only scale.y

Upvotes: 1

Related Questions