0
$\begingroup$

I was trying to make an infinite while loop for my project, however, for some reason the code refuses to proceed past the first iteration. For reference, the driver is meant to make the circle mesh move infinitely in the y axis when the cube is at a height of 2 meters. Instead it moves one meter and stays locked. Any ideas on what I did wrong?


def custom_driver(value):
    number = 0
    while value == 2:
        number = number + 1
        print(f"{value} -> {number}")
        return number
    
bpy.app.driver_namespace["my_custom_driver"] = custom_driver

While Loop

$\endgroup$
1
  • $\begingroup$ Why are you doing this? It will freeze Blender. But print value before loop, you will see, it is not 2 $\endgroup$ Commented Jun 18, 2024 at 8:17

1 Answer 1

3
$\begingroup$

A driver is evaluated in Python, and once Python part finishes, the value is returned to Blender and Blender uses this value in the field of the driver and continues evaluating the scene. But you say it yourself, the loop is infinite, so Python never finishes. There are engines like PyGame that are constructed using an infinite loop, but this is not how Blender-Python interaction works.

Also, the paradigm of drivers is that they are stateless. Once you maintain state, it becomes tricky to apply a driver to multiple objects. And if you have just one object, it doesn't make sense to use a driver, just use Python and subscribe to a frame change event. I speak about it more here:

Use a custom noise driver for multiple objects without sharing noise seed

Consider using Geometry Nodes with a simulation zone to achieve what you want:

Control shape keys with drivers to the speed of rotation

$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.