"""Red dot (reflex) sight for the rifle's top rail, in the collection 'Sight Red Dot'. Run inside Blender with assault_reference_v13_iron_sights.blend open: exec(open(r'C:\\roblox\\hunter\\blender\\build_red_dot.py', encoding='utf-8').read()) Rifle space: +X toward the muzzle, +Z up. The rail's teeth end at Z 0.689 (rail X -2.52 .. 2.13). The window is a rectangular frame around a glass pane; the dot floats just behind the glass at DOT_Z. The glass has its own material ('Sight glass') so in Roblox it becomes its own MeshPart that can be made see-through. Re-running replaces the previous sight. """ import math import bpy import bmesh from mathutils import Matrix, Vector COLLECTION = bpy.data.collections['Sight Red Dot'] CX = -1.0 # middle of the sight along the rail DOT_Z = 1.0 # height of the window's centre (the aiming line) def material(name, color, metal=0.0, rough=0.5, emit=0.0): m = bpy.data.materials.get(name) or bpy.data.materials.new(name) m.use_nodes = True p = next(n for n in m.node_tree.nodes if n.type == 'BSDF_PRINCIPLED') p.inputs['Base Color'].default_value = (*color, 1) p.inputs['Metallic'].default_value = metal p.inputs['Roughness'].default_value = rough p.inputs['Emission Strength'].default_value = emit if emit: p.inputs['Emission Color'].default_value = (*color, 1) return m MAT = { 'frame': bpy.data.materials['Ceramic graphite'], 'metal': bpy.data.materials['Titanium panels'], 'cyan': bpy.data.materials['Cyan channel'], 'dark': bpy.data.materials['Rubber'], 'glass': material('Sight glass', (0.35, 0.75, 0.7), rough=0.05), 'dot': material('Red dot', (1.0, 0.02, 0.01), emit=6.0), } for o in list(COLLECTION.objects): bpy.data.objects.remove(o, do_unlink=True) def finish(name, bm, mat, bevel): me = bpy.data.meshes.new(name) bm.to_mesh(me) bm.free() me.materials.append(MAT[mat]) for p in me.polygons: p.use_smooth = True 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') 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 cylinder_y(name, center, radius, length, mat, bevel=0.008): """Knob: a cylinder whose axis runs across the rifle (Y).""" bm = bmesh.new() bmesh.ops.create_cone(bm, cap_ends=True, segments=20, radius1=radius, radius2=radius, depth=length) bmesh.ops.rotate(bm, verts=bm.verts, matrix=Matrix.Rotation(math.pi / 2, 3, 'X')) bmesh.ops.translate(bm, verts=bm.verts, vec=Vector(center)) return finish(name, bm, mat, bevel) # mount: a clamp on the rail box('Red dot mount', CX - 0.26, CX + 0.26, -0.2, 0.2, 0.66, 0.79, 'frame') for side, y in (('L', 1), ('R', -1)): box(f'Red dot clamp {side}', CX - 0.2, CX + 0.2, *sorted((0.2 * y, 0.24 * y)), 0.6, 0.74, 'frame', bevel=0.01) box(f'Red dot clamp screw {side}', CX - 0.04, CX + 0.04, *sorted((0.24 * y, 0.26 * y)), 0.63, 0.71, 'metal', bevel=0.006) # housing: a rectangular frame around the window, deeper at the bottom W_Y, W_Z0, W_Z1 = 0.15, DOT_Z - 0.16, DOT_Z + 0.16 # window half-width and height O_Y, O_Z0, O_Z1 = 0.21, 0.79, DOT_Z + 0.22 # outside of the frame X0, X1 = CX - 0.2, CX + 0.2 box('Red dot frame bottom', X0 - 0.06, X1, -O_Y, O_Y, O_Z0, W_Z0, 'metal') box('Red dot frame top', X0, X1 + 0.05, -O_Y, O_Y, W_Z1, O_Z1, 'metal') # top overhangs a bit: hood for side, y in (('L', 1), ('R', -1)): box(f'Red dot frame side {side}', X0, X1, *sorted((W_Y * y, O_Y * y)), W_Z0, W_Z1, 'metal') box(f'Red dot side stripe {side}', X0 + 0.06, X1 - 0.06, *sorted((O_Y * y, (O_Y + 0.006) * y)), DOT_Z - 0.03, DOT_Z + 0.03, 'cyan', bevel=0) # glass pane near the front, the dot just behind it (on the eye side) GX = X1 - 0.07 box('Red dot glass', GX - 0.012, GX + 0.012, -W_Y - 0.004, W_Y + 0.004, W_Z0 - 0.004, W_Z1 + 0.004, 'glass', bevel=0) box('Red dot emitter', X0 + 0.02, X0 + 0.08, -0.025, 0.025, W_Z0, W_Z0 + 0.025, 'dark', bevel=0.004) dot = bpy.data.meshes.new('Red dot dot') bm = bmesh.new() bmesh.ops.create_uvsphere(bm, u_segments=12, v_segments=8, radius=0.018) bmesh.ops.translate(bm, verts=bm.verts, vec=Vector((GX - 0.03, 0, DOT_Z))) finish('Red dot dot', bm, 'dot', 0) # brightness knob on the right side, battery cap on the left cylinder_y('Red dot knob', (CX - 0.05, -(O_Y + 0.035), O_Z0 + 0.09), 0.065, 0.07, 'frame') box('Red dot knob mark', CX - 0.06, CX - 0.04, -(O_Y + 0.072), -(O_Y + 0.068), O_Z0 + 0.1, O_Z0 + 0.145, 'cyan', bevel=0) cylinder_y('Red dot battery cap', (CX - 0.05, O_Y + 0.025, O_Z0 + 0.09), 0.06, 0.05, 'metal') RED_DOT = sorted(o.name for o in COLLECTION.objects) print(RED_DOT)