Looking for the proper python syntax to configure my driver, based on an XZ local position

Hello all, I am trying to make a controller that swaps out a models “mouths” based on the local X and Z positions of the controllers marker.

I have the formula for one axis at a time, but i am certain there is a way to combine the two in one statement;
I just don’t know enough of python yet to make it on my own (yet)

Any help is appreciated!

Apparently adding “or” between the two statements was sufficient, creating the following:

(posX <= 0.5) * 0 + (posX > 0.5) * 1 or (posZ <= 0.5) * 0 + (posZ > 0.5) * 1

This still seems like a bit of an overkill or the wrong approach, so I would appreciate any feedback on the code before implementing it for the rest of my sheet!

In cases like this, I find it helpful to think things through logically before worrying about code. Currently you have two numbers that you’re reducing into one number- so in order to maintain the integrity of your 1D numbers, your 2D range needs to be as large or larger than the the two numbers multiplied together. In your case, it’s a six by six grid, so your 2D range needs to be at least 0-36. This immediately gives a hint about the math required- multiplication of the numbers gives you a nice row/column index, but it’s not dimensionally respective. 3,4 will give the same result as 4,3, for example. To get those nice round values, that’s simple- just use a floor() or ceil().

Now what? Well, you need some way to easily distinguish rows from columns without overlapping with existing data. For example, what if you add .1 after your floor() to just the columns? Now you have 3.1,4 and 4.1,3- 12.4 and 12.3- nice! No more dimensional mixup!

So currently you have (floor(X) +.1) * floor(z) - this will give you a unique result for every combination of X and Z, which seems to be what you want :slight_smile:

1 Like

Here’s the proof that this formula will give unique, dimensionally-respective values:

0 0 0.0
0 1 0.01
0 2 0.02
0 3 0.03
0 4 0.04
0 5 0.05
0 6 0.06
1 0 0.0
1 1 1.01
1 2 2.02
1 3 3.0300000000000002
1 4 4.04
1 5 5.05
1 6 6.0600000000000005
2 0 0.0
2 1 2.01
2 2 4.02
2 3 6.029999999999999
2 4 8.04
2 5 10.049999999999999
2 6 12.059999999999999
3 0 0.0
3 1 3.01
3 2 6.02
3 3 9.03
3 4 12.04
3 5 15.049999999999999
3 6 18.06
4 0 0.0
4 1 4.01
4 2 8.02
4 3 12.03
4 4 16.04
4 5 20.049999999999997
4 6 24.06
5 0 0.0
5 1 5.01
5 2 10.02
5 3 15.03
5 4 20.04
5 5 25.049999999999997
5 6 30.06
6 0 0.0
6 1 6.01
6 2 12.02
6 3 18.03
6 4 24.04
6 5 30.049999999999997
6 6 36.06

Although I’m seeing now that there is one small issue- 0.0 repeats, so you should start from 1, not 0.

(floor(1+X) +.1) * floor(1+z) to be safe :slight_smile:

0 0 1.01
0 1 2.02
0 2 3.0300000000000002
0 3 4.04
0 4 5.05
0 5 6.0600000000000005
0 6 7.07
1 0 2.01
1 1 4.02
1 2 6.029999999999999
1 3 8.04
1 4 10.049999999999999
1 5 12.059999999999999
1 6 14.069999999999999
2 0 3.01
2 1 6.02
2 2 9.03
2 3 12.04
2 4 15.049999999999999
2 5 18.06
2 6 21.07
3 0 4.01
3 1 8.02
3 2 12.03
3 3 16.04
3 4 20.049999999999997
3 5 24.06
3 6 28.07
4 0 5.01
4 1 10.02
4 2 15.03
4 3 20.04
4 4 25.049999999999997
4 5 30.06
4 6 35.07
5 0 6.01
5 1 12.02
5 2 18.03
5 3 24.04
5 4 30.049999999999997
5 5 36.06
5 6 42.07
6 0 7.01
6 1 14.02
6 2 21.03
6 3 28.04
6 4 35.05
6 5 42.06

yep, that fixed it

1 Like

Thank you for your assitance Joseph.
I have tackled similar scripts in the past, but only via C++ or C#.

It is kind of wild to wrap my head around the short snippets you can just place in a Driver and see it Do Stuff.

1 Like

Pretty cool stuff :slight_smile: In case I wasn’t clear, your final driver code should be this:

(floor(1+posX) +.1) * floor(1+posZ)

Parsing that data presents a new challenge, but hey, it’s unique and dimensionally correct

1 Like

Funny you bring that up, because I was already contemplating if I was approaching the rest from the wrong angle, having my Driver/Driven relationship backwards.
If you have the time, could you glance over my other topic where I tried expanding on my base?

Most optimal way to create a Facial Animation rig via drivers / Python, advice requested - Support / Animation and Rigging - Blender Artists Community

1 Like