Add Distance driver to emission strength

I would like to add a distance driver to a materiel emission node using python. I have tried several things to no avail, can someone please give me some guidance.

As the Suzanne object move along the path, i want the distance between Suzanne and one of the bulbs to control the emission strength

String of Flashing Lights 02.blend (617.4 KB)

Here is what i have, it creates a driver, but the driver does not show in the graph editor. I have attached the blend file.

    nodes = newMat.node_tree.nodes
    # Remove default
    nodes.remove(newMat.node_tree.nodes.get('Diffuse BSDF'))
    material_output = nodes.get('Material Output')
    emission = nodes.new('ShaderNodeEmission')
    emission.inputs[0].default_value = (1,0,0,1)
    emission.inputs[1].default_value = 1.0
    # link emission shader to material
    newMat.node_tree.links.new(material_output.inputs[0], emission.outputs[0])
    
    driver = emission.inputs[1].driver_add("default_value")
    var = driver.driver.variables.new()
    var.name = "variable"
    var.type = 'LOC_DIFF'
    #var.expression = "var<0.50"
    var.targets[0].data_path = "PATH"
    var.targets[0].id = tracker_obj
1 Like

Not sure of the python way, but it sounds like this could be done with texture coordinate - object > gradient - spherical in terms of cycles nodes. (And a few converter math nodes to adjust things.) Somebody who knows scripting may show up knowing the equivalent.

I believe there is some issue with drivers in cycles nodes, I know I have always had issues getting them to work myself.

I can create a driver to control emission strength by hand. See base object (“Bulb”) in blend file. It works fine, i just want to duplicate this driver for all the new objects created using python.

If I recall correctly, you would have to dig into data-blocks for the material via the outliner to set drivers for materials. (Look for something like “node tree, shader node tree”, and go from there.) A bit tedious, compared to the expected way of using the nodes on the compositor interface. However you can keep the compositor nodes displayed while hunting through the outliner tree heirarchy, since when you find the right one it’s values will change with the setting in the outliner. Also don’t forget about the search function in the outliner, since that can help narrow things down.

I consider it an odd annoying quirky way of doing things, but it does work if you really need drivers on materials.

I tried using texture coord object with linked object. Unfortunately this doesn’t update correctly for lights when I move things around; if I want to update the output I have to reselect the object in the dropdown. If this updated correctly (or you can force an update), you only need to figure out the distance between the linked coordinate and the lights geometry position.

Ermmm… Maybe something like this material applied to the light spheres?

I tried it the way I mentioned earlier, but the spherical falloff aspect was too obvious. This vector math way seems to do it as a level over the entire object.

Now how to build it through a script… Somebody else will have to cover that part.