# -*- coding: utf-8 -*-
import bpy, math, sys
# Declare constants
#TEXT = 'Hello World\nWelcome in Blender'
TEXT = ' ' . join(sys.argv[4:])
CYCLES_SAMPLES = 50
TEXT_ALIGN = 'CENTER'
FORMAT_FILE = 'PNG'
FORMAT_MODE = 'RGBA' # FORMAT_MODE = 'RGB'
FONT_SIZE = 1.0
FONT_DEPTH = 0.1
FONT_NAME = '/home/mike/Desktop/stuff/fonts/ubuntu/Ubuntu-Bold.ttf'
SAVE_AS = '/home/mike/Desktop/output.png'
POINT_LIGHT_LOCATION = (0, 0, 1)
POINT_LIGHT_ENERGY = 100
POINT_LIGHT_COLOR = (1, 1, 1)
CAMERA_Z_OFFSET = 1.2
# Deselect all objects
bpy.ops.object.select_all(action='DESELECT')
# Select all objects in the scene
bpy.ops.object.select_all(action='SELECT')
# Delete all selected objects
bpy.ops.object.delete()
# Set up the scene
bpy.context.scene.unit_settings.system = 'METRIC'
bpy.context.scene.unit_settings.scale_length = 0.1
# Calculate the scene dimensions
scene_width = bpy.context.scene.render.resolution_x * bpy.context.scene.render.pixel_aspect_x / bpy.context.scene.render.resolution_y * bpy.context.scene.render.pixel_aspect_y
scene_height = 1.0
# Set the font parameters
text_content = TEXT
font_size = FONT_SIZE
font_depth = FONT_DEPTH
font_location = (0, 0, 0)
# Create the text object
bpy.ops.object.text_add(location=font_location)
text_obj = bpy.context.object
text_obj.data.body = TEXT
text_obj.data.size = FONT_SIZE
text_obj.data.extrude = FONT_DEPTH
text_obj.data.align_x = TEXT_ALIGN
text_obj.data.align_y = TEXT_ALIGN
font = bpy.data.fonts.load(FONT_NAME)
text_obj.data.font = font
bpy.context.view_layer.update()
# Calculate text bounding box
text_obj.update_from_editmode()
text_obj.select_set(True)
bpy.context.view_layer.objects.active = text_obj
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='BOUNDS')
text_bbox = text_obj.bound_box
text_width = max(text_bbox[i][0] for i in range(8)) - min(text_bbox[i][0] for i in range(8))
text_height = max(text_bbox[i][1] for i in range(8)) - min(text_bbox[i][1] for i in range(8))
# Set the 3D cursor location as the text position
text_obj.location = bpy.context.scene.cursor.location
# Set up the materials
material_text = bpy.data.materials.new(name="Text")
material_text.diffuse_color = (1.0, 1.0, 1.0, 1.0)
text_obj.data.materials.append(material_text)
# Set up the camera
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()
bpy.ops.object.camera_add(location=(0, 0, 0))
camera_obj = bpy.context.object
# Set camera parameters
camera_obj.location = (bpy.context.scene.cursor.location.x, bpy.context.scene.cursor.location.y, text_width / (math.tan(camera_obj.data.angle / 1.2)))
camera_obj.location.z += CAMERA_Z_OFFSET
camera_obj.rotation_euler = (0, 0, 0)
# Set camera as active camera for the scene
bpy.context.scene.camera = camera_obj
# Create a new point light object
bpy.ops.object.light_add(type='POINT', location = POINT_LIGHT_LOCATION)
# Set the energy/intensity of the light
light = bpy.context.active_object
light.data.energy = POINT_LIGHT_ENERGY
light.data.color = POINT_LIGHT_COLOR
# Set up rendering settings
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.samples = CYCLES_SAMPLES
# Set up output path
bpy.context.scene.render.image_settings.file_format = FORMAT_FILE
bpy.context.scene.render.image_settings.color_mode = FORMAT_MODE
bpy.context.scene.render.filepath = SAVE_AS
# Render the image
bpy.ops.render.render(write_still = True)
print("Finished!:)")