import bpy, math
from mathutils import Vector

# Start from a clean scene and create exactly one mesh object: a 2 m cube at the origin.
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
for datablocks in (bpy.data.meshes, bpy.data.curves, bpy.data.materials, bpy.data.cameras, bpy.data.lights):
    pass

scene = bpy.context.scene
scene.unit_settings.system = 'METRIC'
scene.unit_settings.scale_length = 1.0
scene.unit_settings.length_unit = 'METERS'

bpy.ops.mesh.primitive_cube_add(size=2.0, enter_editmode=False, align='WORLD', location=(0.0, 0.0, 0.0))
cube = bpy.context.active_object
cube.name = 'Cube'
cube.data.name = 'Cube'
cube.location = (0.0, 0.0, 0.0)
cube.rotation_euler = (0.0, 0.0, 0.0)
cube.scale = (1.0, 1.0, 1.0)
# Explicitly enforce the requested world dimensions.
cube.dimensions = (2.0, 2.0, 2.0)
bpy.context.view_layer.objects.active = cube
cube.select_set(True)

# A clean, slightly satin blue material keeps the form legible in both the render and GLB.
mat = bpy.data.materials.new('Cube Blue')
mat.diffuse_color = (0.055, 0.24, 0.62, 1.0)
mat.use_nodes = True
bsdf = mat.node_tree.nodes.get('Principled BSDF')
if bsdf:
    bsdf.inputs['Base Color'].default_value = (0.055, 0.24, 0.62, 1.0)
    bsdf.inputs['Roughness'].default_value = 0.28
    bsdf.inputs['Metallic'].default_value = 0.05
cube.data.materials.append(mat)

# Presentation camera (does not add any extra mesh geometry).
def point_at(obj, target=(0.0, 0.0, 0.0)):
    obj.rotation_euler = (Vector(target) - obj.location).to_track_quat('-Z', 'Y').to_euler()

cam_data = bpy.data.cameras.new('Presentation Camera')
cam = bpy.data.objects.new('Presentation Camera', cam_data)
scene.collection.objects.link(cam)
cam.location = (4.8, -5.4, 3.6)
cam_data.lens = 58
cam_data.sensor_width = 36
point_at(cam)
scene.camera = cam

# Soft studio lighting against a dark neutral world; no floor or other mesh is added.
world = scene.world or bpy.data.worlds.new('World')
scene.world = world
world.use_nodes = True
world.node_tree.nodes['Background'].inputs['Color'].default_value = (0.008, 0.012, 0.025, 1.0)
world.node_tree.nodes['Background'].inputs['Strength'].default_value = 0.18

def add_area(name, location, energy, color, size):
    data = bpy.data.lights.new(name, type='AREA')
    data.energy = energy
    data.color = color
    data.shape = 'DISK'
    data.size = size
    obj = bpy.data.objects.new(name, data)
    scene.collection.objects.link(obj)
    obj.location = location
    point_at(obj)
    return obj

add_area('Key Light', (4.5, -4.0, 5.5), 700.0, (1.0, 0.87, 0.72), 3.5)
add_area('Fill Light', (-4.0, -2.5, 2.5), 420.0, (0.55, 0.70, 1.0), 3.0)
add_area('Rim Light', (1.0, 4.5, 5.0), 900.0, (0.62, 0.75, 1.0), 3.0)

# Render settings are set to Cycles for a native-fidelity preview; the benchmark may override resolution/samples.
scene.render.engine = 'BLENDER_EEVEE_NEXT' if 'BLENDER_EEVEE_NEXT' in {i.identifier for i in bpy.types.RenderSettings.bl_rna.properties['engine'].enum_items} else scene.render.engine
# Prefer Cycles when available for validation.
try:
    scene.render.engine = 'CYCLES'
    scene.cycles.samples = 32
    scene.cycles.use_denoising = True
except Exception:
    pass
scene.render.resolution_x = 512
scene.render.resolution_y = 512
scene.render.resolution_percentage = 100
scene.render.image_settings.file_format = 'PNG'
scene.render.film_transparent = False
scene.render.filepath = '/tmp/cube_scene_preview.png'
scene.render.image_settings.color_mode = 'RGBA'
scene.view_settings.look = 'AgX - Medium High Contrast'

# Helpful metadata for inspection.
cube['object_purpose'] = 'Single 2 meter cube centered at world origin'
cube['dimensions_meters'] = '2 x 2 x 2'

# Select only the cube for a clear interactive starting state.
bpy.ops.object.select_all(action='DESELECT')
cube.select_set(True)
bpy.context.view_layer.objects.active = cube

# Save the complete scene.
bpy.ops.wm.save_as_mainfile(filepath='/tmp/cube_scene.blend')

result = {
    'file': bpy.data.filepath,
    'objects': [(o.name, o.type, tuple(round(v,6) for v in o.location), tuple(round(v,6) for v in o.dimensions)) for o in scene.objects],
    'cube_scale': tuple(cube.scale),
    'cube_dimensions': tuple(cube.dimensions),
    'cube_location': tuple(cube.location),
    'engine': scene.render.engine,
}


Traceback (most recent call last):
  File "<repo>\.runner\blender-mcp-v1.0.0\addon\mcp_to_blender_server.py", line 230, in _execute_code
    exec(code, namespace)
    ~~~~^^^^^^^^^^^^^^^^^
  File "<string>", line 102, in <module>
RuntimeError: Error: Cannot open file <repo>\apps\console\tmp\cube_scene.blend@ for writing: No such file or directory