Keyframe insert for python smoke modifier setting: density

I’m looking for for the correct way to use python to insert a keyframe for a smoke flow object’s density setting.

I tried this but the console error says “density” is not found:

myDensity = (100 - obj.location.z) / 100
obj.modifiers[-1].flow_settings.density = myDensity
obj.modifiers[-1].keyframe_insert(data_path=“density”)

Error: bpy.struct.keyframe_insert() property “density” not found.

What is the right way to do this? Thanks.

If you keyframe it manually, then check the data_path:

>>> C.object.animation_data.action.fcurves[0].data_path
‘modifiers[“Smoke”].flow_settings.density’

then you see that the modifier is part of it.

You need to call keyframe_insert like:
C.object.keyframe_insert(data_path=‘modifiers[“Smoke”].flow_settings.density’)

You may use path_from_id() to get the data_path:

>>> C.object.modifiers[‘Smoke’].flow_settings.path_from_id(“density”)
‘modifiers[“Smoke”].flow_settings.density’

Thanks CoDEmanX! You saved the day.

If you are coding you can


>>> C.object.modifiers['Smoke'].flow_settings.keyframe_insert('density')
True

Which as a matter of preference IMO reads more nicely than


C.object.keyframe_insert(data_path='modifiers["Smoke"].flow_settings.density')

you were trying to keyframe density on the modifier, rather than modifier.flow_settings, hence the error.

As CoDEmanX pointed out the fcurve is on the object and the fcurve datapath will be relative from there.