-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry_script.py
248 lines (164 loc) · 5.91 KB
/
geometry_script.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
241
242
243
244
245
246
247
248
import c4d, random
from c4d import utils
from c4d import Vector as v
#--------
def run(): #---- Only runs the script when userdata switch is on
global GenSw
GenSw = doc.SearchObject("Script") #Find the object the switch
global Gsw
if GenSw[c4d.ID_USERDATA,1]==True: # if the switch on...
Gsw = True
GenSw[c4d.ID_USERDATA,1]=False # turn it off immediately (like a click press)
else:
Gsw = False #ensure switch remains off
def noI(): #Number of iterations to run
global noi
noi = GenSw[c4d.ID_USERDATA,4]
print "Number of interations to run", noi
def gen():
global grpN
grpN = c4d.BaseObject(c4d.Onull)
grpN.SetName("Generated")
doc.InsertObject(grpN)
def clear(): #---- Deletes children (old objects upon re-running script)
a = doc.SearchObject("Generated")
chld = a.GetChildren()
print len(chld)
if len(chld) == 0:
return
else:
doc.SearchObject("Generated")
def getG(): #----- Gets geometry and adds a display tag
global geoM
geoM = GenSw[c4d.ID_USERDATA,2]
print "Geometry =", geoM
dtag = geoM.MakeTag(c4d.Tdisplay) #adds display tag to cube
dtag[c4d.DISPLAYTAG_AFFECT_DISPLAYMODE] = True #sets display oon
dtag[c4d.DISPLAYTAG_SDISPLAYMODE] = c4d.DISPLAYTAG_SDISPLAY_NOSHADING #sets shading mode
def geomD(): #----- Gets geometry data
global l, geomPolyC
iD = GenSw[c4d.ID_USERDATA,3]
print " "
geomPolyC = geoM.GetPolygonCount() #gets polygon count
geomPntC = geoM.GetPointCount() #gets point count
geomP = geoM.GetAllPoints() #gets all points from Geometry, vectors
print "Point count =", geomPntC
polyA = geoM.GetPolygon(iD) #gets polygon (index)
polyPli = polyA.a, polyA.b, polyA.c, polyA.d #gets points index of polygon
print "Polygon id =", iD
print "Index points of polygon", polyPli
print " "
l =[]
for x in xrange (0, 4):
l2 = geomP[(polyPli[x])]
l.append(l2)
def check():
for x in xrange (0, 4):
tobj = c4d.BaseObject(c4d.Osplinetext)
tobj.SetRelPos(l[x])
tobj[c4d.PRIM_TEXT_TEXT] = str(x)
tobj[c4d.PRIM_TEXT_ALIGN] = c4d.PRIM_TEXT_ALIGN_MIDDLE
tobj[c4d.PRIM_TEXT_HEIGHT] = 5
tobj.SetName(x)
def calcE(): #------ calculates centre point of indexed polygon
global edg
cpX = (l[0][0] + l[1][0] + l[2][0] + l[3][0]) / 4
cpY = (l[0][1] + l[1][1] + l[2][1] + l[3][1]) / 4
cpZ = (l[0][2] + l[1][2] + l[2][2] + l[3][2]) / 4
tobj1 = c4d.BaseObject(c4d.Onull)
tobj1.SetRelPos(v(cpX, cpY, cpZ))
tobj1.SetName("Poly Edge")
#tobj1.InsertUnder(grpN)
edg1 = tobj1.GetAbsPos()
edg = edg1
def calcC(): #------ calculates centre point geometry
global cntr
geomPntC = geoM.GetPointCount() #gets point count
geomP = geoM.GetAllPoints()
cntr1 = (sum(geomP) / geomPntC) #maintains vector
cntr = cntr1
tobj1 = c4d.BaseObject(c4d.Onull)
tobj1.SetRelPos(v(cntr))
tobj1.SetName("Center")
#tobj1.InsertUnder(grpN)
def dist():
global dist
dist = edg - cntr
#print "distance =", dist
strd1 = (dist.x),(dist.y),(dist.z)
strd2 = list(strd1)
#print "strd2 = ", strd2
for i in xrange (0, 2):
#print strd2[i]
if strd2[i] == 0:
del strd2[i]
#print "deleted"
dist = strd2
dist = int(dist[0])*2
if dist < 0:
dist = dist * (-1)
print dist
def dirc():
global nV
dr = range(0, geomPolyC)
nV = []
for x in xrange (0, int(noi)):
#nV[x] = []
dri = random.randint(0,(len(dr)+1))
root = cntr
#print dri
nV1 = v(root.x,root.y,root.z)
if dri == 0:
#print "-Z"
nV1[2] = root.z - dist
if dri == 1:
#print "+X"
nV1[0] = root.x + dist
if dri == 2:
#print "+Z"
nV1[2] = root.z + dist
if dri == 3:
#print "-X"
nV1[0] = root.x - dist
if dri == 4:
#print "+Y"
nV1[1] = root.y + dist
if dri == 5:
#print "-Y"
nV1[1] = root.y - dist
nV.append(nV1)
def cr8():
for x in xrange (0, int(noi)):
lobj = grpN.GetDown()
if lobj == None:
lobj = geoM.GetClone()
a = lobj.GetClone()
a.SetName("Clone")
pos = a.GetAbsPos() + nV[x]
a.SetAbsPos(pos)
a.InsertUnder(grpN)
def main():
run() #runs script
if Gsw ==True:
print "Running!"
print " "
noI()
gen() # Creates generated null
getG() # Gets the geometry from the script userdata
#clear() #Should delete Generated null with its children
if geoM == None:
return
else:
geomD() #Gets geometry information from getC
#check() #check index
calcE() #calculates polygon centre (edge of geom)
calcC() #calculates centre of geom
if edg and cntr == None:
return
else:
print edg, cntr #prints the two vectors
dist() #calculates the distance between edge and centre x 2
dirc() #calucates movement in 3D space
cr8() #creates a clone with distance and dirc
else:
return