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