```python
import bpy
import math
from mathutils import Vector

# ------------------------------------------------------------
# Scene setup
# ------------------------------------------------------------

bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

scene = bpy.context.scene
scene.render.engine = 'BLENDER_EEVEE_NEXT'
scene.render.resolution_x = 768
scene.render.resolution_y = 768
scene.render.resolution_percentage = 100
scene.render.image_settings.file_format = 'PNG'
scene.render.film_transparent = False
scene.render.resolution_percentage = 100

scene.view_settings.look = 'AgX - Medium High Contrast'
scene.view_settings.exposure = 0.35

world = bpy.data.worlds.new("Neutral Studio World")
scene.world = world
world.use_nodes = True
world.node_tree.nodes["Background"].inputs["Color"].default_value = (0.055, 0.065, 0.075, 1)
world.node_tree.nodes["Background"].inputs["Strength"].default_value = 0.28

# ------------------------------------------------------------
# Materials
# ------------------------------------------------------------

def material(name, color, metallic=0.0, roughness=0.4, alpha=1.0,
             transmission=0.0, coat=0.0):
    m = bpy.data.materials.new(name)
    m.diffuse_color = (*color, alpha)
    m.use_nodes = True
    bsdf = m.node_tree.nodes.get("Principled BSDF")
    bsdf.inputs["Base Color"].default_value = (*color, 1)
    bsdf.inputs["Metallic"].default_value = metallic
    bsdf.inputs["Roughness"].default_value = roughness
    if "Coat Weight" in bsdf.inputs:
        bsdf.inputs["Coat Weight"].default_value = coat
    if "Transmission Weight" in bsdf.inputs:
        bsdf.inputs["Transmission Weight"].default_value = transmission
    if "Alpha" in bsdf.inputs:
        bsdf.inputs["Alpha"].default_value = alpha
    if alpha < 1:
        m.surface_render_method = 'DITHERED'
        m.use_transparency_overlap = False
    return m

MAT_TOPO = material("Topology Optimized Titanium",
                    (0.28, 0.43, 0.43), metallic=0.88, roughness=0.23, coat=0.2)
MAT_TOPO_EDGE = material("Topology Highlight",
                         (0.42, 0.63, 0.60), metallic=0.8, roughness=0.2)
MAT_SILVER = material("Precision Machined Aluminum",
                      (0.64, 0.69, 0.72), metallic=0.92, roughness=0.22)
MAT_DARK = material("Actuator Graphite",
                    (0.045, 0.06, 0.07), metallic=0.74, roughness=0.25)
MAT_BLACK = material("Cable and Pads",
                     (0.012, 0.016, 0.019), metallic=0.15, roughness=0.32)
MAT_ACCENT = material("Interface Accent",
                      (0.12, 0.45, 0.56), metallic=0.75, roughness=0.23)
MAT_BOLT = material("Fasteners",
                    (0.11, 0.13, 0.14), metallic=0.92, roughness=0.18)
MAT_MEMBRANE = material("Clear Reinforced TPU",
                        (0.28, 0.68, 0.74), metallic=0.0, roughness=0.2,
                        alpha=0.25, transmission=0.5, coat=0.25)
MAT_DESK = material("Warm White Lab Bench",
                    (0.69, 0.70, 0.68), metallic=0.0, roughness=0.3, coat=0.25)
MAT_EDGE = material("Desk Edge",
                    (0.13, 0.15, 0.16), metallic=0.2, roughness=0.3)
MAT_WALL = material("Studio Background",
                    (0.20, 0.225, 0.24), metallic=0.0, roughness=0.68)
MAT_BRASS = material("Machined Sample",
                     (0.68, 0.38, 0.09), metallic=0.9, roughness=0.18)
MAT_RED = material("Status Indicator",
                   (0.8, 0.035, 0.018), metallic=0.1, roughness=0.25)

# ------------------------------------------------------------
# Geometry helpers
# ------------------------------------------------------------

def smooth(obj):
    if obj and obj.type == 'MESH':
        for poly in obj.data.polygons:
            poly.use_smooth = True

def bevel(obj, amount=0.04, segments=3):
    mod = obj.modifiers.new("Machined edge radii", 'BEVEL')
    mod.width = amount
    mod.segments = segments
    mod.limit_method = 'ANGLE'
    return obj

def cube_obj(name, loc, scale, mat, bevel_amt=0.0):
    bpy.ops.mesh.primitive_cube_add(location=loc)
    o = bpy.context.object
    o.name = name
    o.scale = (scale[0] / 2, scale[1] / 2, scale[2] / 2)
    bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
    if mat:
        o.data.materials.append(mat)
    if bevel_amt:
        bevel(o, bevel_amt, 3)
    return o

def cylinder_between(name, a, b, radius, mat, vertices=32, r2=None):
    a, b = Vector(a), Vector(b)
    d = b - a
    length = d.length
    if length < 1e-5:
        return None
    mid = (a + b) * 0.5
    if r2 is None:
        r2 = radius
    bpy.ops.mesh.primitive_cone_add(
        vertices=vertices, radius1=radius, radius2=r2,
        depth=length, location=mid
    )
    o = bpy.context.object
    o.name = name
    o.rotation_mode = 'QUATERNION'
    o.rotation_quaternion = d.to_track_quat('Z', 'Y')
    if mat:
        o.data.materials.append(mat)
    smooth(o)
    return o

def sphere(name, loc, radius, mat, scale=(1,1,1)):
    bpy.ops.mesh.primitive_uv_sphere_add(
        segments=28, ring_count=16, radius=radius, location=loc
    )
    o = bpy.context.object
    o.name = name
    o.scale = scale
    bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
    o.data.materials.append(mat)
    smooth(o)
    return o

def disk_z(name, loc, radius, depth, mat, vertices=48):
    bpy.ops.mesh.primitive_cylinder_add(
        vertices=vertices, radius=radius, depth=depth, location=loc
    )
    o = bpy.context.object
    o.name = name
    o.data.materials.append(mat)
    smooth(o)
    bevel(o, min(0.018, depth * 0.14), 2)
    return o

def box_between(name, a, b, width, depth, mat, bevel_amt=0.02):
    a, b = Vector(a), Vector(b)
    d = b - a
    length = d.length
    o = cube_obj(name, (a+b)*0.5, (width, depth, length), mat, bevel_amt)
    o.rotation_mode = 'QUATERNION'
    o.rotation_quaternion = d.to_track_quat('Z', 'Y')
    return o

def curve_tube(name, points, radius, mat):
    curve = bpy.data.curves.new(name, type='CURVE')
    curve.dimensions = '3D'
    curve.resolution_u = 2
    curve.bevel_depth = radius
    curve.bevel_resolution = 3
    spline = curve.splines.new('BEZIER')
    spline.bezier_points.add(len(points)-1)
    for bp, p in zip(spline.bezier_points, points):
        bp.co = p
        bp.handle_left_type = 'AUTO'
        bp.handle_right_type = 'AUTO'
    obj = bpy.data.objects.new(name, curve)
    bpy.context.collection.objects.link(obj)
    obj.data.materials.append(mat)
    return obj

def panel_mesh(name, coords, mat, thickness=0.0):
    mesh = bpy.data.meshes.new(name)
    if len(coords) == 3:
        faces = [(0,1,2)]
    else:
        faces = [(0,1,2,3)]
    mesh.from_pydata(coords, [], faces)
    mesh.update()
    o = bpy.data.objects.new(name, mesh)
    bpy.context.collection.objects.link(o)
    o.data.materials.append(mat)
    if thickness:
        sol = o.modifiers.new("Film thickness", 'SOLIDIFY')
        sol.thickness = thickness
    return o

# ------------------------------------------------------------
# Studio and desk
# ------------------------------------------------------------

desk = cube_obj("Precision Lab Work Surface", (0, 0.2, -0.15),
                (5.8, 4.1, 0.30), MAT_DESK, 0.09)
cube_obj("Dark Front Edge", (0, -1.80, -0.16),
         (5.65, 0.11, 0.23), MAT_EDGE, 0.025)

# legs glimpsed under the table
for x in (-2.35, 2.35):
    for y in (-1.35, 1.55):
        cube_obj("Bench Leg", (x, y, -1.03), (0.16, 0.16, 1.75),
                 MAT_EDGE, 0.025)

# Seamless visual background
cube_obj("Rear Studio Wall", (0, 2.62, 1.7),
         (7.2, 0.12, 4.0), MAT_WALL, 0.03)
cube_obj("Left Architectural Panel", (-3.05, 0.9, 1.4),
         (0.10, 3.3, 3.2), MAT_WALL, 0.02)

# restrained wall light strips
strip_mat = material("Soft Wall Strip", (0.75, 0.86, 0.89),
                     metallic=0.0, roughness=0.25)
for x in (-2.15, 2.05):
    cube_obj("Wall Light Recess", (x, 2.545, 1.82),
             (0.09, 0.025, 1.75), strip_mat, 0.015)

# ------------------------------------------------------------
# Mechanical base: no lattice
# ------------------------------------------------------------

base_x = -1.55

disk_z("Base Isolation Pad", (base_x, 0, 0.035), 0.55, 0.07, MAT_BLACK)
disk_z("Bolted Base Flange", (base_x, 0, 0.10), 0.50, 0.12, MAT_SILVER)
disk_z("Rotary Yaw Bearing", (base_x, 0, 0.205), 0.42, 0.105, MAT_DARK)
disk_z("Rotating Platform", (base_x, 0, 0.315), 0.44, 0.12, MAT_SILVER)
disk_z("Yaw Interface Ring", (base_x, 0, 0.397), 0.34, 0.055, MAT_ACCENT)

# base fasteners
for i in range(8):
    a = 2*math.pi*i/8
    x = base_x + 0.405*math.cos(a)
    y = 0 + 0.405*math.sin(a)
    disk_z("Base Mount Bolt", (x, y, 0.177), 0.035, 0.025, MAT_BOLT, 20)

# shoulder pedestal and side cheeks
cube_obj("Shoulder Pedestal", (base_x, 0, 0.60),
         (0.42, 0.52, 0.48), MAT_DARK, 0.065)
cube_obj("Left Shoulder Cheek", (base_x, -0.255, 0.76),
         (0.45, 0.105, 0.55), MAT_SILVER, 0.045)
cube_obj("Right Shoulder Cheek", (base_x, 0.255, 0.76),
         (0.45, 0.105, 0.55), MAT_SILVER, 0.045)

# ------------------------------------------------------------
# Joint definitions
# ------------------------------------------------------------

P0 = Vector((-1.55, 0, 0.92))
P1 = Vector((-0.72, 0, 2.02))
P2 = Vector((0.38, 0, 1.78))
P3 = Vector((1.04, 0, 1.18))

def hinge_joint(name, p, radius, depth, accent=True):
    p = Vector(p)
    # Main actuator body, axis along Y
    cylinder_between(name+" Actuator",
                     p + Vector((0,-depth*0.34,0)),
                     p + Vector((0, depth*0.34,0)),
                     radius*0.90, MAT_DARK, 40)
    cylinder_between(name+" Front Flange",
                     p + Vector((0,-depth*0.52,0)),
                     p + Vector((0,-depth*0.31,0)),
                     radius, MAT_SILVER, 40)
    cylinder_between(name+" Rear Flange",
                     p + Vector((0,depth*0.31,0)),
                     p + Vector((0,depth*0.52,0)),
                     radius, MAT_SILVER, 40)
    cylinder_between(name+" Bearing Cap",
                     p + Vector((0,-depth*0.59,0)),
                     p + Vector((0,-depth*0.50,0)),
                     radius*0.61, MAT_ACCENT if accent else MAT_BOLT, 36)
    cylinder_between(name+" Axle",
                     p + Vector((0,-depth*0.64,0)),
                     p + Vector((0,depth*0.64,0)),
                     radius*0.25, MAT_BOLT, 28)
    # flange bolts on both faces
    for side in (-1,1):
        y = side*depth*0.575
        for i in range(6):
            a = 2*math.pi*i/6
            q = p + Vector((radius*0.72*math.cos(a), y,
                            radius*0.72*math.sin(a)))
            cylinder_between(name+" Flange Bolt",
                             q+Vector((0,-0.018,0)),
                             q+Vector((0,0.018,0)),
                             radius*0.075, MAT_BOLT, 16)

hinge_joint("Shoulder J1", P0, 0.30, 0.62)
hinge_joint("Elbow J2", P1, 0.255, 0.54)
hinge_joint("Wrist Pitch J3", P2, 0.205, 0.45)
hinge_joint("Wrist Bend J4", P3, 0.165, 0.38)

# ------------------------------------------------------------
# Organic topology optimized structural links
# ------------------------------------------------------------

def topology_link(name, A, B, width_a, width_b, depth, membrane=False):
    A, B = Vector(A), Vector(B)
    axis = (B-A).normalized()
    # In-plane normal, preserving the arm's planar mechanism
    normal = Vector((-axis.z, 0, axis.x)).normalized()
    side = Vector((0,1,0))
    length = (B-A).length

    # Keep optimized metal away from rotary housings
    start = A + axis * min(0.23, length*0.16)
    end = B - axis * min(0.21, length*0.15)
    span = end-start

    ts = [0.0, 0.22, 0.50, 0.78, 1.0]
    widths = [
        width_a*0.38,
        width_a,
        (width_a+width_b)*0.54,
        width_b,
        width_b*0.36
    ]

    layers = {}
    for sy in (-1,1):
        yoff = side * sy * depth*0.5
        U, L = [], []
        for t,w in zip(ts,widths):
            c = start + span*t + yoff
            U.append(c + normal*w)
            L.append(c - normal*w)
        layers[sy] = (U,L)

        # Outer grown rails, intentionally tapered by segment
        for i in range(4):
            taper = 1.0 - 0.12*i
            cylinder_between(name+" Upper grown rail", U[i], U[i+1],
                             0.052*taper, MAT_TOPO, 24, 0.046*taper)
            cylinder_between(name+" Lower grown rail", L[i], L[i+1],
                             0.055*taper, MAT_TOPO, 24, 0.047*taper)

        # Branch network creates controlled triangular / lenticular openings
        cylinder_between(name+" Root upper branch", U[0], L[1],
                         0.048, MAT_TOPO, 24, 0.041)
        cylinder_between(name+" Root lower branch", L[0], U[1],
                         0.048, MAT_TOPO, 24, 0.041)
        cylinder_between(name+" Load diagonal A", U[1], L[2],
                         0.045, MAT_TOPO, 24, 0.038)
        cylinder_between(name+" Load diagonal B", L[2], U[3],
                         0.041, MAT_TOPO, 24, 0.035)
        cylinder_between(name+" Tip upper branch", U[3], L[4],
                         0.038, MAT_TOPO, 24, 0.032)
        cylinder_between(name+" Tip lower branch", L[3], U[4],
                         0.038, MAT_TOPO, 24, 0.032)

        # Smooth branch transition nodes
        for i, q in enumerate(U+L):
            rr = 0.066 if i not in (0,4,5,9) else 0.058
            sphere(name+" Organic transition", q, rr, MAT_TOPO,
                   (1.15, 0.92, 1.15))

    # Through-depth members turn the form into a genuine spatial component
    Uf, Lf = layers[-1]
    Ub, Lb = layers[1]
    for i in range(5):
        rr = 0.039 if i in (1,2,3) else 0.044
        cylinder_between(name+" Upper transverse web", Uf[i], Ub[i],
                         rr, MAT_TOPO_EDGE, 20)
        cylinder_between(name+" Lower transverse web", Lf[i], Lb[i],
                         rr, MAT_TOPO_EDGE, 20)

    # Conventional replaceable interface plates remain distinct
    for p, vec, label, size in (
        (start, axis, "Root interface", width_a),
        (end, axis, "Tip interface", width_b)
    ):
        box_between(name+" "+label,
                    p-axis*0.055, p+axis*0.055,
                    size*1.55, depth*1.35, MAT_SILVER, 0.025)
        for sy in (-1,1):
            cylinder_between(name+" Interface fastener",
                             p + side*(sy*depth*0.72) - side*0.02,
                             p + side*(sy*depth*0.72) + side*0.02,
                             0.027, MAT_BOLT, 16)

    # One restrained clear membrane panel, deliberately inset on front layer
    if membrane:
        U, L = layers[-1]
        inset = side * -0.012
        panel_mesh(name+" Tensioned protective membrane",
                   [U[1]+inset, U[2]+inset, L[2]+inset, L[1]+inset],
                   MAT_MEMBRANE, 0.006)

topology_link("Upper Arm Optimized Strut", P0, P1, 0.245, 0.205, 0.22, True)
topology_link("Forearm Optimized Strut", P1, P2, 0.205, 0.165, 0.19, False)
topology_link("Distal Optimized Strut", P2, P3, 0.155, 0.115, 0.15, True)

# ------------------------------------------------------------
# Compact actuator/gearbox details and controlled cabling
# ------------------------------------------------------------

# Small gearbox covers positioned next to joints
for name, p, scale in (
    ("Shoulder Servo Cover", P0+Vector((-0.24,0.24,-0.03)), (0.22,0.12,0.27)),
    ("Elbow Servo Cover", P1+Vector((0.02,0.22,0.22)), (0.24,0.11,0.18)),
    ("Wrist Servo Cover", P2+Vector((0.02,0.19,0.16)), (0.18,0.10,0.14)),
):
    cube_obj(name, p, scale, MAT_DARK, 0.035)

# Recessed/slim electrical line hugging one side of structure
cable_points = [
    P0 + Vector((0,0.34,0.04)),
    Vector((-1.27,0.31,1.35)),
    P1 + Vector((0,0.29,0.02)),
    Vector((-0.15,0.25,1.93)),
    P2 + Vector((0,0.23,0.01)),
    Vector((0.72,0.20,1.51)),
    P3 + Vector((0,0.18,0.02)),
]
curve_tube("Integrated Electrical Harness", cable_points, 0.018, MAT_BLACK)

# Clamps keep the cable disciplined
for p in (
    Vector((-1.25,0.305,1.38)),
    Vector((-0.16,0.25,1.93)),
    Vector((0.70,0.20,1.52))
):
    sphere("Cable Guide", p, 0.034, MAT_DARK, (1.0,0.55,1.0))

# ------------------------------------------------------------
# Wrist roll and two-finger parallel gripper
# ------------------------------------------------------------

target = Vector((1.54, 0, 0.23))
reach_dir = Vector((0.56, 0, -0.72)).normalized()

# Inline rotary wrist (J5), conventional housing
roll_a = P3 + reach_dir*0.10
roll_b = P3 + reach_dir*0.34
cylinder_between("Wrist Roll J5 Main Housing", roll_a, roll_b,
                 0.145, MAT_DARK, 40)
cylinder_between("Wrist Roll Front Ring",
                 roll_b-reach_dir*0.045, roll_b+reach_dir*0.035,
                 0.158, MAT_SILVER, 40)
cylinder_between("Wrist Roll Accent Ring",
                 roll_b+reach_dir*0.028, roll_b+reach_dir*0.068,
                 0.132, MAT_ACCENT, 40)

palm_center = roll_b + reach_dir*0.13
palm_a = roll_b + reach_dir*0.055
palm_b = roll_b + reach_dir*0.245
box_between("Gripper Replaceable Interface", palm_a, palm_b,
            0.29, 0.40, MAT_SILVER, 0.035)

# compact lead-screw actuator on top
act_a = palm_center - Vector((0,0.15,0))
act_b = palm_center + Vector((0,0.15,0))
cylinder_between("Parallel Jaw Lead Screw", act_a, act_b,
                 0.035, MAT_BOLT, 24)
for sy in (-1,1):
    p = palm_center + Vector((0,sy*0.205,0))
    cylinder_between("Finger Pivot Pin",
                     p+Vector((0,-0.035,0)),
                     p+Vector((0,0.035,0)),
                     0.052, MAT_ACCENT, 24)

# Parallel fingers flank the sample in Y, with two solid links each
finger_root = palm_b + reach_dir*0.02
for sy in (-1,1):
    off = Vector((0, sy*0.19, 0))
    q0 = finger_root + off
    q1 = q0 + reach_dir*0.30
    # slight inward jaw segment
    inward = Vector((0, -sy*0.055, 0))
    q2 = q1 + reach_dir*0.25 + inward

    box_between("Machined Finger Proximal", q0, q1,
                0.10, 0.105, MAT_SILVER, 0.022)
    box_between("Machined Finger Distal", q1, q2,
                0.085, 0.095, MAT_DARK, 0.018)

    cylinder_between("Finger Knuckle Pin",
                     q1+Vector((0,-0.075,0)),
                     q1+Vector((0,0.075,0)),
                     0.034, MAT_ACCENT, 22)

    # replaceable soft contact pad on inward face
    pad_center = q2 + reach_dir*0.018 + Vector((0,-sy*0.047,0))
    pad = cube_obj("Replaceable Contact Pad", pad_center,
                   (0.085,0.035,0.115), MAT_BLACK, 0.012)
    pad.rotation_mode = 'QUATERNION'
    pad.rotation_quaternion = reach_dir.to_track_quat('Z','Y')

# ------------------------------------------------------------
# Small machined desk object
# ------------------------------------------------------------

bpy.ops.mesh.primitive_cylinder_add(
    vertices=6, radius=0.145, depth=0.19, location=(1.54,0,0.095)
)
sample = bpy.context.object
sample.name = "Small Machined Hexagonal Sample"
sample.data.materials.append(MAT_BRASS)
bevel(sample, 0.018, 3)

disk_z("Sample Top Insert", (1.54,0,0.199), 0.072, 0.020, MAT_DARK, 32)
disk_z("Sample Center Boss", (1.54,0,0.216), 0.034, 0.015, MAT_SILVER, 24)

# Small locating mat beneath sample
disk_z("Sample Locator", (1.54,0,0.008), 0.24, 0.016, MAT_BLACK, 48)
# locator was added after sample and overlaps only below desk top very slightly

# ------------------------------------------------------------
# Additional status detail
# ------------------------------------------------------------

# status light and clean data port on pedestal
sphere("Status LED", (base_x+0.215,-0.265,0.64), 0.025, MAT_RED,
       (1,0.45,1))
cube_obj("Service Port", (base_x+0.11,-0.318,0.51),
         (0.14,0.022,0.065), MAT_BLACK, 0.008)

# ------------------------------------------------------------
# Lighting
# ------------------------------------------------------------

def area_light(name, loc, energy, size, color, target):
    data = bpy.data.lights.new(name, type='AREA')
    data.energy = energy
    data.shape = 'DISK'
    data.size = size
    data.color = color
    obj = bpy.data.objects.new(name, data)
    bpy.context.collection.objects.link(obj)
    obj.location = loc
    direction = Vector(target) - Vector(loc)
    obj.rotation_euler = direction.to_track_quat('-Z','Y').to_euler()
    return obj

area_light("Large Product Key",
           (1.5,-3.6,5.0), 1050, 4.0, (0.88,0.94,1.0), (-0.3,0,1.0))
area_light("Warm Rim",
           (-3.4,1.7,3.4), 900, 3.0, (1.0,0.76,0.55), (-0.7,0,1.2))
area_light("Arm Detail Fill",
           (3.2,1.5,2.7), 780, 2.5, (0.58,0.78,1.0), (0.3,0,1.1))
area_light("Desk Softbox",
           (0,-0.2,5.6), 620, 3.2, (1.0,1.0,1.0), (0,0,0))

# ------------------------------------------------------------
# Camera
# ------------------------------------------------------------

camera_data = bpy.data.cameras.new("Presentation Camera")
camera = bpy.data.objects.new("Presentation Camera", camera_data)
bpy.context.collection.objects.link(camera)
scene.camera = camera

camera.location = (4.35, -6.45, 3.35)
look_at = Vector((-0.05, 0.02, 1.10))
camera.rotation_euler = (look_at - camera.location).to_track_quat('-Z','Y').to_euler()
camera_data.lens = 55
camera_data.sensor_width = 36
camera_data.dof.use_dof = True
camera_data.dof.focus_object = sample
camera_data.dof.aperture_fstop = 7.5

# ------------------------------------------------------------
# Render settings and save
# ------------------------------------------------------------

scene.render.resolution_x = 768
scene.render.resolution_y = 768
scene.render.resolution_percentage = 100

# Cycles-ready settings for benchmark override
scene.render.engine = 'BLENDER_EEVEE_NEXT'
scene.render.image_settings.file_format = 'PNG'
scene.render.filepath = "/tmp/robotic_arm.png"

# Set sensible object origins / selection state
bpy.ops.object.select_all(action='DESELECT')
camera.select_set(True)
bpy.context.view_layer.objects.active = camera

bpy.ops.wm.save_as_mainfile(filepath="/tmp/robotic_arm.blend")
```