-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsurf_cyl.py
823 lines (701 loc) · 28.5 KB
/
subsurf_cyl.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import bpy
import bmesh
import math
from bpy.props import (
BoolProperty,
IntProperty,
EnumProperty,
FloatProperty,
FloatVectorProperty)
bl_info = {
"name": "Create Tube",
"author": "Jeremy Behreandt",
"version": (0, 2),
"blender": (4, 1, 0),
"category": "Add Mesh",
"description": "Creates a subdivision surface ready cylinder.",
"tracker_url": "https://github.com/behreajj/SubSurfCylinder"
}
class TubeMaker(bpy.types.Operator):
"""Creates a subdivision surface ready cylinder"""
bl_idname = "mesh.primitive_tube_add"
bl_label = "Tube"
bl_options = {"REGISTER", "UNDO"}
sectors: IntProperty(
name="Vertices",
description="Number of vertices on the tube circumference",
min=3,
soft_max=64,
default=8)
orientation: FloatProperty(
name="Rotation",
description="Rotation of the tube",
soft_min=-math.pi,
soft_max=math.pi,
default=0.0,
subtype="ANGLE",
unit="ROTATION")
radius_btm: FloatProperty(
name="Bottom Radius",
description="Tube radius at the bottom",
min=0.001,
soft_max=100.0,
default=0.5)
radius_top: FloatProperty(
name="Top Radius",
description="Tube radius at the top",
min=0.001,
soft_max=100.0,
default=0.5)
depth: FloatProperty(
name="Depth",
description="Tube depth",
min=0.001,
soft_max=100.0,
default=1.5)
depth_offset: FloatProperty(
name="Offset",
description="Offset on the z axis",
default=0.0,
step=1,
precision=3,
min=-1.0,
max=1.0,
subtype="FACTOR")
cap_face_type: EnumProperty(
items=[
("NGON", "NGon", "Use n-sides polygons", 1),
("NONE", "None", "Do not add end caps", 2),
("QUAD", "Quad", "Use quadrilaterals for an even number of vertices", 3),
("TRI", "Tri", "Use triangles", 4)
],
name="Cap Fill Type",
default="QUAD",
description="How to create end cap polygons"
)
edge_loop_fac: FloatProperty(
name="Edge Loops",
description="Add loops used to control a subdivision surface modifier.",
default=0.175,
step=1,
precision=3,
min=0.0,
max=1.0,
subtype="FACTOR")
levels: IntProperty(
name="Subsurf",
description="Number of subdivisions in modifier.",
min=0,
soft_max=6,
max=6,
default=0)
shade_smooth: BoolProperty(
name="Shade Smooth",
description="Whether to use smooth shading",
default=False)
calc_uvs: BoolProperty(
name="Calc UVs",
description="Calculate texture coordinates",
default=False)
def execute(self, context):
# Unpack arguments.
sectors = self.sectors
orientation = self.orientation
radius_btm = self.radius_btm
radius_top = self.radius_top
depth = self.depth
depth_offset = self.depth_offset
cap_face_type = self.cap_face_type
edge_loop_fac = self.edge_loop_fac
shade_smooth = self.shade_smooth
calc_uvs = self.calc_uvs
levels = self.levels
# Convert string comparisons of cap face type
# to booleans.
cap_is_none = cap_face_type == "NONE"
cap_is_ngon = cap_face_type == "NGON"
cap_is_quad = cap_face_type == "QUAD"
cap_is_tri = cap_face_type == "TRI"
# A quadrilateral based end cap is possible only
# when the number of sectors or vertices is even.
# If not, then default to an n-gon face.
if (sectors < 5) or (sectors % 2 != 0):
cap_is_quad = False
if not cap_is_none:
cap_is_ngon = True
if sectors < 4:
cap_is_tri = False
cap_is_quad = False
if not cap_is_none:
cap_is_ngon = True
# Derive some booleans from combinations of the above.
# Used in deciding index offsets for vertices and faces.
use_center_spoke = cap_is_tri or cap_is_quad
use_caps = cap_is_tri or cap_is_quad or cap_is_ngon
use_edge_loops = edge_loop_fac > 0.0 \
and edge_loop_fac < 1.0
# Convert offset from [-1.0, 1.0] to [0.0, 1.0],
# Use it as a factor to find the cylinder's middle
# on the z axis. Then find the top and bottom.
offset_fac = depth_offset * 0.5 + 0.5
half_depth = depth * 0.5
vz_mid = (1.0 - offset_fac) * -half_depth \
+ offset_fac * half_depth
vz_btm = vz_mid - half_depth
vz_top = vz_mid + half_depth
# Alias edge loop factor to make linear interpolation
# easier.
t = edge_loop_fac
u = 1.0 - t
# Find the length of the cylinder's sides in cases
# where top and bottom radii are unequal.
diff = radius_top - radius_btm
slope = math.sqrt(diff * diff + depth * depth)
# To make the rounding of a Catmull Subsurf modifier
# more uniform, the factors used to set control loops
# need to be scaled according to whether cylinder sides
# are greater than radius or vice versa.
aspect_ratio_btm = 1.0
aspect_ratio_top = 1.0
if use_caps:
aspect_ratio_btm = radius_btm / slope
aspect_ratio_top = radius_top / slope
# For control loops on side panels.
t_btm = t
u_btm = u
if radius_btm < slope:
t_btm = t * aspect_ratio_btm
u_btm = 1.0 - t_btm
t_top = t
u_top = u
if radius_top < slope:
t_top = t * aspect_ratio_top
u_top = 1.0 - t_top
# For control loops on end caps.
t_fan_btm = t
u_fan_btm = u
if radius_btm > slope:
t_fan_btm = t / aspect_ratio_btm
u_fan_btm = 1.0 - t_fan_btm
t_fan_top = t
u_fan_top = u
if radius_top > slope:
t_fan_top = t / aspect_ratio_top
u_fan_top = 1.0 - t_fan_top
# Cache the sine and cosine calculations for
# the cylinder's radius, as they will be re-used.
sectors_to_theta = math.tau / sectors
cartesian = [(0.0, 0.0)] * sectors
sectors_range = range(0, sectors)
for i in sectors_range:
theta = orientation + i * sectors_to_theta
cos_t = math.cos(theta)
sin_t = math.sin(theta)
cartesian[i] = (cos_t, sin_t)
# Find index offsets for vector3s in vertices
# array. Ideally, vertices should be in order from
# z negative to z positive.
v_idx_btm_spoke = 0
v_idx_btm_fan = v_idx_btm_spoke
if use_center_spoke:
v_idx_btm_fan += 1
v_idx_btm_ctrl = v_idx_btm_fan
if use_edge_loops and use_caps:
v_idx_btm_ctrl += sectors
v_idx_btm_edge = v_idx_btm_ctrl
if use_edge_loops and use_caps:
v_idx_btm_edge += sectors
v_idx_side_lwr_ctrl = v_idx_btm_edge + sectors
v_idx_mid = v_idx_side_lwr_ctrl
if use_edge_loops:
v_idx_mid += sectors
v_idx_side_upp_ctrl = v_idx_mid
if use_edge_loops:
v_idx_side_upp_ctrl += sectors
v_idx_top_edge = v_idx_side_upp_ctrl
if use_edge_loops:
v_idx_top_edge += sectors
v_idx_top_ctrl = v_idx_top_edge
if use_edge_loops:
v_idx_top_ctrl += sectors
v_idx_top_fan = v_idx_top_ctrl
if use_edge_loops:
v_idx_top_fan += sectors
v_idx_top_spoke = v_idx_top_fan
if use_caps or cap_is_none:
v_idx_top_spoke += sectors
# Total length of vertices is the found at
# the end by summing all index offsets.
len_vs = v_idx_top_spoke
if use_center_spoke:
len_vs += 1
vs = [(0.0, 0.0, 0.0)] * len_vs
# For quads and tris, create a central spoke.
if use_center_spoke:
vs[v_idx_btm_spoke] = (0.0, 0.0, vz_btm)
vs[v_idx_top_spoke] = (0.0, 0.0, vz_top)
# Calculate top and bottom cylinder rings.
for i in sectors_range:
cart = cartesian[i]
cos_t = cart[0]
sin_t = cart[1]
vs[v_idx_btm_edge + i] = (
cos_t * radius_btm,
sin_t * radius_btm,
vz_btm)
vs[v_idx_top_edge + i] = (
cos_t * radius_top,
sin_t * radius_top,
vz_top)
if use_edge_loops:
radius_mid = (radius_btm + radius_top) * 0.5
side_lwr_ctrl_z = u_btm * vz_btm + t_btm * vz_mid
side_upp_ctrl_z = u_top * vz_top + t_top * vz_mid
radius_side_lwr_ctrl = u_btm * radius_btm \
+ t_btm * radius_mid
radius_side_upp_ctrl = u_top * radius_top \
+ t_top * radius_mid
# Find the middle ring, lower and upper control
# loops on the side of the cylinder.
for i in sectors_range:
cart = cartesian[i]
cos_t = cart[0]
sin_t = cart[1]
vs[v_idx_side_lwr_ctrl + i] = (
cos_t * radius_side_lwr_ctrl,
sin_t * radius_side_lwr_ctrl,
side_lwr_ctrl_z)
vs[v_idx_mid + i] = (
cos_t * radius_mid,
sin_t * radius_mid,
vz_mid)
vs[v_idx_side_upp_ctrl + i] = (
cos_t * radius_side_upp_ctrl,
sin_t * radius_side_upp_ctrl,
side_upp_ctrl_z)
if use_caps:
radius_fan_btm = radius_btm * 0.5
radius_cap_lwr_ctrl = u_fan_btm * radius_btm \
+ t_fan_btm * radius_fan_btm
radius_fan_top = radius_top * 0.5
radius_cap_upp_ctrl = u_fan_top * radius_top \
+ t_fan_top * radius_fan_top
# If end caps are used, find the middle fan
# and the control loop for both top and bottom.
for i in sectors_range:
cart = cartesian[i]
cos_t = cart[0]
sin_t = cart[1]
vs[v_idx_btm_fan + i] = (
cos_t * radius_fan_btm,
sin_t * radius_fan_btm,
vz_btm)
vs[v_idx_btm_ctrl + i] = (
cos_t * radius_cap_lwr_ctrl,
sin_t * radius_cap_lwr_ctrl,
vz_btm)
vs[v_idx_top_ctrl + i] = (
cos_t * radius_cap_upp_ctrl,
sin_t * radius_cap_upp_ctrl,
vz_top)
vs[v_idx_top_fan + i] = (
cos_t * radius_fan_top,
sin_t * radius_fan_top,
vz_top)
# For quad faces, a fan consists of half the number
# of sectors as triangle faces.
half_sectors = sectors // 2
half_sectors_range = range(0, half_sectors)
# Loop array offsets and length are equivalent
# for all mesh data -- coordinates (vs), texture
# coordinates (vts) and normals (vns).
loop_idx_btm_fan = 0
loop_idx_btm_mid = loop_idx_btm_fan
if cap_is_ngon:
loop_idx_btm_mid += 1
if cap_is_quad:
loop_idx_btm_mid += half_sectors
if cap_is_tri:
loop_idx_btm_mid += sectors
loop_idx_btm_ctrl = loop_idx_btm_mid
if use_edge_loops and use_caps:
loop_idx_btm_ctrl += sectors
loop_idx_side_lwr_ctrl = loop_idx_btm_ctrl
if use_edge_loops and use_caps:
loop_idx_side_lwr_ctrl += sectors
loop_idx_side_lwr = loop_idx_side_lwr_ctrl
if use_edge_loops:
loop_idx_side_lwr += sectors
loop_idx_side_upp = loop_idx_side_lwr
if use_edge_loops:
loop_idx_side_upp += sectors
loop_idx_side_upp_ctrl = loop_idx_side_upp
if use_edge_loops:
loop_idx_side_upp_ctrl += sectors
loop_idx_top_ctrl = loop_idx_side_upp_ctrl
if use_edge_loops and use_caps:
loop_idx_top_ctrl += sectors
loop_idx_top_mid = loop_idx_top_ctrl
if use_edge_loops and use_caps:
loop_idx_top_mid += sectors
loop_idx_top_fan = loop_idx_top_mid + sectors
len_loop_idcs = loop_idx_top_fan
if cap_is_ngon:
len_loop_idcs += 1
if cap_is_quad:
len_loop_idcs += half_sectors
if cap_is_tri:
len_loop_idcs += sectors
v_idcs = [(0, 0, 0, 0)] * len_loop_idcs
# Create central fan.
if cap_is_tri:
for i in sectors_range:
j = (i + 1) % sectors
v_idcs[loop_idx_btm_fan + i] = (
v_idx_btm_spoke,
v_idx_btm_fan + j,
v_idx_btm_fan + i)
v_idcs[loop_idx_top_fan + i] = (
v_idx_top_spoke,
v_idx_top_fan + i,
v_idx_top_fan + j)
if cap_is_quad:
for h in half_sectors_range:
i = h + h
j = (i + 1) % sectors
k = (i + 2) % sectors
v_idcs[loop_idx_btm_fan + h] = (
v_idx_btm_spoke,
v_idx_btm_fan + k,
v_idx_btm_fan + j,
v_idx_btm_fan + i)
v_idcs[loop_idx_top_fan + h] = (
v_idx_top_spoke,
v_idx_top_fan + i,
v_idx_top_fan + j,
v_idx_top_fan + k)
if cap_is_ngon:
idcs_btm_arr = [0] * sectors
idcs_top_arr = [0] * sectors
for i in sectors_range:
j = (i - 1) % sectors
idcs_btm_arr[i] = v_idx_btm_fan + sectors - 1 - j
idcs_top_arr[i] = v_idx_top_fan + i
v_idcs[loop_idx_btm_fan] = tuple(idcs_btm_arr)
v_idcs[loop_idx_top_fan] = tuple(idcs_top_arr)
if use_edge_loops:
if use_caps:
for i in sectors_range:
j = (i + 1) % sectors
v_idcs[loop_idx_btm_mid + i] = (
v_idx_btm_fan + i,
v_idx_btm_fan + j,
v_idx_btm_ctrl + j,
v_idx_btm_ctrl + i)
v_idcs[loop_idx_btm_ctrl + i] = (
v_idx_btm_ctrl + i,
v_idx_btm_ctrl + j,
v_idx_btm_edge + j,
v_idx_btm_edge + i)
v_idcs[loop_idx_top_ctrl + i] = (
v_idx_top_edge + i,
v_idx_top_edge + j,
v_idx_top_ctrl + j,
v_idx_top_ctrl + i)
v_idcs[loop_idx_top_mid + i] = (
v_idx_top_ctrl + i,
v_idx_top_ctrl + j,
v_idx_top_fan + j,
v_idx_top_fan + i)
# Create side panel faces.
for i in sectors_range:
k = (i + 1) % sectors
v_idcs[loop_idx_side_lwr_ctrl + i] = (
v_idx_btm_edge + i,
v_idx_btm_edge + k,
v_idx_side_lwr_ctrl + k,
v_idx_side_lwr_ctrl + i)
v_idcs[loop_idx_side_lwr + i] = (
v_idx_side_lwr_ctrl + i,
v_idx_side_lwr_ctrl + k,
v_idx_mid + k,
v_idx_mid + i)
v_idcs[loop_idx_side_upp + i] = (
v_idx_mid + i,
v_idx_mid + k,
v_idx_side_upp_ctrl + k,
v_idx_side_upp_ctrl + i)
v_idcs[loop_idx_side_upp_ctrl + i] = (
v_idx_side_upp_ctrl + i,
v_idx_side_upp_ctrl + k,
v_idx_top_edge + k,
v_idx_top_edge + i)
else:
for i in sectors_range:
k = (i + 1) % sectors
v_idcs[loop_idx_side_lwr_ctrl + i] = (
v_idx_btm_edge + i,
v_idx_btm_edge + k,
v_idx_top_edge + k,
v_idx_top_edge + i)
d_objs = bpy.data.objects
d_meshes = bpy.data.meshes
scene_objs = context.scene.collection.objects
mesh_data = d_meshes.new("Cylinder")
mesh_data.from_pydata(vs, [], v_idcs)
mesh_data.validate(verbose=True)
bm = bmesh.new()
bm.from_mesh(mesh_data)
for face in bm.faces:
face.smooth = shade_smooth
if calc_uvs:
# Order of vts matters less than of coordinates
# or of faces, so indexed offsets are calculated
# according to convenience.
sectorsp1 = sectors + 1
len_vts = sectorsp1 * 2
if use_edge_loops:
len_vts += sectorsp1 * 3
if use_caps:
len_vts += sectors * 2
if use_edge_loops:
len_vts += sectors * 4
if use_center_spoke:
len_vts += 2
vt_idx_btm_strip = 0
vt_idx_top_strip = sectorsp1
vt_idx_side_lwr_ctrl = -1
vt_idx_mid_strip = -1
vt_idx_side_upp_ctrl = -1
if use_edge_loops:
vt_idx_side_lwr_ctrl = sectorsp1 * 2
vt_idx_mid_strip = sectorsp1 * 3
vt_idx_side_upp_ctrl = sectorsp1 * 4
vt_idx_btm_edge = -1
vt_idx_btm_ctrl = -1
vt_idx_btm_fan = -1
vt_idx_top_edge = -1
vt_idx_top_ctrl = -1
vt_idx_top_fan = -1
vt_idx_btm_spoke = -1
vt_idx_top_spoke = -1
if use_caps:
sectorsp1_2 = sectorsp1 * 2
vt_idx_btm_edge = sectorsp1_2
vt_idx_top_edge = sectorsp1_2 + sectors
# In case no edge loops are used, the edge
# and the fan will be the same index offsets.
vt_idx_btm_fan = vt_idx_btm_edge
vt_idx_top_fan = vt_idx_top_edge
if use_edge_loops:
sectorsp1_5 = sectorsp1 * 5
vt_idx_btm_edge = sectorsp1_5
vt_idx_btm_ctrl = sectorsp1_5 + sectors
vt_idx_btm_fan = sectorsp1_5 + sectors * 2
vt_idx_top_edge = sectorsp1_5 + sectors * 3
vt_idx_top_ctrl = sectorsp1_5 + sectors * 4
vt_idx_top_fan = sectorsp1_5 + sectors * 5
if use_center_spoke:
vt_idx_btm_spoke = len_vts - 2
vt_idx_top_spoke = len_vts - 1
vts = [(0.0, 0.0)] * len_vts
# If no end caps are used, then the cylinder sides
# span the entire UV range. Otherwise, the sides
# are compressed to the top-half of the range and
# the end caps are on the bottom-half.
vts_min_y = 0.0
if use_caps:
vts_min_y = 0.5
vts_max_y = 1.0
# Find the top and bottom of the uv sides.
# UVs include one extra edge, as the wrapping
# at (0.0, 1.0) is automatically calculated.
# Ideally, these would be trapezoids when the top
# and bottom radius differ, but if the goal is subsurf
# then artifacts of connected rects will be reduced.
sectorsp1_range = range(0, sectorsp1)
sectors_to_uv = 1.0 / sectors
for j in sectorsp1_range:
x = j * sectors_to_uv
vts[vt_idx_btm_strip + j] = (x, vts_min_y)
vts[vt_idx_top_strip + j] = (x, vts_max_y)
if use_edge_loops:
vts_mid_y = (vts_min_y + vts_max_y) * 0.5
vt_side_lwr_ctrl_y = u_btm * vts_min_y \
+ t_btm * vts_mid_y
vt_side_upp_ctrl_y = u_top * vts_max_y \
+ t_top * vts_mid_y
for j in sectorsp1_range:
x = j * sectors_to_uv
vts[vt_idx_side_lwr_ctrl + j] = (x, vt_side_lwr_ctrl_y)
vts[vt_idx_mid_strip + j] = (x, vts_mid_y)
vts[vt_idx_side_upp_ctrl + j] = (x, vt_side_upp_ctrl_y)
if use_caps:
# Follows Blender UV conventions.
vts_rad = 0.25
vt_btm_center_x = 0.75
vt_btm_center_y = 0.25
vt_top_center_x = 0.25
vt_top_center_y = 0.25
# For fan-based end caps, add the central spoke.
if use_center_spoke:
vts[vt_idx_btm_spoke] = (
vt_btm_center_x,
vt_btm_center_y)
vts[vt_idx_top_spoke] = (
vt_top_center_x,
vt_top_center_y)
for i in sectors_range:
cart = cartesian[i]
cos_t_r = cart[0] * vts_rad
sin_t_r = cart[1] * vts_rad
vts[vt_idx_btm_edge + i] = (
vt_btm_center_x + cos_t_r,
vt_btm_center_y + sin_t_r)
vts[vt_idx_top_edge + i] = (
vt_top_center_x + cos_t_r,
vt_top_center_y + sin_t_r)
if use_edge_loops:
vts_radius_mid = vts_rad * 0.5
vts_radius_cap_lwr_ctrl = u_fan_btm * vts_rad \
+ t_fan_btm * vts_radius_mid
vts_radius_cap_upp_ctrl = u_fan_top * vts_rad \
+ t_fan_top * vts_radius_mid
for i in sectors_range:
cart = cartesian[i]
cos_t = cart[0]
sin_t = cart[1]
cos_t_mid = cos_t * vts_radius_mid
sin_t_mid = sin_t * vts_radius_mid
vts[vt_idx_btm_fan + i] = (
vt_btm_center_x + cos_t_mid,
vt_btm_center_y + sin_t_mid)
vts[vt_idx_btm_ctrl + i] = (
vt_btm_center_x + cos_t * vts_radius_cap_lwr_ctrl,
vt_btm_center_y + sin_t * vts_radius_cap_lwr_ctrl)
vts[vt_idx_top_ctrl + i] = (
vt_top_center_x + cos_t * vts_radius_cap_upp_ctrl,
vt_top_center_y + sin_t * vts_radius_cap_upp_ctrl)
vts[vt_idx_top_fan + i] = (
vt_top_center_x + cos_t_mid,
vt_top_center_y + sin_t_mid)
# Loop indices are consistent across all data types.
vt_idcs = [(0, 0, 0, 0)] * len_loop_idcs
if cap_is_tri:
for i in sectors_range:
j = (i + 1) % sectors
vt_idcs[loop_idx_btm_fan + i] = (
vt_idx_btm_spoke,
vt_idx_btm_fan + j,
vt_idx_btm_fan + i)
vt_idcs[loop_idx_top_fan + i] = (
vt_idx_top_spoke,
vt_idx_top_fan + i,
vt_idx_top_fan + j)
if cap_is_quad:
for h in half_sectors_range:
i = h + h
j = (i + 1) % sectors
k = (i + 2) % sectors
vt_idcs[loop_idx_btm_fan + h] = (
vt_idx_btm_spoke,
vt_idx_btm_fan + k,
vt_idx_btm_fan + j,
vt_idx_btm_fan + i)
vt_idcs[loop_idx_top_fan + h] = (
vt_idx_top_spoke,
vt_idx_top_fan + i,
vt_idx_top_fan + j,
vt_idx_top_fan + k)
if cap_is_ngon:
idcs_btm_arr = [0] * sectors
idcs_top_arr = [0] * sectors
for i in sectors_range:
j = (i - 1) % sectors
idcs_btm_arr[i] = vt_idx_btm_fan + sectors - 1 - j
idcs_top_arr[i] = vt_idx_top_fan + i
vt_idcs[loop_idx_btm_fan] = tuple(idcs_btm_arr)
vt_idcs[loop_idx_top_fan] = tuple(idcs_top_arr)
if use_edge_loops:
if use_caps:
for i in sectors_range:
j = (i + 1) % sectors
vt_idcs[loop_idx_btm_mid + i] = (
vt_idx_btm_fan + i,
vt_idx_btm_fan + j,
vt_idx_btm_ctrl + j,
vt_idx_btm_ctrl + i)
vt_idcs[loop_idx_btm_ctrl + i] = (
vt_idx_btm_ctrl + i,
vt_idx_btm_ctrl + j,
vt_idx_btm_edge + j,
vt_idx_btm_edge + i)
vt_idcs[loop_idx_top_ctrl + i] = (
vt_idx_top_edge + i,
vt_idx_top_edge + j,
vt_idx_top_ctrl + j,
vt_idx_top_ctrl + i)
vt_idcs[loop_idx_top_mid + i] = (
vt_idx_top_ctrl + i,
vt_idx_top_ctrl + j,
vt_idx_top_fan + j,
vt_idx_top_fan + i)
# Create side panels.
for i in sectors_range:
j = i + 1
vt_idcs[loop_idx_side_lwr_ctrl + i] = (
vt_idx_btm_strip + i,
vt_idx_btm_strip + j,
vt_idx_side_lwr_ctrl + j,
vt_idx_side_lwr_ctrl + i)
vt_idcs[loop_idx_side_lwr + i] = (
vt_idx_side_lwr_ctrl + i,
vt_idx_side_lwr_ctrl + j,
vt_idx_mid_strip + j,
vt_idx_mid_strip + i)
vt_idcs[loop_idx_side_upp + i] = (
vt_idx_mid_strip + i,
vt_idx_mid_strip + j,
vt_idx_side_upp_ctrl + j,
vt_idx_side_upp_ctrl + i)
vt_idcs[loop_idx_side_upp_ctrl + i] = (
vt_idx_side_upp_ctrl + i,
vt_idx_side_upp_ctrl + j,
vt_idx_top_strip + j,
vt_idx_top_strip + i)
else:
for i in sectors_range:
j = i + 1
vt_idcs[loop_idx_side_lwr_ctrl + i] = (
vt_idx_btm_strip + i,
vt_idx_btm_strip + j,
vt_idx_top_strip + j,
vt_idx_top_strip + i)
uv_layer = bm.loops.layers.uv.verify()
for face in bm.faces:
faceuvidcs = vt_idcs[face.index]
for i, loop in enumerate(face.loops):
loop[uv_layer].uv = vts[faceuvidcs[i]]
bm.to_mesh(mesh_data)
bm.free()
mesh_obj = d_objs.new(mesh_data.name, mesh_data)
mesh_obj.location = context.scene.cursor.location
if levels > 0:
sds_mod = mesh_obj.modifiers.new("Subdivision", "SUBSURF")
sds_mod.levels = levels
sds_mod.render_levels = levels
sds_mod.uv_smooth = "SMOOTH_ALL"
sds_mod.show_in_editmode = False
scene_objs.link(mesh_obj)
return {"FINISHED"}
@classmethod
def poll(cls, context):
return context.area.type == "VIEW_3D"
def menu_func(self, context):
self.layout.operator(TubeMaker.bl_idname, icon="MESH_CYLINDER")
def register():
bpy.utils.register_class(TubeMaker)
bpy.types.VIEW3D_MT_mesh_add.append(menu_func)
def unregister():
bpy.utils.unregister_class(TubeMaker)
bpy.types.VIEW3D_MT_mesh_add.remove(menu_func)