Skip to main content
3 of 7
added 1 character in body

Python: Adding F-curve Modifier to Simple Deform Modifier's angle parameter and Adding Random Values in offset parameter of Noise Fmodifier

I have been trying to figure out how to add the Noise fcurve modifier to a Simple Deform's angle parameter. When I attempt to do that, I get an error saying that the context is wrong.

I have also been looking through the documentation to see if I can change the value of Noise's offset to something random for each object selected. Since it's an f-curve modifier, I'm having trouble knowing how to go about that.

I'm new to programming so I'm sorry if this is a simple answer. I've been struggling so I would appreciate any help.

Thank you so much!

def anim_bend(input_name, bend_angle):
    
    obj = bpy.context.active_object
    
    # for each selected object, run this
    for obj in bpy.data.objects:
        if input_name in obj.name:
            
            #select objects individually 
            ob = bpy.context.scene.objects[obj.name]     # get the object
            bpy.ops.object.select_all(action='DESELECT') # deselect all objects
            bpy.context.view_layer.objects.active = ob   # make the current object the active object 
            ob.select_set(True)                          # select the object
            print(obj.name, ": Selected Successfully")
            
            
            modBendName = "SimpleDeform_Bend"
            
            for modifier in obj.modifiers:
                if modifier.name == modBendName:
                    obj.modifiers.remove(obj.modifiers.get(modBendName))
                    print(obj.name, ": DELETED existing", modBendName)
                    
            modBend = obj.modifiers.new(name= modBendName, type='SIMPLE_DEFORM') # add simple Deform to mesh
            modBend.deform_method = 'BEND'                                       # change deform to bend
            modBend.angle = math.radians(bend_angle)                             # set initial angle before adding animated noise
            modBend.keyframe_insert("angle",frame=0)                             # add key at frame 0
            fNoise = bpy.ops.graph.fmodifier_add(type='NOISE')
            

            print(obj.name, ": Added", modBendName)