-
Notifications
You must be signed in to change notification settings - Fork 0
/
testPlanes.py
241 lines (179 loc) · 6.97 KB
/
testPlanes.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import maya.cmds as cmds
import math
cmds.file( f=True, new=True )
# clear up the scene
cmds.select(all=True)
cmds.delete()
def cross(a, b):
c = [a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]]
return c
p=0
def dot(a,b):
global p
p=0
for i in range(len(a)):
p+=a[i]*b[i]
return p
def toEuler( x, y, z, angle):
s=math.sin(angle)
c=math.cos(angle)
t=1-c
#if axis is not already normalised then uncomment this
#magnitude = math.sqrt(x*x + y*y + z*z);
#if (magnitude==0):
# print 'magnitude of axis of rotation 0'
# exit()
# x /= magnitude;
# y /= magnitude;
# z /= magnitude;
if x*y*t + z*s > 0.998:# north pole singularity detected
heading = 2*math.atan2(x*math.sin(angle/2),math.cos(angle/2))
attitude = math.pi/2
bank = 0
return bank,heading,attitude
if ((x*y*t + z*s) < -0.998):#south pole singularity detected
heading = -2*math.atan2(x*math.sin(angle/2),math.cos(angle/2));
attitude = -math.pi/2;
bank = 0
return bank,heading,attitude
heading = math.atan2(y * s- x * z * t , 1 - (y*y+ z*z ) * t);
attitude = math.asin(x * y * t + z * s) ;
bank = math.atan2(x * s - y * z * t , 1 - (x*x + z*z) * t);
return bank,heading,attitude
numberOfTotalWholeCurves=1
curveNumber=1
curveName='pPlane1'#curve1
numberOfWholeCircleRotations=4
lengthCircleRadius=5
angle=0.0
springCoefficient=200.0
angleIncrementCircleSegments=20
mycurve=''
#for i in range(numberOfTotalWholeCurves):
# curveNumber+=1
# angle=0.0
planeList=[]
while angle<numberOfWholeCircleRotations*360.0:
x=lengthCircleRadius*math.cos(angle*(math.pi/180.0))
y=lengthCircleRadius*math.sin(angle*(math.pi/180.0))
springzIncrement=(angle/springCoefficient)
''' + str(curveNumber)'''
if not cmds.objExists(curveName ):
#mycurve=cmds.curve(p=[(x,y,springzIncrement),(x,y,springzIncrement),(x,y,springzIncrement),(x,y,springzIncrement)])
mycurve=cmds.polyPlane(sx=1,sy=1 )#', axis=[x,y,springzIncrement]
cmds.move(x,y,springzIncrement)
cmds.scale(2,2,1)
cmds.rotate(0,0,90)
planeList.append(mycurve[0])
cmds.select(all=True)
cmds.delete(ch=True)
else:
#cmds.curve(mycurve, append=True, p=[(x,y,springzIncrement)])
mycurve=cmds.polyPlane(sx=1,sy=1 )#, axis=[x,y,springzIncrement]
cmds.move(x,y,springzIncrement)
cmds.scale(2,2,1)
cmds.rotate(0,-springzIncrement,90+angle)
planeList.append(mycurve[0])
#now we can hide the planes
#cmds.hide()
cmds.select(all=True)
cmds.delete(ch=True)
angle+=angleIncrementCircleSegments
#cmds.refresh()
for i in planeList:
#facenormals=cmds.polyInfo( fn=True )
facepositionWS=cmds.xform(i+".f[:]", query=True, translation=True, worldSpace=True)
print facepositionWS
'''get normals of each face'''
#Cannot use the following command since it will return the initial plane creation normal, not the one after we performed the 'cmds.rotate' command
#facenormals=cmds.polyInfo( fn=True )
#so..we can actually calculate each plane's normal from cross(vertex2-vertex1, vertex3-vertex1), since they all line on the same plane
v1=cmds.xform(i+".vtx[0]", query=True, translation=True, worldSpace=True)
v2=cmds.xform(i+".vtx[1]", query=True, translation=True, worldSpace=True)
v3=cmds.xform(i+".vtx[2]", query=True, translation=True, worldSpace=True)
#v2-v1
sideVector1=[ v2[0]-v1[0], v2[1]-v1[1], v2[2]-v1[2] ]
#v3-v1
sideVector2=[ v3[0]-v1[0], v3[1]-v1[1], v3[2]-v1[2] ]
#Now we can feed them to the cross product function
planeNormal=cross(sideVector1,sideVector2)
#find center of face (average the face's vertex posiitons)
sumX=0
sumY=0
sumZ=0
#4 vertices (12_all_coordinates / 3_coordinates_each vertex in 3d)
faceVertices=len(facepositionWS)/3
vertexElements=3
#print "faceVertices=%d"%(faceVertices)
for v in range(0,len(facepositionWS),vertexElements):
sumX=sumX + facepositionWS[v]
sumY=sumY + facepositionWS[v+1]
sumZ=sumZ + facepositionWS[v+2]
#average position of vertices of each faces
faceCenter = [sumX/faceVertices, sumY/faceVertices, sumZ/faceVertices]
#place cone
c=cmds.polyCone( n='myCone', sx=5, sy=5, sz=5)
cmds.move( faceCenter[0], faceCenter[1], faceCenter[2], c, absolute=True )
cmds.scale(0.3,0.3,0.3, c, absolute=True)
#split polyinfo unicode , to get normals of each face
#fnormal= []
#label, vertex, x, y, z = facenormals[i].split()
#fnormal.append(float(x))
#fnormal.append(float(y))
#fnormal.append(float(z))
fnormal=planeNormal
fnormalNormalized=[]
#normalize vector from point on sphee to origin of sphere
mag=math.sqrt( fnormal[0]*fnormal[0] + fnormal[1]*fnormal[1] + fnormal[2]*fnormal[2] )
if mag==0:
mag=1
#calculate axis-angle
upVec=[0,1,0]
axis=cross(upVec, fnormal)
axisNormalized=[]
mag=math.sqrt( axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2] )
if mag==0:
mag=1
axisNormalized.append(axis[0]/mag)
axisNormalized.append(axis[1]/mag)
axisNormalized.append(axis[2]/mag)
#print "dot(upVec,fnormalNormalized)=%f"%(dot(upVec,fnormal))
#normalize if fnormal is not normalized
if (fnormal[0]>1 or fnormal[0]<-1) or (fnormal[1]>1 or fnormal[1]<-1) or ((fnormal[2]>1 or fnormal[2]<-1)):
mag=math.sqrt( fnormal[0]*fnormal[0] + fnormal[1]*fnormal[1] + fnormal[2]*fnormal[2] )
fnormalNormalized.append(fnormal[0]/mag)
fnormalNormalized.append(fnormal[1]/mag)
fnormalNormalized.append(fnormal[2]/mag)
fnormal=fnormalNormalized
angle=math.acos( dot(upVec,fnormal) )
bank,heading,attitude = toEuler(axisNormalized[0],axisNormalized[1],axisNormalized[2],angle)
heading*=(180/math.pi)
attitude*=(180/math.pi)
bank*=(180/math.pi)
'''
if fnormal[1]<0 and fnormal[2]<0 and fnormal[0]>0:
heading=heading+90
attitude=-attitude
if fnormal[1]<0 and fnormal[2]<0 and fnormal[0]<0:
heading=-heading+90
attitude=-attitude
elif fnormal[1]<0:
attitude=-attitude
'''
'''
mag=math.sqrt( axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2] )
if fnormal==[0,-0.5,0]:
attitude=attitude+180
print 1
'''
#print "heading=%f"%(heading)
#print "attitude=%f"%(attitude)
#print "bank=%f"%(bank)
r = math.sqrt(fnormal[0]*fnormal[0] + fnormal[1]*fnormal[1] + fnormal[2]*fnormal[2])
fi = (math.acos(fnormal[1])/r) * (180.0/math.pi)
theta = math.atan2(fnormal[0],fnormal[2]) * (180.0/math.pi)
#cmds.rotate( bank, heading, attitude, absolute=True, r=True )
cmds.rotate(fi, theta, 0)
cmds.delete(i,ch=True)