1 # SPDX-License-Identifier: GPL-2.0-or-later
2 # Author: Sjaak-de-Draak
6 "description": "Create different types of triangles",
7 "author": "Sjaak-de-Draak",
10 "location": "View3D > Add > Mesh",
11 "warning": "First Version",
12 "doc_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/"
14 "category": "Add Mesh",
18 This script provides a triangle mesh primitive
19 and a toolbar menu to further specify settings
24 from mathutils
import Vector
25 from bpy
.types
import Operator
26 from bpy
.props
import (
34 # Check if we are in edit mode
37 if (bpy
.context
.active_object
.mode
== 'EDIT'):
43 # Check if we are in edit mode (cuz we don't want this when creating a new Mesh)
44 # If we are then toggle back to object mode
45 # Check if there are active objects
46 if bpy
.context
.active_object
is not None:
47 # Only the active object should be in edit mode
48 if (bpy
.context
.active_object
.mode
== 'EDIT'):
49 bpy
.ops
.object.editmode_toggle()
52 class MakeTriangle(Operator
):
53 bl_idname
= "mesh.make_triangle"
55 bl_description
= "Construct different types of Triangle Meshes"
56 bl_options
= {"REGISTER", "UNDO"}
66 ('ISOSCELES', "Isosceles", "Two equal sides", 0),
67 ('EQUILATERAL', "Equilateral", "Three equal sides and angles (60°)", 1),
68 ('ISOSCELESRIGHTANGLE', "Isosceles right angled", "90° angle and two equal sides", 2),
69 ('SCALENERIGHTANGLE', "Scalene right angled", "90° angle, no equal sides", 3)
72 ('DEFAULT', "Normal", "1 Tri(angle) face", 0),
73 ('TRIANGLES', "3 Tri faces", "4 Vertices & 3 Tri(angle) faces", 1),
74 ('QUADS', "3 Quad faces", "7 Vertices & 3 Quad faces", 2),
75 ('SAFEQUADS', "6 Quad faces", "12 Vertices & 6 Quad faces", 3)
78 # add definitions for some manipulation buttons
81 description
="Draw on the other side of the X axis (Mirror on Y axis)",
86 description
="Draw on the other side of the Y axis (Mirror on X axis)",
91 description
="Triangle scale",
95 triangleType
: EnumProperty(
96 items
=triangleTypeList
,
98 description
="Triangle Type"
100 triangleFace
: EnumProperty(
101 items
=triangleFaceList
,
103 description
="Triangle Face Types"
105 at_3Dcursor
: BoolProperty(
106 name
="Use 3D Cursor",
107 description
="Draw the triangle where the 3D cursor is",
111 def draw(self
, context
):
114 col
= layout
.column(align
=True)
115 col
.prop(self
, "triangleType", text
="Type")
116 col
.prop(self
, "scale")
117 col
.prop(self
, "triangleFace", text
="Face")
119 col
= layout
.column(align
=True)
120 col
.prop(self
, "at_3Dcursor", text
="3D Cursor", toggle
=True)
122 row
= col
.row(align
=True)
123 row
.prop(self
, "flipX", toggle
=True)
124 row
.prop(self
, "flipY", toggle
=True)
126 def drawBasicTriangleShape(self
):
127 # set everything to 0
132 Xsign
= -1 if self
.flipX
else 1
133 Ysign
= -1 if self
.flipY
else 1
135 # Isosceles (2 equal sides)
136 if (self
.triangleType
== 'ISOSCELES'):
137 # below a simple triangle containing 2 triangles with 1:2 side ratio
138 Ya
= (1 * Ysign
* scale
)
139 A
= Vector([0.0, Ya
, 0.0])
140 Xb
= (0.5 * Xsign
* scale
)
141 B
= Vector([Xb
, 0.0, 0.0])
142 Xc
= (-0.5 * Xsign
* scale
)
143 C
= Vector([Xc
, 0.0, 0.0])
148 self
.Vertices
= [A
, B
, C
, ]
152 # Equilateral (all sides equal)
153 if (self
.triangleType
== 'EQUILATERAL'):
154 Ya
= (math
.sqrt(0.75) * Ysign
* scale
)
155 A
= Vector([0.0, Ya
, 0.0])
156 Xb
= (0.5 * Xsign
* scale
)
157 B
= Vector([Xb
, 0.0, 0.0])
158 Xc
= (-0.5 * Xsign
* scale
)
159 C
= Vector([Xc
, 0.0, 0.0])
164 self
.Vertices
= [A
, B
, C
, ]
168 # Isosceles right angled (1, 1, sqrt(2))
169 if (self
.triangleType
== 'ISOSCELESRIGHTANGLE'):
170 Ya
= (1 * Ysign
* scale
)
171 A
= Vector([0.0, Ya
, 0.0])
173 B
= Vector([Xb
, 0.0, 0.0])
174 Xc
= (1 * Xsign
* scale
)
175 C
= Vector([Xc
, 0.0, 0.0])
180 self
.Vertices
= [A
, B
, C
, ]
183 # Scalene right angled (3, 4, 5)
184 if (self
.triangleType
== 'SCALENERIGHTANGLE'):
185 Ya
= (1 * Ysign
* scale
)
186 A
= Vector([0.0, Ya
, 0.0])
188 B
= Vector([Xb
, 0.0, 0.0])
189 Xc
= (0.75 * Xsign
* scale
)
190 C
= Vector([Xc
, 0.0, 0.0])
195 self
.Vertices
= [A
, B
, C
, ]
200 def addFaces(self
, fType
=None):
205 if (self
.triangleFace
== 'DEFAULT'):
206 self
.Faces
= [[0, 1, 2]]
209 if (self
.triangleFace
== 'TRIANGLES'):
210 A
= Vector([0.0, Ya
, 0.0])
211 B
= Vector([Xb
, 0.0, 0.0])
212 C
= Vector([Xc
, 0.0, 0.0])
213 D
= Vector([((A
.x
+ B
.x
+ C
.x
) / 3), ((A
.y
+ B
.y
+ C
.y
) / 3), ((A
.z
+ B
.z
+ C
.z
) / 3)])
215 self
.Vertices
= [A
, B
, C
, D
, ]
216 self
.Faces
= [[0, 1, 3], [1, 2, 3], [2, 0, 3]]
219 if (self
.triangleFace
== 'QUADS'):
220 A
= Vector([0.0, Ya
, 0.0])
221 B
= Vector([Xb
, 0.0, 0.0])
222 C
= Vector([Xc
, 0.0, 0.0])
223 D
= Vector([((A
.x
+ B
.x
+ C
.x
) / 3), ((A
.y
+ B
.y
+ C
.y
) / 3), ((A
.z
+ B
.z
+ C
.z
) / 3)])
228 self
.Vertices
= [A
, AB
, B
, BC
, C
, AC
, D
, ]
229 self
.Faces
= [[0, 1, 6, 5], [1, 2, 3, 6], [3, 4, 5, 6]]
232 if (self
.triangleFace
== 'SAFEQUADS'):
233 A
= Vector([0.0, Ya
, 0.0])
234 B
= Vector([Xb
, 0.0, 0.0])
235 C
= Vector([Xc
, 0.0, 0.0])
236 D
= Vector([((A
.x
+ B
.x
+ C
.x
) / 3), ((A
.y
+ B
.y
+ C
.y
) / 3), ((A
.z
+ B
.z
+ C
.z
) / 3)])
241 AAB
= AB
.lerp(A
, 0.5)
242 AAC
= AC
.lerp(A
, 0.5)
243 BBA
= AB
.lerp(B
, 0.5)
244 BBC
= BC
.lerp(B
, 0.5)
245 BCC
= BC
.lerp(C
, 0.5)
246 CCA
= AC
.lerp(C
, 0.5)
248 self
.Vertices
= [A
, AAB
, BBA
, B
, BBC
, BC
, BCC
, C
, CCA
, AAC
, D
, E
, ]
249 self
.Faces
= [[0, 1, 11, 9], [1, 2, 10, 11], [2, 3, 4, 10],
250 [4, 5, 6, 10], [6, 7, 8, 10], [8, 9, 11, 10]]
255 def action_common(self
, context
):
257 # a triangle consists of 3 points: A, B, C
258 # a 'safer' subdividable triangle consists of 4 points: A, B, C, D
259 # a subdivide friendly triangle consists of 7 points: A, B, C, D, AB, AC, BC
260 # a truly subdivide friendly triangle consists of (3 x 4 = )12 points:
261 # A, B, C, D, E, BC, AAB, AAC, BBA, BBC, BCC, CCA
263 BasicShapeCreated
= False
264 ShapeFacesCreated
= False
268 # call the functions for creating the triangles and test if successful
270 BasicShapeCreated
= self
.drawBasicTriangleShape()
271 if (BasicShapeCreated
):
272 ShapeFacesCreated
= self
.addFaces()
273 if ShapeFacesCreated
:
277 NewMesh
= bpy
.data
.meshes
.new("Triangle")
278 NewMesh
.from_pydata(self
.Vertices
, [], self
.Faces
)
281 NewObj
= bpy
.data
.objects
.new("Triangle", NewMesh
)
282 context
.collection
.objects
.link(NewObj
)
284 # before doing the deselect make sure edit mode isn't active
286 bpy
.ops
.object.select_all(action
="DESELECT")
287 NewObj
.select_set(True)
288 context
.view_layer
.objects
.active
= NewObj
290 if self
.at_3Dcursor
is True:
291 # we'll need to be sure there is actually an object selected
292 if NewObj
.select_get() is True:
293 # we also have to check if we're considered to be in 3D View (view3d)
294 if bpy
.ops
.view3d
.snap_selected_to_cursor
.poll() is True:
295 bpy
.ops
.view3d
.snap_selected_to_cursor()
297 # as we weren't considered to be in 3D View
298 # the object couldn't be moved to the 3D cursor
299 # so to avoid confusion we change the at_3Dcursor boolean to false
300 self
.at_3Dcursor
= False
303 self
.report({'WARNING'},
304 "Triangle could not be completed. (See Console for more Info)")
306 print("\n[Add Mesh Extra Objects]\n\nModule: add_mesh_triangle")
307 print("Triangle type: %s\n" % self
.triangleType
,
308 "Face type: %s\n" % self
.triangleFace
,
309 "Ya: %s, Xb: %s, Xc: %s\n" % (self
.Ya
, self
.Xb
, self
.Xc
),
310 "Vertices: %s\n" % self
.Vertices
,
311 "Faces: %s\n" % self
.Faces
)
313 def execute(self
, context
):
314 self
.action_common(context
)
317 def invoke(self
, context
, event
):
318 self
.action_common(context
)