Last active
March 16, 2021 11:00
-
-
Save roolo/c43c11bab65a9d0b985c58000cde8702 to your computer and use it in GitHub Desktop.
Screw thread (závit) in blender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from math import * | |
def createMeshFromData(name, origin, verts, edges, faces): | |
# Create mesh and object | |
me = bpy.data.meshes.new(name+'Mesh') | |
ob = bpy.data.objects.new(name, me) | |
ob.location = origin | |
ob.show_name = False | |
# Link object to scene and make active | |
bpy.context.collection.objects.link(ob) | |
ob.select_set(True) | |
# Create mesh from given verts, faces. | |
me.from_pydata(verts, edges, faces) | |
# Update mesh with new data | |
me.update() | |
# Parameters | |
VerticesPerLoop = 256 | |
Loops = 5 | |
R = 32.2 # Outer radius | |
r = 20 # Inner radius | |
# thread profile (h1, h2, h3, h4 of the 4 points defining the profile) | |
h1 = 4 | |
h2 = 1 | |
h3 = 4 | |
h4 = 5 | |
falloffRate = 2 | |
# Code | |
H = h1 + h2 + h3 + h4 | |
#build array of profile points | |
ProfilePoints = [] | |
ProfilePoints.append( [r, 0, 0] ) | |
ProfilePoints.append( [R, 0, h1] ) | |
if h2 > 0: | |
ProfilePoints.append( [R, 0, h1 + h2] ) | |
ProfilePoints.append( [r, 0, h1 + h2 + h3] ) | |
if h4 > 0: | |
ProfilePoints.append( [r, 0, h1 + h2 + h3 + h4] ) | |
N = len(ProfilePoints) | |
verts = [[0, 0, 0] for _ in range(N * (VerticesPerLoop + 1) * Loops)] | |
faces = [[0, 0, 0, 0] for _ in range(( N - 1) * VerticesPerLoop * Loops) ] | |
# go around a cirle. for each point in ProfilePoints array, create a vertex | |
angle = 0 | |
for i in range(VerticesPerLoop * Loops + 1): | |
for j in range(N): | |
angle = i * 2 * pi / VerticesPerLoop | |
# falloff applies to outer rings only | |
u = i / (VerticesPerLoop * Loops) | |
radius = r + (R - r) * (1 - 6*(pow(2 * u - 1, falloffRate * 4)/2 - pow(2 * u - 1, falloffRate * 6)/3)) if ProfilePoints[j][0] == R else r | |
x = radius * cos(angle) | |
y = radius * sin(angle) | |
z = ProfilePoints[j][2] + i / VerticesPerLoop * H | |
verts[N*i + j][0] = x | |
verts[N*i + j][1] = y | |
verts[N*i + j][2] = z | |
# now build face array | |
for i in range(VerticesPerLoop * Loops): | |
for j in range(N - 1): | |
faces[(N - 1) * i + j][0] = N * i + j | |
faces[(N - 1) * i + j][1] = N * i + 1 + j | |
faces[(N - 1) * i + j][2] = N * (i + 1) + 1 + j | |
faces[(N - 1) * i + j][3] = N * (i + 1) + j | |
createMeshFromData( 'Thread', [0, 0, 0], verts, [], faces ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what it looks like