"""Iron sights for the assault-rifle version of the rifle (replaces the optical scope). Run inside Blender with assault_reference_v13_iron_sights.blend open (scope objects already removed): exec(open(r'C:\\roblox\\hunter\\blender\\build_iron_sights.py', encoding='utf-8').read()) Rifle space: +X toward the muzzle, +Z up, Y across. The top rail's teeth end at Z 0.689 and the rail runs X -2.52 .. 2.13. Both sights sit on the rail; the rear aperture's centre and the tip of the front post are on one sight line at Z = SIGHT_Z. Re-running replaces the previous sights. """ import bpy import bmesh from mathutils import Matrix, Vector SIGHT_Z = 0.95 COLLECTION = bpy.data.collections['Rifle V4'] MAT = {k: bpy.data.materials[v] for k, v in { 'frame': 'Ceramic graphite', 'metal': 'Titanium panels', 'glow': 'Amber channel', 'dark': 'Rubber'}.items()} for o in [o for o in COLLECTION.all_objects if o.name.startswith('Iron sight')]: bpy.data.objects.remove(o, do_unlink=True) def finish(name, bm, mat, bevel=0.012): me = bpy.data.meshes.new(name) bm.to_mesh(me) bm.free() me.materials.append(MAT[mat]) ob = bpy.data.objects.new(name, me) COLLECTION.objects.link(ob) if bevel: m = ob.modifiers.new('Bevel', 'BEVEL') m.width = bevel m.segments = 2 m.limit_method = 'ANGLE' ob.modifiers.new('WeightedNormal', 'WEIGHTED_NORMAL') for p in me.polygons: p.use_smooth = True return ob def box(name, x0, x1, y0, y1, z0, z1, mat, bevel=0.012): bm = bmesh.new() bmesh.ops.create_cube(bm, size=1.0) bmesh.ops.transform(bm, verts=bm.verts, matrix=Matrix.Diagonal((x1 - x0, y1 - y0, z1 - z0, 1))) bmesh.ops.translate(bm, verts=bm.verts, vec=Vector(((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2))) return finish(name, bm, mat, bevel) def ring(name, center, outer, inner, length, mat): """Tube along X: an aperture.""" bm = bmesh.new() seg = 24 rings = [] for x in (-length / 2, length / 2): for r in (outer, inner): rings.append([bm.verts.new((x, r * __import__('math').cos(a * 6.283185 / seg), r * __import__('math').sin(a * 6.283185 / seg))) for a in range(seg)]) o0, i0, o1, i1 = rings for k in range(seg): n = (k + 1) % seg bm.faces.new((o0[k], o0[n], o1[n], o1[k])) # outside bm.faces.new((i0[n], i0[k], i1[k], i1[n])) # inside of the hole bm.faces.new((o0[n], o0[k], i0[k], i0[n])) # rear face bm.faces.new((o1[k], o1[n], i1[n], i1[k])) # front face bmesh.ops.recalc_face_normals(bm, faces=bm.faces) bmesh.ops.translate(bm, verts=bm.verts, vec=Vector(center)) return finish(name, bm, mat, bevel=0.006) # ---------------------------------------------------------------- rear sight (aperture) RX = -2.2 box('Iron sight rear base', RX - 0.25, RX + 0.25, -0.2, 0.2, 0.66, 0.80, 'frame') box('Iron sight rear base groove', RX - 0.26, RX + 0.26, -0.21, 0.21, 0.70, 0.72, 'dark', bevel=0) for side, y in (('L', 1), ('R', -1)): box(f'Iron sight rear ear {side}', RX - 0.11, RX + 0.11, *sorted((0.13 * y, 0.2 * y)), 0.80, 1.08, 'metal') box('Iron sight rear post', RX - 0.04, RX + 0.04, -0.035, 0.035, 0.80, SIGHT_Z - 0.06, 'metal', bevel=0.006) ring('Iron sight rear aperture', (RX, 0, SIGHT_Z), 0.085, 0.04, 0.06, 'metal') for side, y in (('L', 1), ('R', -1)): # tritium-style dots beside the aperture, facing the eye box(f'Iron sight rear dot {side}', RX - 0.115, RX - 0.105, 0.155 * y - 0.018, 0.155 * y + 0.018, SIGHT_Z - 0.018, SIGHT_Z + 0.018, 'glow', bevel=0) # ---------------------------------------------------------------- front sight (hooded post) FX = 1.9 box('Iron sight front base', FX - 0.18, FX + 0.18, -0.2, 0.2, 0.66, 0.80, 'frame') for side, y in (('L', 1), ('R', -1)): box(f'Iron sight front ear {side}', FX - 0.08, FX + 0.08, *sorted((0.115 * y, 0.175 * y)), 0.80, 1.12, 'metal') box('Iron sight front post', FX - 0.03, FX + 0.03, -0.024, 0.024, 0.80, SIGHT_Z - 0.01, 'frame', bevel=0.005) box('Iron sight front dot', FX - 0.032, FX + 0.032, -0.026, 0.026, SIGHT_Z - 0.01, SIGHT_Z + 0.025, 'glow', bevel=0) IRON_SIGHTS = sorted(o.name for o in COLLECTION.all_objects if o.name.startswith('Iron sight')) print(IRON_SIGHTS)