-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOP.py
142 lines (105 loc) · 4.16 KB
/
BOP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
""" https://blender.stackexchange.com/questions/7198/save-the-2d-bounding-box-of-an-object-in-rendered-image-to-a-text-file """
import bpy
import numpy as np
import bmesh
from math import degrees, pi
from scipy.spatial.transform import Rotation as R
def get_relative_object_position(scene, camera_object, mesh_object):
"""
if (mesh_object in camera_object.children):
#print("object is a child of the camera - no calculation required")
#matrix = camera_object.matrix_world.normalized().inverted()
x_lin = mesh_object.location[0]
y_lin = mesh_object.location[1] #-mesh_object.location[1]
z_lin = mesh_object.location[2]
#world_diff = mesh_object.matrix_world
else:
#matrix = camera_object.matrix_world.normalized().inverted()
x_lin = mesh_object.location[0] - camera_object.location[0]
y_lin = (mesh_object.location[1] - camera_object.location[1]) #-(mesh_object.location[1] - camera_object.location[1])
z_lin = mesh_object.location[2] - camera_object.location[2]
world_diff = mesh_object.matrix_world - camera_object.matrix_world
print(world_diff)
print(" x lin ", x_lin)
print(" y lin ", y_lin)
print(" z lin ", z_lin)
print(type(x_lin))
"""
world = mesh_object.matrix_world.to_translation()
#print(world)
matrix = camera_object.matrix_world
R = np.array([
[matrix[0][0],matrix[0][1],matrix[0][2]],
[matrix[1][0],matrix[1][1],matrix[1][2]],
[matrix[2][0],matrix[2][1],matrix[2][2]]])
world = np.dot(world, R)
x_lin = float(world[0])
y_lin = float(world[1])
z_lin = float(world[2])
#world_diff = mesh_object.matrix_world - camera_object.matrix_world
#print(world_diff)
#print(" x lin ", x_lin)
#print(" y lin ", y_lin)
#print(" z lin ", z_lin)
#print(type(x_lin))
### siemens
#trans_vec = (-x_lin, z_lin, -y_lin)
trans_vec = (-x_lin, y_lin, z_lin)
### non siemens
#trans_vec = (x_lin, y_lin, z_lin)
return trans_vec
def get_relative_object_rotation(scene, camera_object, mesh_object):
axis = camera_object.matrix_world.to_quaternion()
q = mesh_object.matrix_world.to_quaternion()
rotdif = axis.rotation_difference(q)
#print("\n _____ quat")
#print(rotdif)
#print(q)
#print("\n _____ matrix")
#print(camera_object.matrix_world - mesh_object.matrix_world)
#print(mesh_object.matrix_local)
#print((rotdif.to_euler().x, rotdif.to_euler().y, rotdif.to_euler().z))
#print( mesh_object.matrix_local)
"""
R = np.array([
[matrix[0][0],matrix[0][1],matrix[0][2]],
[matrix[1][0],matrix[1][1],matrix[1][2]],
[matrix[2][0],matrix[2][1],matrix[2][2]]])
print(R)
return R"""
#print(mesh_object.matrix_local.Rotation())
### debug
#mat = R.from_quat([0, 0, 0, 1])
### for siemens
#mat = euler_to_rotMat(rotdif.to_euler().x, -rotdif.to_euler().y, -rotdif.to_euler().z)
mat = R.from_quat([rotdif.x, -rotdif.y, -rotdif.z, rotdif.w])
#return mat
# non siemens
#mat = euler_to_rotMat(rotdif.to_euler().x, rotdif.to_euler().y, rotdif.to_euler().z)
#mat = R.from_quat([rotdif.x, rotdif.y, rotdif.z, rotdif.w])
return mat.as_matrix()
def euler_to_rotMat(roll, pitch, yaw):
Rz_yaw = np.array([
[np.cos(yaw), -np.sin(yaw), 0],
[np.sin(yaw), np.cos(yaw), 0],
[ 0, 0, 1]])
Ry_pitch = np.array([
[ np.cos(pitch), 0, np.sin(pitch)],
[ 0, 1, 0],
[-np.sin(pitch), 0, np.cos(pitch)]])
Rx_roll = np.array([
[1, 0, 0],
[0, np.cos(roll), -np.sin(roll)],
[0, np.sin(roll), np.cos(roll)]])
# R = RzRyRx
#rotMat = np.dot(Rx_roll, np.dot(Ry_pitch, Rz_yaw))
### for siemens
rotMat = np.dot(Rz_yaw, np.dot(Ry_pitch, Rx_roll))
#print(rotMat)
return rotMat
if __name__ == '__main__':
print("starting")
scene = bpy.data.scenes['Scene']
camera_object = bpy.data.objects['Camera']
mesh_object = bpy.data.objects['Palette']
print(get_relative_object_position(scene, camera_object, mesh_object))