So for a project I have been working on, I am attempting to angle a plane based on the positions of 10 different points (represented by empties) by means of linear regression. The current code, as well as the driver and input variables, I have created are shown below:
So far this error keeps popping up:
I have attempted to use various techniques I found have failed to resolve the problem, so I am wondering whether I did those wrong or the code itself is flawed.
Thanks.
Edit 1:
So after allowing the code to execute, the following error pops up:

Also, the code for the program is below:
import bpy # importing necessary functions
import math
from math import atan
import numpy as np
from numpy.linalg import inv
def Control_Function(obj_points): # creating driver function
X_values = [] # obtaning world cordinates
Y_values = []
Z_values = []
for x in range(len(obj_points)):
coor = obj_points[x]
X_values.append(coor[0])
Y_values.append(coor[1])
Z_values.append(coor[2])
X_average = np.sum(X_values)/len(X_values) # creating approximate center
Y_average = np.sum(Y_values)/len(Y_values)
Z_average = np.sum(Z_values)/len(Z_values)
X_coor = X_values - X_average # calculating cordinates based on approximate center
Y_coor = Y_values - Y_average
Z_coor = Z_values - Z_average
A = np.zeros((3,3)) # Finding least sqaures regression coefficients
b = np.zeros((3,1))
A_two = np.zeros((3,3))
b_two = np.zeros((3,1))
A_three = np.zeros((3,3))
b_three = np.zeros((3,1))
looptime = len(X_values)
x = 0
while x < looptime:
A_three = A
b_three = b
A_two = [[X_coor[x]*X_coor[x] ,X_coor[x]*Y_coor[x] ,X_coor[x]] ,[Y_coor[x]*X_coor[x] ,Y_coor[x]*Y_coor[x] ,Y_coor[x]] ,[X_coor[x] ,Y_coor[x] ,1]]
b_two = [[Z_coor[x]*X_coor[x]] ,[Z_coor[x]*Y_coor[x]] ,[Z_coor[x]]]
A = A_two + A_three
b = b_two + b_three
x += 1
A_t = np.transpose(A) # Solving least squares regression
A_At = np.matmul(A_t,A)
A_At_inv = inv(np.array(A_At))
AAt_inv_A_t = np.matmul(A_At_inv,A_t)
[[a],[b],[c]] = np.matmul(AAt_inv_A_t,b)
return a ,b ,c
def xanglecalc(p1 ,p2 ,p3 ,p4 ,p5 ,p6 ,p7 ,p8 ,p9 ,p10):
print(p2)
in_vector = [p1 ,p2 ,p3 ,p4 ,p5 ,p6 ,p7 ,p8 ,p9 ,p10]
out_vector = []
coor = []
sub_coor = []
for mat in range(len(in_vector)):
coor = in_vector[mat]
sub_coor = coor[4,0:2]
out_vector.append(sub_coor)
print(out_vector)
a ,b ,c = Control_Function(out_vector)
x_angle = atan(a)
y_angle = -atan(b)
return x_angle
def yanglecalc(p1 ,p2 ,p3 ,p4 ,p5 ,p6 ,p7 ,p8 ,p9 ,p10):
in_vector = [p1 ,p2 ,p3 ,p4 ,p5 ,p6 ,p7 ,p8 ,p9 ,p10]
out_vector = []
coor = []
sub_coor = []
for mat in range(len(in_vector)):
coor = in_vector[mat]
sub_coor = [[coor[4,0]] ,[coor[4,1]] ,[coor[4,2]]]
out_vector.append(sub_coor)
a ,b ,c = Control_Function(out_vector)
x_angle = atan(a)
y_angle = -atan(b)
return y_angle
bpy.app.driver_namespace['xangle'] = xanglecalc
bpy.app.driver_namespace['yangle'] = yanglecalc


Save & Load>Auto Run Python Scripts– X Y Jul 10 '22 at 23:34