1 # GPL # "author": Paulo_Gomes
4 from mathutils
import Quaternion
, Vector
5 from math
import cos
, sin
, pi
6 from bpy
.props
import (
12 from bpy_extras
import object_utils
15 # Create a new mesh (object) from verts/edges/faces
16 # verts/edges/faces ... List of vertices/edges/faces for the
17 # new mesh (as used in from_pydata)
18 # name ... Name of the new mesh (& object)
20 def create_mesh_object(context
, verts
, edges
, faces
, name
):
23 mesh
= bpy
.data
.meshes
.new(name
)
25 # Make a mesh from a list of verts/edges/faces
26 mesh
.from_pydata(verts
, edges
, faces
)
28 # Update mesh geometry after adding stuff
31 from bpy_extras
import object_utils
32 return object_utils
.object_data_add(context
, mesh
, operator
=None)
35 # A very simple "bridge" tool
37 def createFaces(vertIdx1
, vertIdx2
, closed
=False, flipped
=False):
40 if not vertIdx1
or not vertIdx2
:
43 if len(vertIdx1
) < 2 and len(vertIdx2
) < 2:
47 if (len(vertIdx1
) != len(vertIdx2
)):
48 if (len(vertIdx1
) == 1 and len(vertIdx2
) > 1):
56 # Bridge the start with the end
63 face
.append(vertIdx1
[total
- 1])
67 face
= [vertIdx2
[0], vertIdx1
[0]]
69 face
.append(vertIdx1
[total
- 1])
70 face
.append(vertIdx2
[total
- 1])
73 # Bridge the rest of the faces
74 for num
in range(total
- 1):
77 face
= [vertIdx2
[num
], vertIdx1
[0], vertIdx2
[num
+ 1]]
79 face
= [vertIdx2
[num
], vertIdx1
[num
],
80 vertIdx1
[num
+ 1], vertIdx2
[num
+ 1]]
84 face
= [vertIdx1
[0], vertIdx2
[num
], vertIdx2
[num
+ 1]]
86 face
= [vertIdx1
[num
], vertIdx2
[num
],
87 vertIdx2
[num
+ 1], vertIdx1
[num
+ 1]]
93 def add_twisted_torus(major_rad
, minor_rad
, major_seg
, minor_seg
, twists
):
95 z_axis
= (0.0, 0.0, 1.0)
101 for major_index
in range(major_seg
):
102 quat
= Quaternion(z_axis
, (major_index
/ major_seg
) * PI_2
)
103 rot_twists
= PI_2
* major_index
/ major_seg
* twists
107 # Create section ring
108 for minor_index
in range(minor_seg
):
109 angle
= (PI_2
* minor_index
/ minor_seg
) + rot_twists
112 major_rad
+ (cos(angle
) * minor_rad
),
114 sin(angle
) * minor_rad
))
117 edgeloop
.append(len(verts
))
120 # Remember very first edgeloop
122 edgeloop_first
= edgeloop
124 # Bridge last with current ring
126 f
= createFaces(edgeloop_prev
, edgeloop
, closed
=True)
129 edgeloop_prev
= edgeloop
131 # Bridge first and last ring
132 f
= createFaces(edgeloop_prev
, edgeloop_first
, closed
=True)
138 class AddTwistedTorus(bpy
.types
.Operator
, object_utils
.AddObjectHelper
):
139 bl_idname
= "mesh.primitive_twisted_torus_add"
140 bl_label
= "Add Twisted Torus"
141 bl_description
= "Construct a twisted torus mesh"
142 bl_options
= {'REGISTER', 'UNDO', 'PRESET'}
144 TwistedTorus
: BoolProperty(name
= "TwistedTorus",
146 description
= "TwistedTorus")
147 change
: BoolProperty(name
= "Change",
149 description
= "change TwistedTorus")
151 major_radius
: FloatProperty(
153 description
="Radius from the origin to the"
154 " center of the cross section",
159 minor_radius
: FloatProperty(
161 description
="Radius of the torus' cross section",
166 major_segments
: IntProperty(
167 name
="Major Segments",
168 description
="Number of segments for the main ring of the torus",
173 minor_segments
: IntProperty(
174 name
="Minor Segments",
175 description
="Number of segments for the minor ring of the torus",
182 description
="Number of twists of the torus",
187 use_abso
: BoolProperty(
188 name
="Use Int/Ext Controls",
189 description
="Use the Int/Ext controls for torus dimensions",
192 abso_major_rad
: FloatProperty(
193 name
="Exterior Radius",
194 description
="Total Exterior Radius of the torus",
199 abso_minor_rad
: FloatProperty(
200 name
="Inside Radius",
201 description
="Total Interior Radius of the torus",
207 def draw(self
, context
):
210 layout
.prop(self
, 'major_radius', expand
=True)
211 layout
.prop(self
, 'minor_radius', expand
=True)
212 layout
.prop(self
, 'major_segments', expand
=True)
213 layout
.prop(self
, 'minor_segments', expand
=True)
214 layout
.prop(self
, 'twists', expand
=True)
215 layout
.prop(self
, 'use_abso', expand
=True)
216 layout
.prop(self
, 'abso_major_rad', expand
=True)
217 layout
.prop(self
, 'abso_minor_rad', expand
=True)
219 if self
.change
== False:
220 col
= layout
.column(align
=True)
221 col
.prop(self
, 'align', expand
=True)
222 col
= layout
.column(align
=True)
223 col
.prop(self
, 'location', expand
=True)
224 col
= layout
.column(align
=True)
225 col
.prop(self
, 'rotation', expand
=True)
227 def execute(self
, context
):
228 # turn off 'Enter Edit Mode'
229 use_enter_edit_mode
= bpy
.context
.preferences
.edit
.use_enter_edit_mode
230 bpy
.context
.preferences
.edit
.use_enter_edit_mode
= False
232 if self
.use_abso
is True:
233 extra_helper
= (self
.abso_major_rad
- self
.abso_minor_rad
) * 0.5
234 self
.major_radius
= self
.abso_minor_rad
+ extra_helper
235 self
.minor_radius
= extra_helper
237 if bpy
.context
.mode
== "OBJECT":
238 if context
.selected_objects
!= [] and context
.active_object
and \
239 (context
.active_object
.data
is not None) and ('TwistedTorus' in context
.active_object
.data
.keys()) and \
240 (self
.change
== True):
241 obj
= context
.active_object
243 oldmeshname
= obj
.data
.name
244 verts
, faces
= add_twisted_torus(
251 mesh
= bpy
.data
.meshes
.new('TwistedTorus')
252 mesh
.from_pydata(verts
, [], faces
)
254 for material
in oldmesh
.materials
:
255 obj
.data
.materials
.append(material
)
256 bpy
.data
.meshes
.remove(oldmesh
)
257 obj
.data
.name
= oldmeshname
259 verts
, faces
= add_twisted_torus(
266 mesh
= bpy
.data
.meshes
.new('TwistedTorus')
267 mesh
.from_pydata(verts
, [], faces
)
268 obj
= object_utils
.object_data_add(context
, mesh
, operator
=self
)
270 obj
.data
["TwistedTorus"] = True
271 obj
.data
["change"] = False
272 for prm
in TwistedTorusParameters():
273 obj
.data
[prm
] = getattr(self
, prm
)
275 if bpy
.context
.mode
== "EDIT_MESH":
276 active_object
= context
.active_object
277 name_active_object
= active_object
.name
278 bpy
.ops
.object.mode_set(mode
='OBJECT')
279 verts
, faces
= add_twisted_torus(
286 mesh
= bpy
.data
.meshes
.new('TwistedTorus')
287 mesh
.from_pydata(verts
, [], faces
)
288 obj
= object_utils
.object_data_add(context
, mesh
, operator
=self
)
290 active_object
.select_set(True)
291 bpy
.context
.view_layer
.objects
.active
= active_object
292 bpy
.ops
.object.join()
293 context
.active_object
.name
= name_active_object
294 bpy
.ops
.object.mode_set(mode
='EDIT')
296 if use_enter_edit_mode
:
297 bpy
.ops
.object.mode_set(mode
= 'EDIT')
299 # restore pre operator state
300 bpy
.context
.preferences
.edit
.use_enter_edit_mode
= use_enter_edit_mode
304 def TwistedTorusParameters():
305 TwistedTorusParameters
= [
315 return TwistedTorusParameters