0
$\begingroup$

i'm stuck here, i'm trying to evaluate a newly created FCurve to get the current value of associated property, and keep getting "0". Here's an example working code with scale:

import bpy
# getting the object
obj = bpy.context.object
# the active object have action but no scale fcurve
# let's create it for x axis
fc = obj.animation_data.action.fcurves.new("scale", index=0)
# evaluate the current frame
value = fc.evaluate(bpy.context.scene.frame_current)
# print the result
print("value is %f" % value)

This returns "0", but the active object is "1" in x scale (code can be run from console with selected object with action and no scale keyframe to test)

I don't really know how to get this "1" value programatically with object and data_path only. The getattr() method could work, but i'm also using this code on pose bones, with these kinds of datapath : 'pose.bones["Bone"].location[0]' and this doesn't seem to work with getattr()

Any clue out there ? thanks in advance guys ! cheers !

$\endgroup$

2 Answers 2

1
$\begingroup$

Get the object from the path.

Can get the object from the datapath via (with and without coercing to the value)

>>> C.object.id_data.path_resolve('scale')
Vector((1.0, 1.0, 1.0))

>>> C.object.id_data.path_resolve('scale', False)
bpy.data.objects['Vert.001'].scale

can look at the last part of the path and check whether that property is an array,

>>> C.object.bl_rna.properties['scale'].is_array
True

however it is possibly easier to try and iter and if not use the index.

datapath = 'scale'
index = 0
frame = bpy.context.scene.frame_current

if not (len(fc.keyframe_points) or len(fc.sampled_points)):
    ob = obj.id_data.path_resolve('scale')
    try:
        iter(ob)
        value = ob[index]
    except TypeError:
        value = ob
else:
    value = fcurve.evaluate(frame)        

print(value)
$\endgroup$
1
  • $\begingroup$ perfect ! works like a charm, i am not familiar with path_resolve() method, seems pretty handy ! Thanks a lot ! just a question though, what is the use of iter(ob) in this case ? $\endgroup$
    – tonton
    Commented Mar 5, 2021 at 18:03
1
$\begingroup$

Here's the function i end up with thanks to @batFINGER answer if anyone has the same problem :

# process original value
def process_original_value(parent, fcurve, data_path, array_index, current_frame):

    if not fcurve.keyframe_points:
        data = parent.id_data.path_resolve(data_path)
        try:
            value = data[array_index]
        except TypeError:
            value = data
    else:
        value = fcurve.evaluate(current_frame)

    return value

cheers

$\endgroup$

You must log in to answer this question.

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