3
$\begingroup$

everyone!

import bpy

context = bpy.context

areatype = context.area.type
context.area.type = 'GRAPH_EDITOR' 
ob = context.object
action = ob.animation_data.action
bpy.ops.graph.fmodifier_add(type='CYCLES')
context.area.type = areatype

Graph Editor showing modifiers and selections In the above code The above code only responds to the active f-curve. I need to access other channels with a script. Is there any way to adapt the cycle modifie?


https://blender.stackexchange.com/a/242554/89484

On lemon's advice, I was able to adapt the cycle modifier.

It just didn't work for some reason. Once I made a meaningless move in the graph editor, the cycle modifier was successfully applied.

Here is the code.

import bpy

o=bpy.context.object
action = o.animation_data.action
for c in action.fcurves: m = c.modifiers.new('CYCLES')




context = bpy.context
areatype = context.area.type
context.area.type = 'GRAPH_EDITOR' 
ob = context.object
action = ob.animation_data.action

#This is a meaningless operation.
bpy.ops.anim.channels_setting_enable(type='PROTECT')
bpy.ops.anim.channels_setting_disable(type='PROTECT')

context.area.type = areatype
$\endgroup$

2 Answers 2

1
$\begingroup$

There's one 'gotcha' with the Cycles modifier. It has to be the first modifier in the fcurve modifier stack. Here's a modified version of the script that generates an error message if the there are modifiers on the stack but there isn't a Cycles modifier. I looked into pulling all of the other modifiers and then replacing them, but that's a lot of code, and perhaps the error message is enough?

import bpy

def enable_cycles_modifiers(action):
    """ Enable the cycles modifier on all F-Curves in this action
        except those that already have modifiers, because the
        cycles modifier must be first.
    """
    for fcurve in action.fcurves:
        modifiers = fcurve.modifiers

        if not len(modifiers):
            modifier = modifiers.new('CYCLES')
            continue
        
        if modifiers[0].type == 'CYCLES':
            continue
        
        print(f'Cannot add Cycles modifier to {action.name} F-curve {fcurve.data_path}[{fcurve.array_index}]')

            
object = bpy.context.object
if object and object.animation_data:
    action = object.animation_data.action
    enable_cycles_modifiers(action)
    
scene = bpy.context.scene
scene.frame_set(scene.frame_current)
```
$\endgroup$
4
  • $\begingroup$ Thank you. Marty Fouts. I was able to adapt the cycle modifier in your script as well. However, I was never able to get the animation to actually work. Is this a bug? i.imgur.com/DFK8jCG.gif $\endgroup$
    – mml
    Commented Nov 9, 2021 at 1:59
  • $\begingroup$ what specifically still isn't working? Is it worth asking another question? $\endgroup$ Commented Nov 9, 2021 at 4:17
  • $\begingroup$ Check the GIF animation of the link. Although the cycle modifier has been adapted. However, the actual animation doesn't work for some reason.imgur.com/DFK8jCG $\endgroup$
    – mml
    Commented Nov 9, 2021 at 5:19
  • $\begingroup$ Your script worked fine! Thanks for the nice function! Was saved! $\endgroup$
    – mml
    Commented Nov 24, 2021 at 4:38
6
$\begingroup$

You can do something like that using bpy.types apis:

action = o.animation_data.action
for c in action.fcurves: m = c.modifiers.new('CYCLES')

m is the modifier object that you can use to change its parameters.

The documentation is here and here.

To force updates, you can add somthing like this which reset the current frame:

s = bpy.context.scene
s.frame_set(s.frame_current)
$\endgroup$
1
  • $\begingroup$ Thanks! But for some reason, the cycle doesn't get triggered until I take action in the graph editor once...? imgur.com/NosPRja $\endgroup$
    – mml
    Commented Nov 7, 2021 at 2:56

You must log in to answer this question.

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