1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Author, Anthony D'Agostino
6 from bpy
.props
import (
14 from bpy_extras
import object_utils
17 class AddTeapot(bpy
.types
.Operator
, object_utils
.AddObjectHelper
):
18 bl_idname
= "mesh.primitive_teapot_add"
19 bl_label
= "Add Teapot"
20 bl_description
= "Construct a teapot or teaspoon mesh"
21 bl_options
= {"REGISTER", "UNDO"}
23 resolution
: IntProperty(
25 description
="Resolution of the Teapot",
29 objecttype
: EnumProperty(
31 description
="Type of Bezier Object",
32 items
=(('1', "Teapot", "Construct a teapot mesh"),
33 ('2', "Tea Spoon", "Construct a teaspoon mesh")),
37 def draw(self
, context
):
41 box
.prop(self
, 'resolution')
44 box
.prop(self
, 'objecttype')
46 # generic transform props
48 box
.prop(self
, 'align', expand
=True)
49 box
.prop(self
, 'location', expand
=True)
50 box
.prop(self
, 'rotation', expand
=True)
52 def execute(self
, context
):
53 # turn off 'Enter Edit Mode'
54 use_enter_edit_mode
= bpy
.context
.preferences
.edit
.use_enter_edit_mode
55 bpy
.context
.preferences
.edit
.use_enter_edit_mode
= False
57 cmode
= bpy
.context
.mode
58 verts
, faces
= make_teapot(self
.objecttype
,
60 # Actually create the mesh object from this geometry data.
61 obj
= create_mesh_object(self
, context
, verts
, [], faces
, "Teapot")
62 bpy
.ops
.object.mode_set(mode
='EDIT')
63 bpy
.ops
.mesh
.remove_doubles()
64 if cmode
!= "EDIT_MESH":
65 bpy
.ops
.object.mode_set(mode
=cmode
)
67 if use_enter_edit_mode
:
68 bpy
.ops
.object.mode_set(mode
= 'EDIT')
70 # restore pre operator state
71 bpy
.context
.preferences
.edit
.use_enter_edit_mode
= use_enter_edit_mode
76 def create_mesh_face_hack(faces
):
77 # FIXME, faces with duplicate vertices shouldn't be created in the first place.
84 faces_copy
.append(f_copy
)
88 def create_mesh_object(self
, context
, verts
, edges
, faces
, name
):
90 create_mesh_face_hack(faces
)
93 mesh
= bpy
.data
.meshes
.new(name
)
94 # Make a mesh from a list of verts/edges/faces.
95 mesh
.from_pydata(verts
, edges
, faces
)
96 # Update mesh geometry after adding stuff.
99 return object_utils
.object_data_add(context
, mesh
, operator
=self
)
102 # ==========================
103 # === Bezier patch Block ===
104 # ==========================
106 def read_indexed_patch_file(filename
):
107 file = io
.StringIO(filename
)
110 numpatches
= int(file.readline())
111 for i
in range(numpatches
):
112 line
= file.readline()
117 ) = map(int, line
.split(","))
118 patches
.append([[a
, b
, c
, d
], [e
, f
, g
, h
], [i
, j
, k
, l
], [m
, n
, o
, p
]])
119 rawpatches
.append([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
121 numverts
= int(file.readline())
122 for i
in range(numverts
):
123 line
= file.readline()
124 v1
, v2
, v3
= map(float, line
.split(","))
125 verts
.append((v1
, v2
, v3
))
126 for i
in range(len(patches
)):
127 for j
in range(4): # len(patches[i])):
128 for k
in range(4): # len(patches[i][j])):
129 index
= patches
[i
][j
][k
] - 1
130 rawpatches
[i
][j
][k
] = verts
[index
]
134 def patches_to_raw(patches
, resolution
):
136 for patch
in patches
:
137 verts
= make_verts(patch
, resolution
)
138 faces
= make_faces(resolution
)
139 rawquads
= indexed_to_rawquads(verts
, faces
)
141 raw
= functools
.reduce(operator
.add
, raw
) # flatten the list
145 def make_bezier(ctrlpnts
, resolution
):
151 return 3.0 * t
* t
* (1.0 - t
)
154 return 3.0 * t
* (1.0 - t
) * (1.0 - t
)
157 return (1.0 - t
) * (1.0 - t
) * (1.0 - t
)
159 p1
, p2
, p3
, p4
= map(mathutils
.Vector
, ctrlpnts
)
162 x
, y
, z
= b1(t
) * p1
+ b2(t
) * p2
+ b3(t
) * p3
+ b4(t
) * p4
164 curveverts
= [makevert(i
/ resolution
) for i
in range(resolution
+ 1)]
168 def make_verts(a
, resolution
):
171 c
= make_bezier(i
, resolution
)
176 c
= make_bezier(i
, resolution
)
179 verts
= functools
.reduce(operator
.add
, verts
) # flatten the list
183 def make_faces(resolution
):
186 for i
in range(resolution
):
187 for j
in range(resolution
):
189 v2
= (i
+ 1) * n
+ j
+ 1
192 faces
.append([v1
, v2
, v3
, v4
])
196 def indexed_to_rawquads(verts
, faces
):
198 cols
= len(faces
[0]) # or 4
199 rawquads
= [[None] * cols
for i
in range(rows
)]
200 for i
in range(rows
):
201 for j
in range(cols
):
203 rawquads
[i
][j
] = verts
[index
]
207 def raw_to_indexed(rawfaces
):
208 # Generate verts and faces lists, without dups
212 for i
in range(len(rawfaces
)):
213 for j
in range(len(rawfaces
[i
])):
214 vertex
= rawfaces
[i
][j
]
215 if vertex
not in coords
:
216 coords
[vertex
] = index
219 rawfaces
[i
][j
] = coords
[vertex
]
220 return verts
, rawfaces
223 def transpose(rowsbycols
):
224 rows
= len(rowsbycols
)
225 cols
= len(rowsbycols
[0])
226 colsbyrows
= [[None] * rows
for i
in range(cols
)]
227 for i
in range(cols
):
228 for j
in range(rows
):
229 colsbyrows
[i
][j
] = rowsbycols
[j
][i
]
233 def make_teapot(enumname
, resolution
):
234 filenames
= [None, teapot
, teaspoon
]
236 indexes
= int(enumname
)
237 filename
= filenames
[indexes
]
239 print("Add Teapot Error: EnumProperty could not be set")
240 filename
= filenames
[1]
242 patches
= read_indexed_patch_file(filename
)
243 raw
= patches_to_raw(patches
, resolution
)
244 verts
, faces
= raw_to_indexed(raw
)
245 return (verts
, faces
)
248 # =================================
249 # === Indexed Bezier Data Block ===
250 # =================================
252 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
253 4,17,18,19,8,20,21,22,12,23,24,25,16,26,27,28
254 19,29,30,31,22,32,33,34,25,35,36,37,28,38,39,40
255 31,41,42,1,34,43,44,5,37,45,46,9,40,47,48,13
256 13,14,15,16,49,50,51,52,53,54,55,56,57,58,59,60
257 16,26,27,28,52,61,62,63,56,64,65,66,60,67,68,69
258 28,38,39,40,63,70,71,72,66,73,74,75,69,76,77,78
259 40,47,48,13,72,79,80,49,75,81,82,53,78,83,84,57
260 57,58,59,60,85,86,87,88,89,90,91,92,93,94,95,96
261 60,67,68,69,88,97,98,99,92,100,101,102,96,103,104,105
262 69,76,77,78,99,106,107,108,102,109,110,111,105,112,113,114
263 78,83,84,57,108,115,116,85,111,117,118,89,114,119,120,93
264 121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136
265 124,137,138,121,128,139,140,125,132,141,142,129,136,143,144,133
266 133,134,135,136,145,146,147,148,149,150,151,152,69,153,154,155
267 136,143,144,133,148,156,157,145,152,158,159,149,155,160,161,69
268 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177
269 165,178,179,162,169,180,181,166,173,182,183,170,177,184,185,174
270 174,175,176,177,186,187,188,189,190,191,192,193,194,195,196,197
271 177,184,185,174,189,198,199,186,193,200,201,190,197,202,203,194
272 204,204,204,204,207,208,209,210,211,211,211,211,212,213,214,215
273 204,204,204,204,210,217,218,219,211,211,211,211,215,220,221,222
274 204,204,204,204,219,224,225,226,211,211,211,211,222,227,228,229
275 204,204,204,204,226,230,231,207,211,211,211,211,229,232,233,212
276 212,213,214,215,234,235,236,237,238,239,240,241,242,243,244,245
277 215,220,221,222,237,246,247,248,241,249,250,251,245,252,253,254
278 222,227,228,229,248,255,256,257,251,258,259,260,254,261,262,263
279 229,232,233,212,257,264,265,234,260,266,267,238,263,268,269,242
280 270,270,270,270,279,280,281,282,275,276,277,278,271,272,273,274
281 270,270,270,270,282,289,290,291,278,286,287,288,274,283,284,285
282 270,270,270,270,291,298,299,300,288,295,296,297,285,292,293,294
283 270,270,270,270,300,305,306,279,297,303,304,275,294,301,302,271
290 1.3375,-0.749,2.53125
291 0.749,-1.3375,2.53125
294 1.4375,-0.805,2.53125
295 0.805,-1.4375,2.53125
304 -0.749,-1.3375,2.53125
305 -1.3375,-0.749,2.53125
307 -0.805,-1.4375,2.53125
308 -1.4375,-0.805,2.53125
316 -1.3375,0.749,2.53125
317 -0.749,1.3375,2.53125
319 -1.4375,0.805,2.53125
320 -0.805,1.4375,2.53125
594 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
595 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
596 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48
597 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
598 65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80
599 81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96
600 97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112
601 113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128
602 129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144
603 145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160
604 161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176
605 177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192
606 193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208
607 209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224
608 225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240
609 241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256
611 -0.000107143,0.205357,0.0
612 0.0,0.196429,-0.0178571
613 0.0,0.196429,-0.0178571
614 0.000107143,0.205357,0.0
615 -0.0535714,0.205357,0.0
616 -0.0222714,0.178571,-0.0534286
617 0.0222714,0.178571,-0.0534286
618 0.0535714,0.205357,0.0
619 -0.107143,0.0952429,-0.0178571
620 -0.0446429,0.0952429,-0.0892857
621 0.0446429,0.0952429,-0.0892857
622 0.107143,0.0952429,-0.0178571
623 -0.107143,0.0,-0.0178571
624 -0.0446429,0.0,-0.0892857
625 0.0446429,0.0,-0.0892857
626 0.107143,0.0,-0.0178571
627 0.000107143,0.205357,0.0
628 0.000135714,0.207589,0.00446429
629 0.000157143,0.216518,0.00446429
630 0.000125,0.214286,0.0
631 0.0535714,0.205357,0.0
632 0.0613964,0.212054,0.0133571
633 0.0714286,0.220982,0.015625
635 0.107143,0.0952429,-0.0178571
636 0.122768,0.0952429,0.0
637 0.142857,0.0952429,0.00446429
638 0.125,0.0952429,-0.0178571
639 0.107143,0.0,-0.0178571
641 0.142857,0.0,0.00446429
643 0.000125,0.214286,0.0
644 0.0,0.205357,-0.0178571
645 0.0,0.205357,-0.0178571
646 -0.000125,0.214286,0.0
648 0.0267857,0.1875,-0.0625
649 -0.0267857,0.1875,-0.0625
651 0.125,0.0952429,-0.0178571
652 0.0535714,0.0952429,-0.107143
653 -0.0535714,0.0952429,-0.107143
654 -0.125,0.0952429,-0.0178571
656 0.0535714,0.0,-0.107143
657 -0.0535714,0.0,-0.107143
658 -0.125,0.0,-0.0178571
659 -0.000125,0.214286,0.0
660 -0.000157143,0.216518,0.00446429
661 -0.000135714,0.207589,0.00446429
662 -0.000107143,0.205357,0.0
664 -0.0714286,0.220982,0.015625
665 -0.0613964,0.212054,0.0133571
666 -0.0535714,0.205357,0.0
667 -0.125,0.0952429,-0.0178571
668 -0.142857,0.0952429,0.00446429
669 -0.122768,0.0952429,0.0
670 -0.107143,0.0952429,-0.0178571
671 -0.125,0.0,-0.0178571
672 -0.142857,0.0,0.00446429
674 -0.107143,0.0,-0.0178571
675 -0.107143,0.0,-0.0178571
676 -0.0446429,0.0,-0.0892857
677 0.0446429,0.0,-0.0892857
678 0.107143,0.0,-0.0178571
679 -0.107143,-0.142857,-0.0178571
680 -0.0446429,-0.142857,-0.0892857
681 0.0446429,-0.142857,-0.0892857
682 0.107143,-0.142857,-0.0178571
683 -0.0133929,-0.160714,0.0386893
684 -0.00557857,-0.160714,0.0386893
685 0.00557857,-0.160714,0.0386893
686 0.0133929,-0.160714,0.0386893
687 -0.0133929,-0.25,0.0535714
688 -0.00557857,-0.25,0.0535714
689 0.00557857,-0.25,0.0535714
690 0.0133929,-0.25,0.0535714
691 0.107143,0.0,-0.0178571
693 0.142857,0.0,0.00446429
695 0.107143,-0.142857,-0.0178571
696 0.122768,-0.142857,0.0
697 0.142857,-0.142857,0.00446429
698 0.125,-0.142857,-0.0178571
699 0.0133929,-0.160714,0.0386893
700 0.0153464,-0.160714,0.0386893
701 0.0178571,-0.160714,0.0314357
702 0.015625,-0.160714,0.0297607
703 0.0133929,-0.25,0.0535714
704 0.0153464,-0.25,0.0535714
705 0.0178571,-0.25,0.0463179
706 0.015625,-0.25,0.0446429
708 0.0535714,0.0,-0.107143
709 -0.0535714,0.0,-0.107143
710 -0.125,0.0,-0.0178571
711 0.125,-0.142857,-0.0178571
712 0.0535714,-0.142857,-0.107143
713 -0.0535714,-0.142857,-0.107143
714 -0.125,-0.142857,-0.0178571
715 0.015625,-0.160714,0.0297607
716 0.00669643,-0.160714,0.0230643
717 -0.00781071,-0.160714,0.0208321
718 -0.015625,-0.160714,0.0297607
719 0.015625,-0.25,0.0446429
720 0.00669643,-0.25,0.0379464
721 -0.00781071,-0.25,0.0357143
722 -0.015625,-0.25,0.0446429
723 -0.125,0.0,-0.0178571
724 -0.142857,0.0,0.00446429
726 -0.107143,0.0,-0.0178571
727 -0.125,-0.142857,-0.0178571
728 -0.142857,-0.142857,0.00446429
729 -0.122768,-0.142857,0.0
730 -0.107143,-0.142857,-0.0178571
731 -0.015625,-0.160714,0.0297607
732 -0.0175786,-0.160714,0.0319929
733 -0.0153464,-0.160714,0.0386893
734 -0.0133929,-0.160714,0.0386893
735 -0.015625,-0.25,0.0446429
736 -0.0175786,-0.25,0.046875
737 -0.0153464,-0.25,0.0535714
738 -0.0133929,-0.25,0.0535714
739 -0.0133929,-0.25,0.0535714
740 -0.00557857,-0.25,0.0535714
741 0.00557857,-0.25,0.0535714
742 0.0133929,-0.25,0.0535714
743 -0.0133929,-0.46425,0.0892857
744 -0.00557857,-0.46425,0.0892857
745 0.00557857,-0.46425,0.0892857
746 0.0133929,-0.46425,0.0892857
747 -0.0446429,-0.678571,0.0535714
748 -0.00892857,-0.678571,0.0625
749 0.00892857,-0.678571,0.0625
750 0.0446429,-0.678571,0.0535714
751 -0.0446429,-0.857143,0.0357143
752 -0.00892857,-0.857143,0.0446429
753 0.00892857,-0.857143,0.0446429
754 0.0446429,-0.857143,0.0357143
755 0.0133929,-0.25,0.0535714
756 0.0153464,-0.25,0.0535714
757 0.0178571,-0.25,0.0463179
758 0.015625,-0.25,0.0446429
759 0.0133929,-0.46425,0.0892857
760 0.0153464,-0.464286,0.0892857
761 0.0178571,-0.46425,0.0820321
762 0.015625,-0.46425,0.0803571
763 0.0446429,-0.678571,0.0535714
764 0.0535714,-0.678571,0.0513393
765 0.0535714,-0.678571,0.0334821
766 0.0446429,-0.678571,0.0357143
767 0.0446429,-0.857143,0.0357143
768 0.0535714,-0.857143,0.0334821
769 0.0535714,-0.857143,0.015625
770 0.0446429,-0.857143,0.0178571
771 0.015625,-0.25,0.0446429
772 0.00669643,-0.25,0.0379464
773 -0.00781071,-0.25,0.0357143
774 -0.015625,-0.25,0.0446429
775 0.015625,-0.46425,0.0803571
776 0.00669643,-0.464286,0.0736607
777 -0.00781071,-0.46425,0.0714286
778 -0.015625,-0.46425,0.0803571
779 0.0446429,-0.678571,0.0357143
780 0.00892857,-0.678571,0.0446429
781 -0.00892857,-0.678571,0.0446429
782 -0.0446429,-0.678571,0.0357143
783 0.0446429,-0.857143,0.0178571
784 0.00892857,-0.857143,0.0267857
785 -0.00892857,-0.857143,0.0267857
786 -0.0446429,-0.857143,0.0178571
787 -0.015625,-0.25,0.0446429
788 -0.0175786,-0.25,0.046875
789 -0.0153464,-0.25,0.0535714
790 -0.0133929,-0.25,0.0535714
791 -0.015625,-0.46425,0.0803571
792 -0.0175786,-0.464286,0.0825893
793 -0.0153464,-0.464286,0.0892857
794 -0.0133929,-0.46425,0.0892857
795 -0.0446429,-0.678571,0.0357143
796 -0.0535714,-0.678571,0.0334821
797 -0.0535714,-0.678571,0.0513393
798 -0.0446429,-0.678571,0.0535714
799 -0.0446429,-0.857143,0.0178571
800 -0.0535714,-0.857143,0.015625
801 -0.0535714,-0.857143,0.0334821
802 -0.0446429,-0.857143,0.0357143
803 -0.0446429,-0.857143,0.0357143
804 -0.00892857,-0.857143,0.0446429
805 0.00892857,-0.857143,0.0446429
806 0.0446429,-0.857143,0.0357143
807 -0.0446429,-0.928571,0.0285714
808 -0.00892857,-0.928571,0.0375
809 0.00892857,-0.928571,0.0375
810 0.0446429,-0.928571,0.0285714
811 -0.0539286,-0.999643,0.0178571
812 0.000357143,-0.999643,0.0178571
813 0.0,-0.999643,0.0178571
814 0.0535714,-0.999643,0.0178571
815 -0.000357143,-1,0.0178571
816 0.000357143,-1,0.0178571
819 0.0446429,-0.857143,0.0357143
820 0.0535714,-0.857143,0.0334821
821 0.0535714,-0.857143,0.015625
822 0.0446429,-0.857143,0.0178571
823 0.0446429,-0.928571,0.0285714
824 0.0535714,-0.928571,0.0263393
825 0.0535714,-0.928571,0.00848214
826 0.0446429,-0.928571,0.0107143
827 0.0535714,-0.999643,0.0178571
828 0.0669643,-0.999643,0.0178571
829 0.0673214,-0.999643,0.0
830 0.0539286,-0.999643,0.0
835 0.0446429,-0.857143,0.0178571
836 0.00892857,-0.857143,0.0267857
837 -0.00892857,-0.857143,0.0267857
838 -0.0446429,-0.857143,0.0178571
839 0.0446429,-0.928571,0.0107143
840 0.00892857,-0.928571,0.0196429
841 -0.00892857,-0.928571,0.0196429
842 -0.0446429,-0.928571,0.0107143
843 0.0539286,-0.999643,0.0
844 0.000357143,-0.999643,0.0
845 -0.000357143,-0.999643,0.0
846 -0.0539286,-0.999643,0.0
851 -0.0446429,-0.857143,0.0178571
852 -0.0535714,-0.857143,0.015625
853 -0.0535714,-0.857143,0.0334821
854 -0.0446429,-0.857143,0.0357143
855 -0.0446429,-0.928571,0.0107143
856 -0.0535714,-0.928571,0.00848214
857 -0.0535714,-0.928571,0.0263393
858 -0.0446429,-0.928571,0.0285714
859 -0.0539286,-0.999643,0.0
860 -0.0673214,-0.999643,0.0
861 -0.0675,-0.999643,0.0178571
862 -0.0539286,-0.999643,0.0178571
865 -0.000535714,-1,0.0178571
866 -0.000357143,-1,0.0178571