2
$\begingroup$

I'm writing a function that should return the frame when a field reaches a certain value in the property window.

To be more specific here the function:

def getFrameFromPercentagePosition(context, pCurveObject, pPercentagePosition):
    context.scene.frame_current = 1
    while pCurveObject.data.bevel_factor_end < pPercentagePosition:
        context.scene.update()
        context.scene.frame_current += 1
    return context.scene.frame_current

The field bevel_factor_end is animated between 0 and 1 and I want to know the frame when its value reaches the pPercentagePosition value.

But, after some debugging, I've found out that pCurveObject.data.bevel_factor_end is always 1.0 in this script. However, when I do manually in the interactive console by asking the bevel_factor_end of the active object, it gives me the right value.

How can I make sure that a got the correct value for this property?

$\endgroup$

1 Answer 1

2
$\begingroup$

When using python to alter the current frame you should use scene.frame_set(f) which will update the scene so you can see animated values as they are at that frame.

A possible logic error in your code - you mention the value is animated between 0 and 1, if frame 1 is keyed to 0.0 then frame 1 will always return as being less than the tested value. You may want to consider a range that is acceptable to matching the value.

if val > (target - 0.1) and val < (target + 0.1):
    return frame

You should also find fcurve.evaluate(f) to be an easier and possibly faster solution as you don't need to update the scene to get the curve value.

def getFrameFromPercentagePosition(context, pCurveObject, pPercentagePosition):
    scn = context.scene
    fcurve = pCurveObject.data.animation_data.action.fcurves.find('bevel_factor_end')
    variance = 0.05
    for f in range(scn.frame_start, scn.frame_end):
        val = fcurve.evaluate(f)
        if val > (pPercentagePosition - variance) and val < (pPercentagePosition + variance):
            return f
$\endgroup$
1
  • $\begingroup$ It works like a charm with scene.frame_set(). Thanks a lot, that's perfect ! And you're right, it is much faster and logical to evaluate the F-Curve directly, I'll do it that way. Thanks again ! :) $\endgroup$ Dec 4, 2015 at 21:38

You must log in to answer this question.

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