# Can we parse “string” as Driver Variable?

I am using scripted driver to changes image texture.

import bpy

def change_image_baby( val,min,max):
if (min <= val <= max):
filePath = "//baby_eyes\\eye_0%d.png" % val
bpy.data.images["Minu Child eyes.001"].filepath = filePath
return val

bpy.app.driver_namespace['change_image_baby'] = change_image_baby


On bone Movement by using above function as driver I am able to change the images.

Now i want to use same driver function on many other characters.

So I defined "filePath" and "image" name as object property of each character

New driver function

import bpy

def change_image(n,min,max,img,file):
val = int(n*10)
# img is image to be replaced and "file" is the file path
return val
if (min <= val <= max):
filePath = file % val # as all my images are in sequence i replacing numbers see above code
bpy.data.images[img].filepath = filePath

bpy.app.driver_namespace['change_image'] = change_image


and use in driver UI to parse it to custom driver

I am not able to parse string it has to be either int or float. What is the workaround to parse string as Driver Variable?

in this method we try to identify the object that is running the driver then we access its properties ( image and file ):

1. we add a new property to all scene objects this property is their index in the scene objects list ( we need to run this every time we add/delete an object to the scene ) :

import bpy
for obj in bpy.context.scene.objects :
obj["index"] = bpy.context.scene.objects.find(obj.name)

2. we pass only the object index to the driver to identify it and access its properties.

import bpy

def change_image(n,min,max,index):
obj = bpy.context.scene.objects[index]
val = int(n*10)
if (min <= val <= max):
filePath = obj["file"] % val
bpy.data.images[obj["img"]].filepath = filePath

bpy.app.driver_namespace['change_image'] = change_image

• @ashwin images are not added in scene.objects list ( only objects ) – Chebhou Feb 14 '16 at 18:11
• @David are images loaded into scene.objects ? – Chebhou Feb 14 '16 at 18:22
• I dont know off the top of my head. I was not the one to make that change, ashwin did, I just fixed the bullets from the review. – David Feb 14 '16 at 18:25
• @David I know you didn't make the change just wanted to clarify that the edit was not correct – Chebhou Feb 14 '16 at 18:26
• @Chebhou you are adding index property to the each object right? then whenever an object is added or deleted there is change in the actual index of the object to what was assigned – ashwin Feb 14 '16 at 19:23

You could choose the image by number from a preset list -

import bpy

img_list = [
('Eyes.001','//eyes/image%d.png'),
('Mouth.001','//mouth/image%d.png'),
]

def change_image(n,min,max,img,file):
val = int(n*10)
# img is type of image and "file" is the file path
if (min <= val <= max):
filePath = img_list[file][1] % val # as all my images are in sequence i replacing numbers see above code
bpy.data.images[img_list[file][0]].filepath = filePath

bpy.app.driver_namespace['change_image'] = change_image