/
add_gear.py
339 lines (254 loc) · 8.83 KB
/
add_gear.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import bpy
import bmesh
import mathutils
from bpy.types import Operator
from math import (
sin, cos, tan,
sqrt,
pi,
radians
)
from bpy.props import (
FloatProperty, IntProperty
)
def add_gear(m, z, h, ir):
if z < 2:
return False
r = m * z * cos(radians(20)) / 2 #Base Radius
R = m * (z / 2 + 1) #Tip Radius
psi = 360 * (pi / 2 + z * (tan(radians(20)) - radians(20))) / (pi * z) #Tooth thickness at base(angles)
phi = 360 / z
invx = str(round(r, 4)) + " * (cos(u) + u * sin(u))"; #X-equation
invy = str(round(r, 4)) + " * (sin(u) - u * cos(u))"; #Y-equation
invz = "0" #Z-equation
umin = 0.0
umax = sqrt((R * R) / (r * r) - 1)
ustep = 10.0
uwrap = False
vmin = 0.0
vmax = 0.0
vstep = 1.0
#Set pivot point type and location
bpy.context.screen.areas[4].spaces[0].pivot_point = 'CURSOR'
bpy.context.screen.areas[4].spaces[0].cursor_location = (0.0, 0.0, 0.0)
#Draw involute
bpy.ops.mesh.primitive_xyz_function_surface(
x_eq = invx,
y_eq = invy,
z_eq = invz,
range_u_min = umin,
range_u_max = umax,
range_u_step = ustep,
wrap_u = uwrap,
range_v_min = vmin,
range_v_max = vmax,
range_v_step = vstep
)
#Enter edit mode
bpy.ops.object.mode_set(mode = 'EDIT')
#Remove doubles
bpy.ops.mesh.remove_doubles()
#Init bmesh
me = bpy.context.active_object.data
bm = bmesh.from_edit_mesh(me)
#Select/unselect all vertices and edges
#param Boolean@select to select(True) or unselect(False) all verts and edges
#return None
def handle_all_verts_and_edges(select):
vertices = [e for e in bm.verts]
edges = [e for e in bm.edges]
for vert in vertices:
vert.select = select
for edge in edges:
edge.select = select
#Select/unselect all faces
#param Boolean@select to select(True) or unselect(False) all faces
#return None
def handle_all_faces(select):
faces = [e for e in bm.faces]
for face in faces:
face.select = select
#Set cursor to selected
#param None
#return None
def set_cursor_to_selected():
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
ctx = bpy.context.copy()
ctx['area'] = area
ctx['region'] = area.regions[-1]
bpy.ops.view3d.view_selected(ctx)
bpy.ops.view3d.snap_cursor_to_selected(ctx)
break
#Duplicate involute
bpy.ops.mesh.duplicate()
selected_involute = list(filter(lambda v: v.select, bm.verts))
#Scale the selected involute
scale = mathutils.Vector((1.0, -1.0, 1.0))
bmesh.ops.scale(
bm,
vec = scale,
verts = selected_involute
)
#Rotate the selected involute
center = bpy.context.scene.cursor_location
rot = mathutils.Euler((0.0, 0.0, radians(psi))).to_matrix()
bmesh.ops.rotate(
bm,
cent = center,
matrix = rot,
verts = selected_involute
)
#Connect the two top vertices of each involutes
vertices = [e for e in bm.verts]
handle_all_verts_and_edges(False)
vertices[10].select = True
vertices[11].select = True
bm.edges.new((vertices[10], vertices[11]))
#Create a new face from all of the vertices
handle_all_verts_and_edges(True)
bm.faces.new(vertices)
#Duplicate and rotate for a phi degree
handle_all_verts_and_edges(True)
handle_all_faces(True)
bpy.ops.mesh.duplicate()
selected_involute = list(filter(lambda v: v.select, bm.verts))
center = bpy.context.scene.cursor_location
rot = mathutils.Euler((0.0, 0.0, radians(phi))).to_matrix()
bmesh.ops.rotate(
bm,
cent = center,
matrix = rot,
verts = selected_involute
)
handle_all_faces(False)
#Draw a semi-circle between the two buttom vertices of the teeth
handle_all_verts_and_edges(False)
vertices = [e for e in bm.verts]
vertices[21].select = True
vertices[22].select = True
set_cursor_to_selected()
vertices[22].select = False
bpy.ops.mesh.spin(
steps = 16,
dupli = False,
angle = radians(180),
center = bpy.context.screen.areas[4].spaces[0].cursor_location,
axis = (0.0, 0.0, 1.0)
)
bpy.context.screen.areas[4].spaces[0].cursor_location = (0.0, 0.0, 0.0)
#Duplicate and rotate until a full round
handle_all_verts_and_edges(True)
handle_all_faces(True)
for i in range(z - 1):
bpy.ops.mesh.duplicate()
selected_involute = list(filter(lambda v: v.select, bm.verts))
center = bpy.context.scene.cursor_location
rot = mathutils.Euler((0.0, 0.0, radians(phi))).to_matrix()
bmesh.ops.rotate(
bm,
cent = center,
matrix = rot,
verts = selected_involute
)
#Remove doubles
handle_all_verts_and_edges(True)
handle_all_faces(True)
bpy.ops.mesh.remove_doubles()
#Extrude
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(0, 0, h)})
#Create inner circle and bridge with the base circle(vertices)
set_cursor_to_selected()
handle_all_verts_and_edges(False)
handle_all_faces(False)
verts_to_base_radius = list(filter(
lambda v:
round(sqrt(pow(v.co.x, 2) + pow(v.co.y, 2)), 4) <= round(r, 4) + 0.0001,
bm.verts))
bpy.ops.mesh.primitive_circle_add(
vertices = int(len(verts_to_base_radius) / 2),
radius = ir,
location = bpy.context.scene.cursor_location
)
for e in list(filter(lambda v: v.co.z == h, verts_to_base_radius)):
e.select = True
for edge in bm.edges:
if edge.verts[0].select and edge.verts[1].select:
edge.select = True
bpy.ops.mesh.bridge_edge_loops()
#Extrude the inner circle to the bottom and bridge as below
handle_all_verts_and_edges(False)
handle_all_faces(False)
verts_to_inner_circle = list(filter(
lambda v:
round(sqrt(pow(v.co.x, 2) + pow(v.co.y, 2)), 3) <= round(ir, 3) + 0.001,
bm.verts))
for e in list(filter(lambda v: v.co.z == h, verts_to_inner_circle)):
e.select = True
for edge in bm.edges:
if edge.verts[0].select and edge.verts[1].select:
edge.select = True
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(0, 0, -h)})
for e in list(filter(lambda v: v.co.z == 0, verts_to_base_radius)):
e.select = True
for edge in bm.edges:
if edge.verts[0].select and edge.verts[1].select:
edge.select = True
bpy.ops.mesh.bridge_edge_loops()
#Make normals consistent
handle_all_verts_and_edges(True)
handle_all_faces(True)
bpy.ops.mesh.normals_make_consistent()
#Make bevel
handle_all_verts_and_edges(False)
handle_all_faces(False)
count = 0
for v in bm.verts:
if round(sqrt(pow(v.co.x, 2) + pow(v.co.y, 2)), 4) >= round(R - 0.001, 4):
count += 1
if count == 3:
break
v.select = True
for edge in bm.edges:
if edge.verts[0].select and edge.verts[1].select:
edge.select = True
bpy.ops.mesh.select_mode(type="EDGE")
bpy.ops.mesh.select_similar(type='LENGTH')
bpy.ops.mesh.bevel(offset=0.6, segments=10)
bmesh.update_edit_mesh(me, True)
#Add EDGE_SPLIT modifier
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.shade_smooth()
bpy.ops.object.modifier_add(type='EDGE_SPLIT')
bpy.context.screen.areas[4].spaces[0].cursor_location = (0.0, 0.0, 0.0)
return True
class AddGear(Operator):
bl_idname = "mesh.involute_gear"
bl_label = "Add Involute Gear"
bl_description = "Create an involute gear"
bl_options = {'REGISTER', 'UNDO', 'PRESET'}
m = FloatProperty(
name = "Module of Gear",
description = "Module of the gear",
min = 0.1,
max = 100.0,
default = 1.0,
unit = 'LENGTH'
)
z = IntProperty(
name = "Number of Teeth",
description = "Number of Teeth on the gear",
min = 2,
max = 256,
default = 18
)
def draw(self, context):
layout = self.layout
box = layout.box()
box.prop(self, 'm')
box.prop(self, 'z')
def execute(self, context):
flag = add_gear(self.m, self.z)
if flag:
return {'FINISHED'}
return {'CANCELLED'}