<?php
import bpy
from mathutils import Vector
# Clear existing objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
# Create text object
bpy.ops.object.text_add(align='WORLD', location=(0, -0.2, 0))
text_obj = bpy.context.object
text_obj.name = "BALLS_Emitter"
text_obj.data.body = "BALLS"
text_obj.data.align_x = 'CENTER'
text_obj.data.extrude = 0.1
text_obj.data.bevel_depth = 0.01
#text_obj.data.bevel_resolution = 2
# Convert text to mesh
bpy.ops.object.convert(target='MESH')
# Create instance object (sphere)
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.1, location=(10, 0, 0))
sphere = bpy.context.object
sphere.name = "BALL_Instance"
sphere.hide_render = True # Hide in renders (only particles will show)
# Hide the emitter text
#text_obj.hide_set(True)
#text_obj.hide_render = True
# Add particle system
particle_system = text_obj.modifiers.new(name="ParticleSystem", type='PARTICLE_SYSTEM')
particles = particle_system.particle_system.settings
# Configure particle settings
particles.count = 2500
particles.frame_start = 1
particles.frame_end = 1
particles.lifetime = 10
particles.emit_from = 'FACE'
particles.physics_type = 'NO'
particles.normal_factor = 0
particles.factor_random = 3
particles.particle_size = 0.45
particles.size_random = 0.3
particles.distribution = 'RAND' # THIS SETS RANDOM DISTRIBUTION
# Set particles to use sphere instance
particles.render_type = 'OBJECT'
particles.instance_object = sphere
particles.use_rotation_instance = True
particles.use_scale_instance = True
# Create material for spheres
mat = bpy.data.materials.new(name="BallMaterial")
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
nodes.clear()
# Create glossy shader with random color per particle
bsdf = nodes.new('ShaderNodeBsdfPrincipled')
bsdf.inputs['Base Color'].default_value = (0.8, 0.2, 0.1, 1)
bsdf.inputs['Metallic'].default_value = 0.5
bsdf.inputs['Roughness'].default_value = 0.3
# Add attribute node for random color
attribute = nodes.new('ShaderNodeAttribute')
attribute.attribute_name = 'random'
# Add hue/saturation node
hue_sat = nodes.new('ShaderNodeHueSaturation')
hue_sat.inputs['Hue'].default_value = 0.5
hue_sat.inputs['Saturation'].default_value = 1.0
hue_sat.inputs['Value'].default_value = 1.0
# Connect nodes
links.new(attribute.outputs['Color'], hue_sat.inputs['Color'])
links.new(hue_sat.outputs['Color'], bsdf.inputs['Base Color'])
output = nodes.new('ShaderNodeOutputMaterial')
links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
# Assign material to sphere
if sphere.data.materials:
sphere.data.materials[0] = mat
else:
sphere.data.materials.append(mat)
# Set up lighting
bpy.ops.object.light_add(type='SUN', location=(5, 5, 10))
sun = bpy.context.object
sun.data.energy = 3.0
bpy.ops.object.light_add(type='AREA', location=(0, -5, 5))
area = bpy.context.object
area.data.energy = 500
area.data.size = 2.0
# Set up camera
bpy.ops.object.camera_add(location=(0, 0, 4))
camera = bpy.context.object
camera.rotation_euler = (0, 0, 0)
bpy.context.scene.camera = camera
bpy.context.scene.frame_set(1)