```python
import bpy, math, os
from math import sin, cos, radians, atan2, pi

# ---------------------------------------------------------------- helpers
def M(name, color, rough, metal=0.0, alpha=1.0):
    m = bpy.data.materials.new(name)
    m.use_nodes = True
    b = m.node_tree.nodes["Principled BSDF"]
    b.inputs["Base Color"].default_value = (*color, 1.0)
    b.inputs["Roughness"].default_value = rough
    b.inputs["Metallic"].default_value = metal
    if alpha < 1.0:
        b.inputs["Alpha"].default_value = alpha
        try:
            m.blend_method = 'BLEND'
        except Exception:
            pass
        try:
            m.use_backface_culling = False
        except Exception:
            pass
    return m

mat_alu      = M("Aluminum", (0.75, 0.76, 0.78), 0.28, 1.0)
mat_dark     = M("AnodizedDark", (0.035, 0.037, 0.042), 0.42, 0.85)
mat_strut    = M("TitaniumStrut", (0.52, 0.53, 0.57), 0.38, 1.0)
mat_steel    = M("Steel", (0.6, 0.6, 0.62), 0.22, 1.0)
mat_accent   = M("Accent", (0.85, 0.33, 0.06), 0.45, 0.0)
mat_black    = M("BlackPoly", (0.015, 0.015, 0.017), 0.5, 0.0)
mat_membrane = M("Membrane", (0.82, 0.88, 0.92), 0.12, 0.0, alpha=0.28)
mat_desk     = M("Desk", (0.86, 0.84, 0.80), 0.45, 0.0)
mat_floor    = M("Floor", (0.5, 0.5, 0.52), 0.6, 0.0)
mat_wall     = M("Wall", (0.62, 0.63, 0.65), 0.7, 0.0)
mat_brass    = M("Brass", (0.78, 0.58, 0.22), 0.28, 1.0)

def _finish(o, name, mat, smooth=False, bevel=0.0):
    o.name = name
    if mat:
        if o.data.materials:
            o.data.materials[0] = mat
        else:
            o.data.materials.append(mat)
    if smooth:
        for p in o.data.polygons:
            p.use_smooth = True
        try:
            bpy.ops.object.shade_auto_smooth(angle=radians(40))
        except Exception:
            pass
    if bevel > 0:
        md = o.modifiers.new("Bev", 'BEVEL')
        md.width = bevel
        md.segments = 2
        md.limit_method = 'ANGLE'
        md.angle_limit = radians(45)
    return o

def add_cyl(name, r, d, loc, rot=(0, 0, 0), mat=mat_alu, verts=48, bevel=0.0):
    bpy.ops.mesh.primitive_cylinder_add(vertices=verts, radius=r, depth=d,
                                        location=loc, rotation=rot)
    return _finish(bpy.context.active_object, name, mat, smooth=True, bevel=bevel)

def add_box(name, size, loc, rot=(0, 0, 0), mat=mat_alu, bevel=0.0):
    bpy.ops.mesh.primitive_cube_add(size=1, location=loc, rotation=rot)
    o = bpy.context.active_object
    o.scale = size
    bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
    return _finish(o, name, mat, smooth=False, bevel=bevel)

def add_hex(name, r, d, loc, rot=(0, 0, 0), mat=mat_steel):
    bpy.ops.mesh.primitive_cylinder_add(vertices=6, radius=r, depth=d,
                                        location=loc, rotation=rot)
    return _finish(bpy.context.active_object, name, mat)

def frame(origin, theta, local):
    """local (x,y,z) -> world; local z rotated by theta about world Y."""
    x, y, z = local
    st, ct = sin(theta), cos(theta)
    return (origin[0] + x * ct + z * st,
            origin[1] + y,
            origin[2] - x * st + z * ct)

# ---------------------------------------------------------------- kinematics
S  = (0.02, 0.0, 0.19)                 # shoulder pivot
t1 = radians(40)                       # upper arm angle from vertical
L1 = 0.30
d1 = (sin(t1), 0, cos(t1))
E  = (S[0] + L1 * d1[0], 0.0, S[2] + L1 * d1[2])   # elbow

t2 = radians(115)
L2 = 0.26
d2 = (sin(t2), 0, cos(t2))
W  = (E[0] + L2 * d2[0], 0.0, E[2] + L2 * d2[2])   # wrist

t3 = radians(170)                      # gripper approach (near vertical down)
d3 = (sin(t3), 0, cos(t3))

TARGET = (0.50, 0.0, 0.0175)           # small object location

# ---------------------------------------------------------------- environment
add_box("DeskTop", (1.4, 0.85, 0.03), (0.18, 0.0, -0.015), mat=mat_desk, bevel=0.004)
for lx, ly in [(-0.45, -0.35), (-0.45, 0.35), (0.8, -0.35), (0.8, 0.35)]:
    add_cyl("DeskLeg", 0.022, 0.70, (lx, ly, -0.03 - 0.35), mat=mat_dark)
add_box("Floor", (8, 8, 0.02), (0, 0, -0.74), mat=mat_floor)
add_box("Wall", (8, 0.02, 5), (0, 1.0, 1.0), mat=mat_wall)

# small machined target object
add_cyl("TargetObj", 0.018, 0.035, TARGET, mat=mat_brass, bevel=0.0015)
add_cyl("TargetCap", 0.019, 0.005, (TARGET[0], TARGET[1], 0.036), mat=mat_dark)
add_hex("TargetHex", 0.007, 0.006, (TARGET[0], TARGET[1], 0.041), mat=mat_steel)

# ---------------------------------------------------------------- base assembly
add_cyl("BasePlate", 0.135, 0.018, (0, 0, 0.009), mat=mat_alu, verts=64, bevel=0.002)
for i in range(6):
    a = i * pi / 3 + 0.26
    add_hex("BaseBolt", 0.007, 0.008, (0.112 * cos(a), 0.112 * sin(a), 0.020))
add_cyl("BaseHousing", 0.066, 0.082, (0, 0, 0.059), mat=mat_dark, verts=64, bevel=0.002)
add_cyl("BaseRing", 0.068, 0.008, (0, 0, 0.026), mat=mat_alu, verts=64)
add_cyl("BaseSeam", 0.062, 0.005, (0, 0, 0.1025), mat=mat_accent, verts=64)
add_cyl("Platform", 0.058, 0.028, (0, 0, 0.119), mat=mat_alu, verts=64, bevel=0.002)
for i in range(8):
    a = i * pi / 4
    add_hex("PlatBolt", 0.0035, 0.005, (0.048 * cos(a), 0.048 * sin(a), 0.134))

# clevis to shoulder
for sy in (-0.038, 0.038):
    add_box("Clevis", (0.075, 0.013, 0.095), (0.012, sy, 0.147), mat=mat_alu, bevel=0.0015)
add_box("ClevisWeb", (0.06, 0.062, 0.014), (0.005, 0, 0.14), mat=mat_alu)

# ---------------------------------------------------------------- joint builder
def pitch_joint(tag, C, r, w, act_r, act_d):
    rot = (pi / 2, 0, 0)
    add_cyl(tag + "_Housing", r, w, C, rot, mat_dark, 48, 0.0015)
    add_cyl(tag + "_CapA", r * 1.05, 0.009, (C[0], C[1] - w / 2 - 0.004, C[2]), rot, mat_alu, 48)
    add_cyl(tag + "_CapB", r * 1.05, 0.009, (C[0], C[1] + w / 2 + 0.004, C[2]), rot, mat_alu, 48)
    # rotary actuator on +Y side
    add_cyl(tag + "_Act", act_r, act_d, (C[0], C[1] + w / 2 + act_d / 2, C[2]), rot, mat_alu, 48, 0.0015)
    add_cyl(tag + "_ActCap", act_r * 1.02, 0.008, (C[0], C[1] + w / 2 + act_d + 0.004, C[2]), rot, mat_dark, 48)
    add_cyl(tag + "_Axis", 0.008, w + 0.03, C, rot, mat_steel, 24)
    yb = C[1] + w / 2 + act_d + 0.009
    for i in range(6):
        a = i * pi / 3
        add_hex(tag + "_bolt", 0.003, 0.005,
                (C[0] + act_r * 0.62 * cos(a), yb, C[2] + act_r * 0.62 * sin(a)), rot)

pitch_joint("Shoulder", S, 0.046, 0.088, 0.040, 0.045)
pitch_joint("Elbow",    E, 0.035, 0.072, 0.030, 0.034)

# ---------------------------------------------------------------- optimized struts
def modifiers_apply(o):
    bpy.ops.object.select_all(action='DESELECT')
    o.select_set(True)
    bpy.context.view_layer.objects.active = o
    for md in list(o.modifiers):
        try:
            bpy.ops.object.modifier_apply(modifier=md.name)
        except Exception:
            pass

def make_strut(tag, A, B, width, thick, holes, membranes=()):
    dx, dz = B[0] - A[0], B[2] - A[2]
    L = math.hypot(dx, dz)
    th = atan2(dx, dz)
    # body built along +Z at origin
    bpy.ops.mesh.primitive_cube_add(size=1, location=(0, 0, L / 2))
    body = bpy.context.active_object
    body.scale = (width, thick, L)
    bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
    body.name = tag
    # end bosses (merged organically by voxel remesh)
    bosses = []
    for zc in (0.016, L - 0.016):
        bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius=min(width * 0.5, 0.024),
                                            depth=thick * 1.15, location=(0, 0, zc),
                                            rotation=(pi / 2, 0, 0))
        bosses.append(bpy.context.active_object)
    bpy.ops.object.select_all(action='DESELECT')
    for b in bosses:
        b.select_set(True)
    body.select_set(True)
    bpy.context.view_layer.objects.active = body
    bpy.ops.object.join()
    # hole cutters
    cutters = []
    for (f, r, el) in holes:
        bpy.ops.mesh.primitive_cylinder_add(vertices=40, radius=r, depth=thick * 3,
                                            location=(0, 0, f * L), rotation=(pi / 2, 0, 0))
        c = bpy.context.active_object
        c.scale = (0.8, el, 1.0)
        cutters.append(c)
        bm = body.modifiers.new("Cut", 'BOOLEAN')
        bm.operation = 'DIFFERENCE'
        bm.object = c
    rm = body.modifiers.new("Voxel", 'REMESH')
    rm.mode = 'VOXEL'
    rm.voxel_size = 0.0045
    sm = body.modifiers.new("Smooth", 'SMOOTH')
    sm.factor = 0.9
    sm.iterations = 6
    modifiers_apply(body)
    bpy.ops.object.select_all(action='DESELECT')
    for c in cutters:
        c.select_set(True)
    bpy.ops.object.delete()
    for p in body.data.polygons:
        p.use_smooth = True
    if body.data.materials:
        body.data.materials[0] = mat_strut
    else:
        body.data.materials.append(mat_strut)
    body.rotation_euler = (0, th, 0)
    body.location = A
    # machined interface plates at both ends
    for zc in (0.004, L - 0.004):
        add_box(tag + "_Plate", (width * 1.18, thick * 1.25, 0.008),
                frame(A, th, (0, 0, zc)), (0, th, 0), mat_alu, bevel=0.0012)
    # translucent membranes across selected openings
    for mi in membranes:
        f, r, el = holes[mi]
        add_box(tag + "_Membrane", (width * 0.86, 0.0012, 2 * r * el + 0.008),
                frame(A, th, (0, 0, f * L)), (0, th, 0), mat_membrane)
    return body, th, L

A1 = (S[0] + d1[0] * 0.058, 0, S[2] + d1[2] * 0.058)
B1 = (E[0] - d1[0] * 0.048, 0, E[2] - d1[2] * 0.048)
make_strut("StrutUpper", A1, B1, 0.066, 0.036,
           [(0.36, 0.017, 1.6), (0.68, 0.018, 1.6)], membranes=(1,))

A2 = (E[0] + d2[0] * 0.046, 0, E[2] + d2[2] * 0.046)
B2 = (W[0] - d2[0] * 0.036, 0, W[2] - d2[2] * 0.036)
make_strut("StrutFore", A2, B2, 0.052, 0.030,
           [(0.35, 0.013, 1.5), (0.68, 0.012, 1.5)], membranes=())

# ---------------------------------------------------------------- wrist + gripper
rotY = (pi / 2, 0, 0)
add_cyl("WristHousing", 0.027, 0.056, W, rotY, mat_dark, 48, 0.0012)
add_cyl("WristCapA", 0.029, 0.008, (W[0], W[1] - 0.032, W[2]), rotY, mat_alu)
add_cyl("WristCapB", 0.029, 0.008, (W[0], W[1] + 0.032, W[2]), rotY, mat_alu)
add_cyl("WristAxis", 0.006, 0.08, W, rotY, mat_steel, 24)

rot3 = (0, t3, 0)
add_cyl("RollHousing", 0.021, 0.05, frame(W, t3, (0, 0, 0.055)), rot3, mat_alu, 48, 0.0012)
add_cyl("RollRing",    0.022, 0.006, frame(W, t3, (0, 0, 0.082)), rot3, mat_accent, 48)
add_cyl("GripPlate",   0.025, 0.008, frame(W, t3, (0, 0, 0.089)), rot3, mat_dark, 48)
for i in range(4):
    a = i * pi / 2 + pi / 4
    add_hex("GripPlateBolt", 0.0025, 0.004,
            frame(W, t3, (0.017 * cos(a), 0.017 * sin(a), 0.093)), rot3)

add_box("Palm", (0.036, 0.066, 0.034), frame(W, t3, (0, 0, 0.110)), rot3, mat_alu, bevel=0.0018)
add_box("GripMotor", (0.02, 0.032, 0.02), frame(W, t3, (0.028, 0, 0.110)), rot3, mat_dark, bevel=0.0012)
add_cyl("LeadScrew", 0.004, 0.06, frame(W, t3, (0, 0, 0.129)), rotY, mat_steel, 20)

for sy in (-1, 1):
    y0 = sy * 0.024
    add_box("Slider", (0.018, 0.014, 0.022), frame(W, t3, (0, y0, 0.132)), rot3, mat_dark, bevel=0.001)
    add_box("Finger", (0.013, 0.009, 0.062), frame(W, t3, (0, sy * 0.021, 0.166)), rot3, mat_alu, bevel=0.0012)
    add_cyl("Pivot", 0.0035, 0.016, frame(W, t3, (0, sy * 0.021, 0.140)), rotY, mat_steel, 20)
    add_box("Pad", (0.011, 0.0035, 0.024), frame(W, t3, (0, sy * 0.0155, 0.183)), rot3, mat_black)
    add_box("PadTip", (0.008, 0.0035, 0.006), frame(W, t3, (0, sy * 0.0155, 0.194)), rot3, mat_accent)

# ---------------------------------------------------------------- cable routing
mid1 = (S[0] + 0.5 * L1 * d1[0], 0, S[2] + 0.5 * L1 * d1[2])
mid2 = (E[0] + 0.5 * L2 * d2[0], 0, E[2] + 0.5 * L2 * d2[2])
pts = [(-0.095, 0.04, 0.012),
       (-0.055, 0.04, 0.10),
       (S[0] - 0.048, 0.045, S[2] + 0.03),
       (mid1[0] - 0.034, 0.048, mid1[2] + 0.029),
       (E[0] - 0.015, 0.05, E[2] + 0.035),
       (mid2[0] + 0.017, 0.05, mid2[2] + 0.036),
       (W[0] + 0.002, 0.045, W[2] + 0.022)]
cur = bpy.data.curves.new("Cable", 'CURVE')
cur.dimensions = '3D'
cur.bevel_depth = 0.0035
cur.bevel_resolution = 4
sp = cur.splines.new('BEZIER')
sp.bezier_points.add(len(pts) - 1)
for bp, p in zip(sp.bezier_points, pts):
    bp.co = p
    bp.handle_left_type = bp.handle_right_type = 'AUTO'
cab = bpy.data.objects.new("Cable", cur)
bpy.context.collection.objects.link(cab)
bpy.ops.object.select_all(action='DESELECT')
cab.select_set(True)
bpy.context.view_layer.objects.active = cab
bpy.ops.object.convert(target='MESH')
cab = bpy.context.active_object
cab.data.materials.append(mat_black)
for p in cab.data.polygons:
    p.use_smooth = True
# small cable clips
for cp in (pts[2], pts[3], pts[5]):
    add_box("Clip", (0.012, 0.01, 0.012), cp, (0, 0, 0), mat_dark)

# ---------------------------------------------------------------- lighting & camera
tgt = bpy.data.objects.new("AimTarget", None)
tgt.location = (0.24, 0.0, 0.24)
bpy.context.collection.objects.link(tgt)

def area_light(name, loc, energy, size):
    ld = bpy.data.lights.new(name, 'AREA')
    ld.energy = energy
    ld.size = size
    lo = bpy.data.objects.new(name, ld)
    lo.location = loc
    bpy.context.collection.objects.link(lo)
    c = lo.constraints.new('TRACK_TO')
    c.target = tgt
    c.track_axis = 'TRACK_NEGATIVE_Z'
    c.up_axis = 'UP_Y'
    return lo

area_light("Key",  (1.0, -1.15, 1.05), 320, 1.2)
area_light("Fill", (-1.2, -0.7, 0.6),   90, 1.6)
area_light("Rim",  (-0.35, 0.9, 1.0),  180, 0.8)

sd = bpy.data.lights.new("Sun", 'SUN')
sd.energy = 1.2
sd.angle = radians(8)
so = bpy.data.objects.new("Sun", sd)
so.location = (1.5, -1.0, 2.0)
bpy.context.collection.objects.link(so)
sc = so.constraints.new('TRACK_TO')
sc.target = tgt
sc.track_axis = 'TRACK_NEGATIVE_Z'
sc.up_axis = 'UP_Y'

world = bpy.data.worlds.new("Studio")
world.use_nodes = True
bg = world.node_tree.nodes.get("Background")
bg.inputs[0].default_value = (0.55, 0.57, 0.60, 1.0)
bg.inputs[1].default_value = 0.25
bpy.context.scene.world = world

camd = bpy.data.cameras.new("Camera")
camd.lens = 58
cam = bpy.data.objects.new("Camera", camd)
cam.location = (1.12, -1.02, 0.52)
bpy.context.collection.objects.link(cam)
cc = cam.constraints.new('TRACK_TO')
cc.target = tgt
cc.track_axis = 'TRACK_NEGATIVE_Z'
cc.up_axis = 'UP_Y'
bpy.context.scene.camera = cam

# ---------------------------------------------------------------- render settings
sc = bpy.context.scene
sc.render.engine = 'CYCLES'
try:
    sc.cycles.samples = 64
    sc.cycles.use_denoising = True
except Exception:
    pass
sc.render.resolution_x = 1536
sc.render.resolution_y = 1536

# ---------------------------------------------------------------- save
try:
    path = bpy.data.filepath or os.path.join(os.getcwd(), "robotic_arm.blend")
    bpy.ops.wm.save_as_mainfile(filepath=path)
except Exception:
    pass
```