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