Adding driver through python doesn't work properly? I am writing a script and this script required adding a driver to an object,so I added it but I have 2 problems with it.


I have added a single property variable and set its ID and path. After I run the script, the variable added correctly and everything is fine, but I see that the path is colored in red and Blender said that my driver failed. I figure how to fix this, by selecting the ID object (spin object) one more time (which is already selected) as shown in the figure.
So how can I fix this? Because I don't want to do this every time I run the script.
and here is the script and the image.

newVar.name = "Array"
newVar.type = 'SINGLE_PROP'
newVar.targets[0].id = bpy.data.objects['Spin object']
newVar.targets[0].data_path = 'modifiers["Array"].count'






When I change the value of the variable (array count), I didn't see the effect until I press update dependencies. I want it to be updated without pressing update dependencies
every time I change the value of the variable. How can I do this?
I think this is a limitation of the current dependency graph.

The way I would handle it is to add a custom property to the object, than add a driver to the array count that uses the custom value, set this other driver to also use the custom property.

As answered in the comments, you also need to overcome the chicken and the egg theory when creating drivers.

Edit


Add a new custom property to the object. in the UI you click the add button, in python there is nothing special, you just give the property a value and it gets created.

bpy.data.objects['Cube']['myControl'] = 1
Add a driver to the count value of the array modifier





Add the same driver settings for the other item that previous read modifiers['Array'].count


Now when you change the myControl value, both items will use the same value. You can create a panel wherever you like to display the custom property or add it to an existing panel.