# Simple Python script: errors

import bpy
def driver_func(val):

if val < 5:
output = val * val

else:
output = val + 20

return output

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


I try to run this script in Blender 2.8 to add the function to the driver but it constantly fails. Why?

My second problem is to get this working:

    import bpy
activeobj = bpy.context.active_object
def driver_func(val):

if activeobj.name = "A":

output = val * val

else:

output = val + 20

return output

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


The error message is:

SyntaxError: invalid syntax

location: <unknown location>:-1

Error: Python script failed, check the message in the system console

• This works, except for indentations. What is the error message? Sep 15 '19 at 9:48
• The error message is: AttributeError: 'IndentationError' object has no attribute 'msg' Error: Python script failed, check the message in the system console Sep 15 '19 at 9:50
• Try indented like this i.stack.imgur.com/i4Szf.jpg (use tabs) It is the way python interprets things. Sep 15 '19 at 9:56
• There is problem with indentation. As, in python indentation does matter. Sep 15 '19 at 14:20
• Hi. Please only ask one question per post. If you need to ask multiple questions, ask them separately. Sep 15 '19 at 15:25

I would recommend that you use a proper IDE (Integrated Development Environment) that supports Python. This will allow you to check for syntax problems. The correct indentation of the code is important for Python because it is used to determine what part of the code belongs to, e.g. an if, else condition or a function.

Correctly formatted your first code should look like this:

import bpy

def driver_func(val):
if val < 5:
output = val * val
else:
output = val + 20

return output

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


Note that everything that belong into the function is indented by four spaces. What belongs inside the condition is indent by another four additional spaces. This is necessary in Python, because unlike for instance C, it doesn't use curly braces to determine the scope.

Your second script is problematic for two reasons.

1. The syntax is invalid. If you want to perform an equality check you have to use == instead of =. The latter is only used to assign a value to a variable.
2. You're declaring a variable outside the function, but are using it inside without passing it as an argument. This is bad because a) you don't want to create global state b) in this particular case the driver will not be able to access the variable because it has gone out of scope. The bpy.app.driver_namespace['driver_func'] only contains a reference to the function, but not the variable you're trying to use.
3. You cannot access the active object like through bpy.context.active_object when using the code in a driver. It should be bpy.context.view_layer.objects.active.

The correct script would therefore be:

import bpy

def driver_func(val):
active_obj = bpy.context.view_layer.objects.active

if active_obj.name == "A":
output = val * val
else:
output = val + 20

return output