I have a function for a driver that expects 2 objects as arguments, and I can't find a way to set the variable as the Object ID, only a single property of that object. Is that possible?
2 Answers
You can set the driver to data path and use "name" as path, at the cost of getting the object from its name.
With this setting, the object can be renamed by the user and the driver won't be broken.
Using the depsgraph
Since the upgrade to 2.8 the depsgraph is a member of the locals of a driver, as are the variables. See answer to
How to use different types of data blocks for driver variables?
re setting up a test driver.
As an example, driver is on "Cube". The object "Lamp" is in scene, and passed as an evaluated object to the driver via the depsgraph. The depsgraph can also be used.
def test(*args, depsgraph=None, **kwargs):
for arg in args:
print(arg)
print(depsgraph)
return -1 # a simple val to indicate working
<bpy_struct, Object("Cube") at 0x7f62fcbefa08, evaluated>
<bpy_struct, Object("Lamp") at 0x7f62fcbf0008, evaluated>
<bpy_struct, Depsgraph at 0x7f62faba6c08>
An example to return minimum global z of all other objects in the depsgraph. The method of @lemon can be used for the object names. Other options could be tagging them via custom property or name prefix suffix.
def test(self, *args, depsgraph=None, **kwargs):
z = min(o.matrix_world.translation.z for o in depsgraph.objects
if o != self)
return z
Pointerproperties.
IMO the best way to associate an object(s) with another is via a pointer property
bpy.types.Object.driver_obj1 = bpy.props.PointerProperty(type=bpy.types.Object)
which can be assigned via script or UI.
context.object.driver_object1 = context.scene.objects.get("Lamp")
-
$\begingroup$ How does the
depsgraph
keyword argument get automagically assigned inside your firsttest()
function? Don't you have to look up the**kwargs
items through thekwargs
dictionary? .. (Sorry, another Python baby- gurgle.. I really have tried to look it up. $\endgroup$– Robin Betts ♦Commented Dec 5, 2020 at 12:27 -
$\begingroup$ It's in the
locals()
of the driver so can be used by name directly. just like any driver variable accessed by its name. $\endgroup$ Commented Dec 5, 2020 at 12:30 -
1
distance("Camera")
without using var. And get the object, bpy.data.objects[param] in the function. Or if you want var, use "name" as datapath $\endgroup$