2
$\begingroup$
import bpy
def driver_func(val):

if val < 5:
output = val * val

else:
output = val + 20

return output

# add function to driver_namespace
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

#add function to driver_namespace
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
$\endgroup$
7
  • $\begingroup$ This works, except for indentations. What is the error message? $\endgroup$
    – lemon
    Sep 15, 2019 at 9:48
  • $\begingroup$ The error message is: AttributeError: 'IndentationError' object has no attribute 'msg' Error: Python script failed, check the message in the system console $\endgroup$
    – Olafur
    Sep 15, 2019 at 9:50
  • 1
    $\begingroup$ Try indented like this i.stack.imgur.com/i4Szf.jpg (use tabs) It is the way python interprets things. $\endgroup$
    – lemon
    Sep 15, 2019 at 9:56
  • 1
    $\begingroup$ There is problem with indentation. As, in python indentation does matter. $\endgroup$ Sep 15, 2019 at 14:20
  • 1
    $\begingroup$ Hi. Please only ask one question per post. If you need to ask multiple questions, ask them separately. $\endgroup$ Sep 15, 2019 at 15:25

2 Answers 2

1
$\begingroup$

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

# add function to driver_namespace
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

#add function to driver_namespace
bpy.app.driver_namespace['driver_func'] = driver_func
$\endgroup$
0
0
$\begingroup$

The indentation error msg has been reported as at bug here: https://developer.blender.org/T67295

There is an add-on which can show indentation guides and whitespace markers: enter image description here Download: https://github.com/K-410/blender-scripts/blob/master/2.8/code_editor.py

Additional add-ons for enhancing the Blender Text Editor can be found here: https://blenderartists.org/t/how-would-you-show-some-love-for-the-blender-text-editor/1163857

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .