0
$\begingroup$

My problem is get the "angle" for updating the expression inside , in this example the expression is a simple string for explain , but i want controll expression by floatprop(that is not a problem) problem is how to get.

import bpy

def update_the_driver():
    ###this funcition is because driver expression is controlling by some variable
    ob = bpy.context.object

    bend = ob.modifiers.get("bend")
    fcurve = getattr(bend, "angle")####Problem
    driver = fcurve.driver####problem

    driver.expression = "sin(frame/20)"###problem

    return

def make_driver():
    ###this is because i call by a boolean on

    ###some check for presence about modifier name
    #if my_bool is True:
        ob = bpy.context.object
        bend = ob.modifiers.new(name="bend", type='SIMPLE_DEFORM')
        bend.deform_method = 'BEND'

        bend.driver_add("angle")

        update_the_driver()
    #if my_bool is False:

        ###delete diver, and modifier.

    return

make_driver()

#####some controller to call make_driver and update the drivers
$\endgroup$
1
$\begingroup$

To get the driver

Py console code. Look at the drivers collection in the animation data. (animation data can be None need to test)

Default file with cube selected and active. Cube has a deform modifier mod name "SimpleDeform". The "angle" property of the modifier has driver. One component of the cubes location has a driver.

>>> ob = C.object
>>> for fcurve in ob.animation_data.drivers:
...     fcurve.data_path
...     
'location'
'modifiers["SimpleDeform"].angle'

Just as we added the driver to the modifier, we can get the path from the ID object (in this case bpy.types.Object type context object)

>>> mod =  C.object.modifiers['SimpleDeform']

>>> mod.id_data
bpy.data.objects['Cube']

>>> mod.id_data is C.object
True

>>> mod.path_from_id("angle")
'modifiers["SimpleDeform"].angle'

This is our "key" to finding driver

>>> key = 'modifiers["SimpleDeform"].angle'
>>> fcurve = C.object.animation_data.drivers.find(key)
>>> fcurve
bpy.data.objects['Cube']...FCurve

>>> fcurve.driver.expression
'var + 0.785'

>>> key = "badkey"
>>> C.object.animation_data.drivers.find(key) is None
True

Here is a helper method, pass an object which returns the angle property driver of a deform modifier named modname or None

def get_deform_angle_driver(ob, modname):
    # does ob have modifiers
    if not hasattr(ob, "modifiers"):
        return None
    # does ob have animation data
    ad = ob.animation_data
    if not ad:
        return None
    # does ob have a modifier of that name
    bend = ob.modifiers.get(modname)
    if not bend:
        return None
    # make the key
    key = bend.path_from_id("angle")
    #key = 'modifiers["%s"].angle' % modname
    fcurve = ad.drivers.find(key)
    # is there a driver fcurve?
    if not fcurve:
        return None
    # woo hoo.. return the drive fcurve
    driver = fcurve.driver

    #driver.expression = "sin(frame/20)"

    return fcurve

# test code
import bpy
ob = bpy.context.object
fcurve = get_deform_angle_driver(ob, "bend")
print(fcurve)
$\endgroup$
  • $\begingroup$ Sorry bat , im confused at this round, if i use : fcurve = bpy.context.object.animation_data.drivers.find(key) give me error, is that the way to find custom named modifier? just for not get another modifier with same "Simpledeform"... I'm not very good on the console $\endgroup$ – Pastrokkio Mar 8 at 21:25
  • 1
    $\begingroup$ key = 'modifiers["%s"].angle' % mod.name` So if your modifier mod has name "bend" the key is 'modifiers["bend"].angle' $\endgroup$ – batFINGER Mar 8 at 22:17
  • $\begingroup$ fcurve = bpy.context.object.animation_data.drivers.find(key) print(fcurve) ... None... not undertand why the name is exactly $\endgroup$ – Pastrokkio Mar 8 at 22:37
  • $\begingroup$ Sorry bat, undertand my problem in the first time i put "modifiers['bend'].angle" ( quotation marks error) now work ! $\endgroup$ – Pastrokkio Mar 8 at 22:44

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.