-
Notifications
You must be signed in to change notification settings - Fork 1
/
Operators.py
426 lines (264 loc) · 13.1 KB
/
Operators.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import time
import threading
import bpy
from bpy.props import *
from . import Properties
from . import Curves
from . import CurveIntersections
from . import Util
from . import Surfaces
class OperatorSelectionInfo(bpy.types.Operator):
bl_idname = "curvetools2.operatorselectioninfo"
bl_label = "Selection Info"
bl_description = "Maintains a list of selected objects in the order they were selected"
@classmethod
def poll(cls, context):
selectedObjectNames = Properties.CurveTools2SelectedObject.GetSelectedObjectNames()
selectedBlenderObjectNames = Properties.CurveTools2SelectedObject.GetSelectedBlenderObjectNames()
sleepTime = 0.02
lock = threading.Lock()
lock_holder = threading.Thread(target = Properties.CurveTools2SelectedObject.UpdateThreadTarget, args=(lock, sleepTime, selectedObjectNames, selectedBlenderObjectNames), name='OperatorSelectionInfoThread')
# lock_holder = threading.Thread(target = Properties.CurveTools2SelectedObject.UpdateThreadTarget2, args=(lock, sleepTime, selectedObjectNames, selectedBlenderObjectNames, context), name='OperatorSelectionInfoThread')
lock_holder.setDaemon(True)
lock_holder.start()
return True
def execute(self, context):
nrSelectedObjects = bpy.context.scene.curvetools.NrSelectedObjects
self.report({'INFO'}, "Selection Info: nrSelectedObjects: %d" % nrSelectedObjects)
selectedObjects = bpy.context.scene.curvetools.SelectedObjects
selectedObjectValues = selectedObjects.values()
for i, selectedObject in enumerate(selectedObjectValues):
print("--", "selected object %d of %d: %s" % (i + 1, nrSelectedObjects, selectedObject.name))
return {'FINISHED'}
# 1 CURVE SELECTED
# ################
class OperatorCurveInfo(bpy.types.Operator):
bl_idname = "curvetools2.operatorcurveinfo"
bl_label = "Info"
bl_description = "Displays general info about the active/selected curve"
@classmethod
def poll(cls, context):
return Util.Selected1Curve()
def execute(self, context):
curve = Curves.Curve(context.active_object)
nrSplines = len(curve.splines)
nrSegments = 0
nrEmptySplines = 0
for spline in curve.splines:
nrSegments += spline.nrSegments
if spline.nrSegments < 1: nrEmptySplines += 1
self.report({'INFO'}, "nrSplines: %d; nrSegments: %d; nrEmptySplines: %d" % (nrSplines, nrSegments, nrEmptySplines))
return {'FINISHED'}
class OperatorCurveLength(bpy.types.Operator):
bl_idname = "curvetools2.operatorcurvelength"
bl_label = "Length"
bl_description = "Calculates the length of the active/selected curve"
@classmethod
def poll(cls, context):
return Util.Selected1Curve()
def execute(self, context):
curve = Curves.Curve(context.active_object)
context.scene.curvetools.CurveLength = curve.length
return {'FINISHED'}
class OperatorSplinesInfo(bpy.types.Operator):
bl_idname = "curvetools2.operatorsplinesinfo"
bl_label = "Info"
bl_description = "Displays general info about the splines of the active/selected curve"
@classmethod
def poll(cls, context):
return Util.Selected1Curve()
def execute(self, context):
curve = Curves.Curve(context.active_object)
nrSplines = len(curve.splines)
print("")
print("OperatorSplinesInfo:", "nrSplines:", nrSplines)
nrEmptySplines = 0
for iSpline, spline in enumerate(curve.splines):
print("--", "spline %d of %d: nrSegments: %d" % (iSpline + 1, nrSplines, spline.nrSegments))
if spline.nrSegments < 1:
nrEmptySplines += 1
print("--", "--", "## WARNING: spline has no segments and will therefor be ignored in any further calculations")
self.report({'INFO'}, "nrSplines: %d; nrEmptySplines: %d" % (nrSplines, nrEmptySplines) + " -- more info: see console")
return {'FINISHED'}
class OperatorSegmentsInfo(bpy.types.Operator):
bl_idname = "curvetools2.operatorsegmentsinfo"
bl_label = "Info"
bl_description = "Displays general info about the segments of the active/selected curve"
@classmethod
def poll(cls, context):
return Util.Selected1Curve()
def execute(self, context):
curve = Curves.Curve(context.active_object)
nrSplines = len(curve.splines)
nrSegments = 0
print("")
print("OperatorSegmentsInfo:", "nrSplines:", nrSplines)
nrEmptySplines = 0
for iSpline, spline in enumerate(curve.splines):
nrSegmentsSpline = spline.nrSegments
print("--", "spline %d of %d: nrSegments: %d" % (iSpline + 1, nrSplines, nrSegmentsSpline))
if nrSegmentsSpline < 1:
nrEmptySplines += 1
print("--", "--", "## WARNING: spline has no segments and will therefor be ignored in any further calculations")
continue
for iSegment, segment in enumerate(spline.segments):
print("--", "--", "segment %d of %d coefficients:" % (iSegment + 1, nrSegmentsSpline))
print("--", "--", "--", "C0: %.6f, %.6f, %.6f" % (segment.coeff0.x, segment.coeff0.y, segment.coeff0.z))
nrSegments += nrSegmentsSpline
self.report({'INFO'}, "nrSplines: %d; nrSegments: %d; nrEmptySplines: %d" % (nrSplines, nrSegments, nrEmptySplines))
return {'FINISHED'}
class OperatorOriginToSpline0Start(bpy.types.Operator):
bl_idname = "curvetools2.operatororigintospline0start"
bl_label = "OriginToSpline0Start"
bl_description = "Sets the origin of the active/selected curve to the starting point of the (first) spline. Nice for curve modifiers."
@classmethod
def poll(cls, context):
return Util.Selected1Curve()
def execute(self, context):
blCurve = context.active_object
blSpline = blCurve.data.splines[0]
newOrigin = blCurve.matrix_world * blSpline.bezier_points[0].co
origOrigin = bpy.context.scene.cursor.location.copy()
print("--", "origOrigin: %.6f, %.6f, %.6f" % (origOrigin.x, origOrigin.y, origOrigin.z))
print("--", "newOrigin: %.6f, %.6f, %.6f" % (newOrigin.x, newOrigin.y, newOrigin.z))
bpy.context.scene.cursor.location = newOrigin
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.context.scene.cursor.location = origOrigin
self.report({'INFO'}, "TODO: OperatorOriginToSpline0Start")
return {'FINISHED'}
# 2 CURVES SELECTED
# #################
class OperatorIntersectCurves(bpy.types.Operator):
bl_idname = "curvetools2.operatorintersectcurves"
bl_label = "Intersect"
bl_description = "Intersects selected curves"
@classmethod
def poll(cls, context):
return Util.Selected2Curves()
def execute(self, context):
print("### TODO: OperatorIntersectCurves.execute()")
algo = context.scene.curvetools.IntersectCurvesAlgorithm
print("-- algo:", algo)
mode = context.scene.curvetools.IntersectCurvesMode
print("-- mode:", mode)
# if mode == 'Split':
# self.report({'WARNING'}, "'Split' mode is not implemented yet -- <<STOPPING>>")
# return {'CANCELLED'}
affect = context.scene.curvetools.IntersectCurvesAffect
print("-- affect:", affect)
curveIntersector = CurveIntersections.CurvesIntersector.FromSelection()
rvIntersectionNrs = curveIntersector.CalcAndApplyIntersections()
self.report({'INFO'}, "Active curve points: %d; other curve points: %d" % (rvIntersectionNrs[0], rvIntersectionNrs[1]))
return {'FINISHED'}
class OperatorLoftCurves(bpy.types.Operator):
bl_idname = "curvetools2.operatorloftcurves"
bl_label = "Loft"
bl_description = "Lofts selected curves"
@classmethod
def poll(cls, context):
return Util.Selected2Curves()
def execute(self, context):
#print("### TODO: OperatorLoftCurves.execute()")
loftedSurface = Surfaces.LoftedSurface.FromSelection()
loftedSurface.AddToScene()
self.report({'INFO'}, "OperatorLoftCurves.execute()")
return {'FINISHED'}
class OperatorSweepCurves(bpy.types.Operator):
bl_idname = "curvetools2.operatorsweepcurves"
bl_label = "Sweep"
bl_description = "Sweeps the active curve along to other curve (rail)"
@classmethod
def poll(cls, context):
return Util.Selected2Curves()
def execute(self, context):
#print("### TODO: OperatorSweepCurves.execute()")
sweptSurface = Surfaces.SweptSurface.FromSelection()
sweptSurface.AddToScene()
self.report({'INFO'}, "OperatorSweepCurves.execute()")
return {'FINISHED'}
# 3 CURVES SELECTED
# #################
class OperatorBirail(bpy.types.Operator):
bl_idname = "curvetools2.operatorbirail"
bl_label = "Birail"
bl_description = "Generates a birailed surface from 3 selected curves -- in order: rail1, rail2 and profile"
@classmethod
def poll(cls, context):
return Util.Selected3Curves()
def execute(self, context):
birailedSurface = Surfaces.BirailedSurface.FromSelection()
birailedSurface.AddToScene()
self.report({'INFO'}, "OperatorBirail.execute()")
return {'FINISHED'}
# 1 OR MORE CURVES SELECTED
# #########################
class OperatorSplinesSetResolution(bpy.types.Operator):
bl_idname = "curvetools2.operatorsplinessetresolution"
bl_label = "SplinesSetResolution"
bl_description = "Sets the resolution of all splines"
@classmethod
def poll(cls, context):
return Util.Selected1OrMoreCurves()
def execute(self, context):
splRes = context.scene.curvetools.SplineResolution
selCurves = Util.GetSelectedCurves()
for blCurve in selCurves:
for spline in blCurve.data.splines:
spline.resolution_u = splRes
return {'FINISHED'}
class OperatorSplinesRemoveZeroSegment(bpy.types.Operator):
bl_idname = "curvetools2.operatorsplinesremovezerosegment"
bl_label = "SplinesRemoveZeroSegment"
bl_description = "Removes splines with no segments -- they seem to creep up, sometimes.."
@classmethod
def poll(cls, context):
return Util.Selected1OrMoreCurves()
def execute(self, context):
selCurves = Util.GetSelectedCurves()
for blCurve in selCurves:
curve = Curves.Curve(blCurve)
nrSplines = curve.nrSplines
splinesToRemove = []
for spline in curve.splines:
if len(spline.segments) < 1: splinesToRemove.append(spline)
nrRemovedSplines = len(splinesToRemove)
for spline in splinesToRemove: curve.splines.remove(spline)
if nrRemovedSplines > 0: curve.RebuildInScene()
self.report({'INFO'}, "Removed %d of %d splines" % (nrRemovedSplines, nrSplines))
return {'FINISHED'}
class OperatorSplinesRemoveShort(bpy.types.Operator):
bl_idname = "curvetools2.operatorsplinesremoveshort"
bl_label = "SplinesRemoveShort"
bl_description = "Removes splines with a length smaller than the threshold"
@classmethod
def poll(cls, context):
return Util.Selected1OrMoreCurves()
def execute(self, context):
threshold = context.scene.curvetools.SplineRemoveLength
selCurves = Util.GetSelectedCurves()
for blCurve in selCurves:
curve = Curves.Curve(blCurve)
nrSplines = curve.nrSplines
nrRemovedSplines = curve.RemoveShortSplines(threshold)
if nrRemovedSplines > 0: curve.RebuildInScene()
self.report({'INFO'}, "Removed %d of %d splines" % (nrRemovedSplines, nrSplines))
return {'FINISHED'}
class OperatorSplinesJoinNeighbouring(bpy.types.Operator):
bl_idname = "curvetools2.operatorsplinesjoinneighbouring"
bl_label = "SplinesJoinNeighbouring"
bl_description = "Joins neighbouring splines within a distance smaller than the threshold"
@classmethod
def poll(cls, context):
return Util.Selected1OrMoreCurves()
def execute(self, context):
selCurves = Util.GetSelectedCurves()
for blCurve in selCurves:
curve = Curves.Curve(blCurve)
nrSplines = curve.nrSplines
threshold = context.scene.curvetools.SplineJoinDistance
startEnd = context.scene.curvetools.SplineJoinStartEnd
mode = context.scene.curvetools.SplineJoinMode
nrJoins = curve.JoinNeighbouringSplines(startEnd, threshold, mode)
if nrJoins > 0: curve.RebuildInScene()
self.report({'INFO'}, "Applied %d joins on %d splines; resulting nrSplines: %d" % (nrJoins, nrSplines, curve.nrSplines))
return {'FINISHED'}