1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
22 from math import cos, sin, pi
25 def add_torus(major_rad, minor_rad, major_seg, minor_seg):
26 Vector = mathutils.Vector
27 Quaternion = mathutils.Quaternion
30 z_axis = 0.0, 0.0, 1.0
35 tot_verts = major_seg * minor_seg
36 for major_index in range(major_seg):
37 quat = Quaternion(z_axis, (major_index / major_seg) * PI_2)
39 for minor_index in range(minor_seg):
40 angle = 2 * pi * minor_index / minor_seg
42 vec = Vector((major_rad + (cos(angle) * minor_rad), 0.0,
43 (sin(angle) * minor_rad))) * quat
47 if minor_index + 1 == minor_seg:
48 i2 = (major_index) * minor_seg
66 faces.extend([i1, i3, i4, i2])
68 faces.extend([i2, i1, i3, i4])
74 from bpy.props import *
77 class AddTorus(bpy.types.Operator):
78 '''Add a torus mesh'''
79 bl_idname = "mesh.primitive_torus_add"
80 bl_label = "Add Torus"
81 bl_options = {'REGISTER', 'UNDO'}
83 major_radius = FloatProperty(name="Major Radius",
84 description="Radius from the origin to the center of the cross sections",
85 default=1.0, min=0.01, max=100.0)
86 minor_radius = FloatProperty(name="Minor Radius",
87 description="Radius of the torus' cross section",
88 default=0.25, min=0.01, max=100.0)
89 major_segments = IntProperty(name="Major Segments",
90 description="Number of segments for the main ring of the torus",
91 default=48, min=3, max=256)
92 minor_segments = IntProperty(name="Minor Segments",
93 description="Number of segments for the minor ring of the torus",
94 default=12, min=3, max=256)
95 use_abso = BoolProperty(name="Use Int+Ext Controls",
96 description="Use the Int / Ext controls for torus dimensions",
98 abso_major_rad = FloatProperty(name="Exterior Radius",
99 description="Total Exterior Radius of the torus",
100 default=1.0, min=0.01, max=100.0)
101 abso_minor_rad = FloatProperty(name="Inside Radius",
102 description="Total Interior Radius of the torus",
103 default=0.5, min=0.01, max=100.0)
105 # generic transform props
106 view_align = BoolProperty(name="Align to View",
108 location = FloatVectorProperty(name="Location")
109 rotation = FloatVectorProperty(name="Rotation")
112 def execute(self, context):
114 if self.use_abso == True:
115 extra_helper = (self.abso_major_rad - self.abso_minor_rad) * 0.5
116 self.major_radius = self.abso_minor_rad + extra_helper
117 self.minor_radius = extra_helper
119 verts_loc, faces = add_torus(self.major_radius,
124 mesh = bpy.data.meshes.new("Torus")
126 mesh.vertices.add(len(verts_loc) // 3)
127 mesh.faces.add(len(faces) // 4)
129 mesh.vertices.foreach_set("co", verts_loc)
130 mesh.faces.foreach_set("vertices_raw", faces)
133 import add_object_utils
134 add_object_utils.object_data_add(context, mesh, operator=self)
139 def menu_func(self, context):
140 self.layout.operator(AddTorus.bl_idname, text="Torus", icon='MESH_TORUS')
144 bpy.types.INFO_MT_mesh_add.append(menu_func)
148 bpy.types.INFO_MT_mesh_add.remove(menu_func)
150 if __name__ == "__main__":