0
$\begingroup$
  • I'm starting from a mesh in object mode.
  • I insert an animation key (I position for example).
  • I go to the graph editor.
  • I select an f-curve.
  • I add a generator type fmodifier.
  • I choose the start and end frames.

It gives me this in Python:

import bpy

bpy.ops.anim.keyframe_insert_menu (type = 'Rent')
bpy.context.area.type = 'GRAPH_EDITOR'
bpy.ops.graph.fmodifier_add (type = 'GENERATOR')
bpy.context.object.use_restricted_range = True
bpy.context.object.frame_start = 0
bpy.context.object.frame_end = 100

On the other hand, I do not know how to insert these additional lines in python which allow me to choose the degree of the polynomial as well as these coefficients.

I saw in the blender API this function corresponding to what I would like to do: url link: https://docs.blender.org/api/blender_python_api_current/bpy.types.FModifierGenerator.html?highlight=generator#bpy.types.FModifierGenerator

My question: How to correctly insert in the python script of the beginning this FModifierGenerator with its degree and its coefficients? I can not do it ... for example polynomial of degree 1:

1st coefficient = 2
2nd coefficient = 3
$\endgroup$
1
$\begingroup$

I fail to get your example to run, which might be in part because I don't have that Keying Set Rent available, partly because it seems as if bpy.context.object in your code refers to the object and not the FCurve, so I don't know how it should access an attribute to the FCurve like use_restricted_range

Anyway, the attributes for the FModifierGenerator are
mode which is either POLYNOMIAL or POLYNOMIAL_FACTORIZED,
poly_order and
coefficients which is a list of length poly_order+1 for a polynomial or poly_order * 2 for a factorized polynomial.

The following code adds a keyframe to the X location of the active object and adds a function modifier generator with polynomial order and coefficients set.

import bpy

obj = bpy.context.active_object
obj.keyframe_insert(data_path='location', index=0)  # X Location

fcurve = obj.animation_data.action.fcurves.find(data_path='location', index=0)
mod = fcurve.modifiers.new(type='GENERATOR')
mod.mode='POLYNOMIAL'
mod.poly_order = 1
mod.coefficients = [2, 3]
mod.use_restricted_range = True
mod.frame_start = 0
mod.frame_end = 100

If your code runs in your blender, then adding something like bpy.context.object.poly_order = 1, etc., should do the trick. Otherwise you have to find the FCurve of your keyframe channel with the correct data path according to your keying set and and modify its attributes like above.

$\endgroup$

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.