add_curve_extra_objects: fix invalid identity checks
[blender-addons.git] / add_mesh_BoltFactory / Boltfactory.py
blob5009549e49fd85768ce2742020753d72334fa8c3
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 #####
20 import bpy
21 from mathutils import Matrix
22 from bpy.types import Operator
23 from bpy_extras.object_utils import AddObjectHelper
24 from bpy.props import (
25 BoolProperty,
26 EnumProperty,
27 FloatProperty,
28 IntProperty,
29 FloatVectorProperty,
30 StringProperty,
32 from . import createMesh
33 from bpy_extras import object_utils
36 class add_mesh_bolt(Operator, AddObjectHelper):
37 bl_idname = "mesh.bolt_add"
38 bl_label = "Add Bolt"
39 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
40 bl_description = "Construct many types of Bolts"
43 MAX_INPUT_NUMBER = 50
45 Bolt : BoolProperty(name = "Bolt",
46 default = True,
47 description = "Bolt")
49 #### change properties
50 name : StringProperty(name = "Name",
51 description = "Name")
53 change : BoolProperty(name = "Change",
54 default = False,
55 description = "change Bolt")
57 # Model Types
58 Model_Type_List = [('bf_Model_Bolt', 'BOLT', 'Bolt Model'),
59 ('bf_Model_Nut', 'NUT', 'Nut Model')]
60 bf_Model_Type: EnumProperty(
61 attr='bf_Model_Type',
62 name='Model',
63 description='Choose the type off model you would like',
64 items=Model_Type_List, default='bf_Model_Bolt'
66 # Head Types
67 Model_Type_List = [('bf_Head_Hex', 'HEX', 'Hex Head'),
68 ('bf_Head_Cap', 'CAP', 'Cap Head'),
69 ('bf_Head_Dome', 'DOME', 'Dome Head'),
70 ('bf_Head_Pan', 'PAN', 'Pan Head'),
71 ('bf_Head_CounterSink', 'COUNTER SINK', 'Counter Sink Head')]
72 bf_Head_Type: EnumProperty(
73 attr='bf_Head_Type',
74 name='Head',
75 description='Choose the type off Head you would like',
76 items=Model_Type_List, default='bf_Head_Hex'
78 # Bit Types
79 Bit_Type_List = [('bf_Bit_None', 'NONE', 'No Bit Type'),
80 ('bf_Bit_Allen', 'ALLEN', 'Allen Bit Type'),
81 ('bf_Bit_Philips', 'PHILLIPS', 'Phillips Bit Type')]
82 bf_Bit_Type: EnumProperty(
83 attr='bf_Bit_Type',
84 name='Bit Type',
85 description='Choose the type of bit to you would like',
86 items=Bit_Type_List, default='bf_Bit_None'
88 # Nut Types
89 Nut_Type_List = [('bf_Nut_Hex', 'HEX', 'Hex Nut'),
90 ('bf_Nut_Lock', 'LOCK', 'Lock Nut')]
91 bf_Nut_Type: EnumProperty(
92 attr='bf_Nut_Type',
93 name='Nut Type',
94 description='Choose the type of nut you would like',
95 items=Nut_Type_List, default='bf_Nut_Hex'
97 # Shank Types
98 bf_Shank_Length: FloatProperty(
99 attr='bf_Shank_Length',
100 name='Shank Length', default=0,
101 min=0, soft_min=0, max=MAX_INPUT_NUMBER,
102 description='Length of the unthreaded shank',
103 unit='LENGTH',
105 bf_Shank_Dia: FloatProperty(
106 attr='bf_Shank_Dia',
107 name='Shank Dia', default=3,
108 min=0, soft_min=0,
109 max=MAX_INPUT_NUMBER,
110 description='Diameter of the shank',
111 unit='LENGTH',
113 bf_Phillips_Bit_Depth: FloatProperty(
114 attr='bf_Phillips_Bit_Depth',
115 name='Bit Depth', default=1.1431535482406616,
116 min=0, soft_min=0,
117 max=MAX_INPUT_NUMBER,
118 description='Depth of the Phillips Bit',
119 unit='LENGTH',
121 bf_Allen_Bit_Depth: FloatProperty(
122 attr='bf_Allen_Bit_Depth',
123 name='Bit Depth', default=1.5,
124 min=0, soft_min=0,
125 max=MAX_INPUT_NUMBER,
126 description='Depth of the Allen Bit',
127 unit='LENGTH',
129 bf_Allen_Bit_Flat_Distance: FloatProperty(
130 attr='bf_Allen_Bit_Flat_Distance',
131 name='Flat Dist', default=2.5,
132 min=0, soft_min=0,
133 max=MAX_INPUT_NUMBER,
134 description='Flat Distance of the Allen Bit',
135 unit='LENGTH',
137 bf_Hex_Head_Height: FloatProperty(
138 attr='bf_Hex_Head_Height',
139 name='Head Height', default=2,
140 min=0, soft_min=0, max=MAX_INPUT_NUMBER,
141 description='Height of the Hex Head',
142 unit='LENGTH',
144 bf_Hex_Head_Flat_Distance: FloatProperty(
145 attr='bf_Hex_Head_Flat_Distance',
146 name='Flat Dist', default=5.5,
147 min=0, soft_min=0,
148 max=MAX_INPUT_NUMBER,
149 description='Flat Distance of the Hex Head',
150 unit='LENGTH',
152 bf_CounterSink_Head_Dia: FloatProperty(
153 attr='bf_CounterSink_Head_Dia',
154 name='Head Dia', default=6.300000190734863,
155 min=0, soft_min=0,
156 max=MAX_INPUT_NUMBER,
157 description='Diameter of the Counter Sink Head',
158 unit='LENGTH',
160 bf_Cap_Head_Height: FloatProperty(
161 attr='bf_Cap_Head_Height',
162 name='Head Height', default=3,
163 min=0, soft_min=0,
164 max=MAX_INPUT_NUMBER,
165 description='Height of the Cap Head',
166 unit='LENGTH',
168 bf_Cap_Head_Dia: FloatProperty(
169 attr='bf_Cap_Head_Dia',
170 name='Head Dia', default=5.5,
171 min=0, soft_min=0,
172 max=MAX_INPUT_NUMBER,
173 description='Diameter of the Cap Head',
174 unit='LENGTH',
176 bf_Dome_Head_Dia: FloatProperty(
177 attr='bf_Dome_Head_Dia',
178 name='Dome Head Dia', default=5.599999904632568,
179 min=0, soft_min=0,
180 max=MAX_INPUT_NUMBER,
181 description='Length of the unthreaded shank',
182 unit='LENGTH',
184 bf_Pan_Head_Dia: FloatProperty(
185 attr='bf_Pan_Head_Dia',
186 name='Pan Head Dia', default=5.599999904632568,
187 min=0, soft_min=0,
188 max=MAX_INPUT_NUMBER,
189 description='Diameter of the Pan Head',
190 unit='LENGTH',
193 bf_Philips_Bit_Dia: FloatProperty(
194 attr='bf_Philips_Bit_Dia',
195 name='Bit Dia', default=1.8199999332427979,
196 min=0, soft_min=0,
197 max=MAX_INPUT_NUMBER,
198 description='Diameter of the Philips Bit',
199 unit='LENGTH',
202 bf_Thread_Length: FloatProperty(
203 attr='bf_Thread_Length',
204 name='Thread Length', default=6,
205 min=0, soft_min=0,
206 max=MAX_INPUT_NUMBER,
207 description='Length of the Thread',
208 unit='LENGTH',
211 bf_Major_Dia: FloatProperty(
212 attr='bf_Major_Dia',
213 name='Major Dia', default=3,
214 min=0, soft_min=0,
215 max=MAX_INPUT_NUMBER,
216 description='Outside diameter of the Thread',
217 unit='LENGTH',
220 bf_Pitch: FloatProperty(
221 attr='bf_Pitch',
222 name='Pitch', default=0.3499999940395355,
223 min=0.1, soft_min=0.1,
224 max=7.0,
225 description='Pitch if the thread',
226 unit='LENGTH',
228 bf_Minor_Dia: FloatProperty(
229 attr='bf_Minor_Dia',
230 name='Minor Dia', default=2.6211137771606445,
231 min=0, soft_min=0,
232 max=MAX_INPUT_NUMBER,
233 description='Inside diameter of the Thread',
234 unit='LENGTH',
236 bf_Crest_Percent: IntProperty(
237 attr='bf_Crest_Percent',
238 name='Crest Percent', default=10,
239 min=1, soft_min=1,
240 max=90,
241 description='Percent of the pitch that makes up the Crest',
243 bf_Root_Percent: IntProperty(
244 attr='bf_Root_Percent',
245 name='Root Percent', default=10,
246 min=1, soft_min=1,
247 max=90,
248 description='Percent of the pitch that makes up the Root',
250 bf_Div_Count: IntProperty(
251 attr='bf_Div_Count',
252 name='Div count', default=36,
253 min=4, soft_min=4,
254 max=4096,
255 description='Div count determine circle resolution',
257 bf_Hex_Nut_Height: FloatProperty(
258 attr='bf_Hex_Nut_Height',
259 name='Hex Nut Height', default=2.4000000953674316,
260 min=0, soft_min=0,
261 max=MAX_INPUT_NUMBER,
262 description='Height of the Hex Nut',
263 unit='LENGTH',
265 bf_Hex_Nut_Flat_Distance: FloatProperty(
266 attr='bf_Hex_Nut_Flat_Distance',
267 name='Hex Nut Flat Dist', default=5.5,
268 min=0, soft_min=0,
269 max=MAX_INPUT_NUMBER,
270 description='Flat distance of the Hex Nut',
271 unit='LENGTH',
274 def draw(self, context):
275 layout = self.layout
276 col = layout.column()
278 # ENUMS
279 col.prop(self, 'bf_Model_Type')
280 col.separator()
282 # Bit
283 if self.bf_Model_Type == 'bf_Model_Bolt':
284 col.prop(self, 'bf_Bit_Type')
285 if self.bf_Bit_Type == 'bf_Bit_None':
286 pass
287 elif self.bf_Bit_Type == 'bf_Bit_Allen':
288 col.prop(self, 'bf_Allen_Bit_Depth')
289 col.prop(self, 'bf_Allen_Bit_Flat_Distance')
290 elif self.bf_Bit_Type == 'bf_Bit_Philips':
291 col.prop(self, 'bf_Phillips_Bit_Depth')
292 col.prop(self, 'bf_Philips_Bit_Dia')
293 col.separator()
294 # Head
295 if self.bf_Model_Type == 'bf_Model_Bolt':
296 col.prop(self, 'bf_Head_Type')
297 if self.bf_Head_Type == 'bf_Head_Hex':
298 col.prop(self, 'bf_Hex_Head_Height')
299 col.prop(self, 'bf_Hex_Head_Flat_Distance')
300 elif self.bf_Head_Type == 'bf_Head_Cap':
301 col.prop(self, 'bf_Cap_Head_Height')
302 col.prop(self, 'bf_Cap_Head_Dia')
303 elif self.bf_Head_Type == 'bf_Head_Dome':
304 col.prop(self, 'bf_Dome_Head_Dia')
305 elif self.bf_Head_Type == 'bf_Head_Pan':
306 col.prop(self, 'bf_Pan_Head_Dia')
307 elif self.bf_Head_Type == 'bf_Head_CounterSink':
308 col.prop(self, 'bf_CounterSink_Head_Dia')
309 col.separator()
310 # Shank
311 if self.bf_Model_Type == 'bf_Model_Bolt':
312 col.label(text='Shank')
313 col.prop(self, 'bf_Shank_Length')
314 col.prop(self, 'bf_Shank_Dia')
315 col.separator()
316 # Nut
317 if self.bf_Model_Type == 'bf_Model_Nut':
318 col.prop(self, 'bf_Nut_Type')
319 col.prop(self, 'bf_Hex_Nut_Height')
320 col.prop(self, 'bf_Hex_Nut_Flat_Distance')
321 # Thread
322 col.label(text='Thread')
323 if self.bf_Model_Type == 'bf_Model_Bolt':
324 col.prop(self, 'bf_Thread_Length')
325 col.prop(self, 'bf_Major_Dia')
326 col.prop(self, 'bf_Minor_Dia')
327 col.prop(self, 'bf_Pitch')
328 col.prop(self, 'bf_Crest_Percent')
329 col.prop(self, 'bf_Root_Percent')
330 col.prop(self, 'bf_Div_Count')
332 # generic transform props
333 col.separator()
334 col.prop(self, 'align')
335 col.prop(self, 'location')
336 col.prop(self, 'rotation')
338 @classmethod
339 def poll(cls, context):
340 return context.scene is not None
342 def execute(self, context):
344 if bpy.context.mode == "OBJECT":
345 if context.selected_objects != [] and context.active_object and \
346 ('Bolt' in context.active_object.data.keys()) and (self.change == True):
347 obj = context.active_object
348 oldmesh = obj.data
349 oldmeshname = obj.data.name
350 mesh = createMesh.Create_New_Mesh(self, context)
351 obj.data = mesh
352 try:
353 bpy.ops.object.vertex_group_remove(all=True)
354 except:
355 pass
357 for material in oldmesh.materials:
358 obj.data.materials.append(material)
360 bpy.data.meshes.remove(oldmesh)
361 obj.data.name = oldmeshname
362 else:
363 mesh = createMesh.Create_New_Mesh(self, context)
364 obj = object_utils.object_data_add(context, mesh, operator=None)
366 obj.data["Bolt"] = True
367 obj.data["change"] = False
368 for prm in BoltParameters():
369 obj.data[prm] = getattr(self, prm)
371 if bpy.context.mode == "EDIT_MESH":
372 active_object = context.active_object
373 name_active_object = active_object.name
374 bpy.ops.object.mode_set(mode='OBJECT')
375 mesh = createMesh.Create_New_Mesh(self, context)
376 obj = object_utils.object_data_add(context, mesh, operator=None)
378 obj.select_set(True)
379 active_object.select_set(True)
380 bpy.ops.object.join()
381 context.active_object.name = name_active_object
382 bpy.ops.object.mode_set(mode='EDIT')
384 return {'FINISHED'}
386 def invoke(self, context, event):
387 self.execute(context)
389 return {'FINISHED'}
391 # Register:
392 def Bolt_contex_menu(self, context):
393 bl_label = 'Change'
395 obj = context.object
396 layout = self.layout
398 if 'Bolt' in obj.data.keys():
399 props = layout.operator("mesh.bolt_add", text="Change Bolt")
400 props.change = True
401 for prm in BoltParameters():
402 setattr(props, prm, obj.data[prm])
403 layout.separator()
405 def menu_func_bolt(self, context):
406 layout = self.layout
407 layout.separator()
408 oper = self.layout.operator(add_mesh_bolt.bl_idname, text="Bolt", icon="MOD_SCREW")
409 oper.change = False
411 classes = (
412 add_mesh_bolt,
417 def register():
418 for cls in classes:
419 bpy.utils.register_class(cls)
420 bpy.types.VIEW3D_MT_mesh_add.append(menu_func_bolt)
421 bpy.types.VIEW3D_MT_object_context_menu.prepend(Bolt_contex_menu)
424 def unregister():
425 bpy.types.VIEW3D_MT_object_context_menu.remove(Bolt_contex_menu)
426 bpy.types.VIEW3D_MT_mesh_add.remove(menu_func_bolt)
427 for cls in reversed(classes):
428 bpy.utils.unregister_class(cls)
430 def BoltParameters():
431 BoltParameters = [
432 "bf_Model_Type",
433 "bf_Head_Type",
434 "bf_Bit_Type",
435 "bf_Nut_Type",
436 "bf_Shank_Length",
437 "bf_Shank_Dia",
438 "bf_Phillips_Bit_Depth",
439 "bf_Allen_Bit_Depth",
440 "bf_Allen_Bit_Flat_Distance",
441 "bf_Hex_Head_Height",
442 "bf_Hex_Head_Flat_Distance",
443 "bf_CounterSink_Head_Dia",
444 "bf_Cap_Head_Height",
445 "bf_Cap_Head_Dia",
446 "bf_Dome_Head_Dia",
447 "bf_Pan_Head_Dia",
448 "bf_Philips_Bit_Dia",
449 "bf_Thread_Length",
450 "bf_Major_Dia",
451 "bf_Pitch",
452 "bf_Minor_Dia",
453 "bf_Crest_Percent",
454 "bf_Root_Percent",
455 "bf_Div_Count",
456 "bf_Hex_Nut_Height",
457 "bf_Hex_Nut_Flat_Distance",
459 return BoltParameters