www.xbdev.net
xbdev - software development
Thursday April 30, 2026
Home | Contact | Support | Blender (.py) Scripts... Automating Blender ..
     
 

Blender (.py) Scripts...

Automating Blender ..

 

Starfield


Generating a starfield isn't as hard as you might think - you can mix in a bit of randomness and map it onto the background world color to create a nice looking starfield.


Generate a background that looks like stars (space).
Generate a background that looks like stars (space).


<?php
import bpy

# Clear the scene
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# Ensure world uses nodes
world = bpy.data.worlds["World"]
world.use_nodes = True
nodes = world.node_tree.nodes
links = world.node_tree.links
nodes.clear()

# Create nodes
tex_coord = nodes.new("ShaderNodeTexCoord")
mapping = nodes.new("ShaderNodeMapping")
voronoi = nodes.new("ShaderNodeTexVoronoi")
color_ramp = nodes.new("ShaderNodeValToRGB")
background = nodes.new("ShaderNodeBackground")
output = nodes.new("ShaderNodeOutputWorld")

# Set locations for clarity
tex_coord.location = (-1000, 0)
mapping.location = (-800, 0)
voronoi.location = (-600, 0)
color_ramp.location = (-400, 0)
background.location = (-200, 0)
output.location = (0, 0)

# Configure Voronoi Texture
voronoi.feature = 'F1'
voronoi.distance = 'EUCLIDEAN'
voronoi.inputs['Scale'].default_value = 50.0

# Configure ColorRamp for stars
color_ramp.color_ramp.interpolation = 'LINEAR'
el_black = color_ramp.color_ramp.elements[0]
el_white = color_ramp.color_ramp.elements[1]
el_black.position = 0.05
el_black.color = (1, 1, 1, 1)  # stars
el_white.position = el_black.position - 0.03
el_white.color = (0,0,0,0)  # space

# Link nodes together
links.new(tex_coord.outputs['Generated'], mapping.inputs['Vector'])
links.new(mapping.outputs['Vector'], voronoi.inputs['Vector'])
links.new(voronoi.outputs['Distance'], color_ramp.inputs['Fac'])
links.new(color_ramp.outputs['Color'], background.inputs['Color'])
links.new(background.outputs['Background'], output.inputs['Surface'])

# Add a camera
bpy.ops.object.camera_add(location=(0, -5, 2), rotation=(1.2, 0, 0))
camera = bpy.context.object
bpy.context.scene.camera = camera

# Add a light
bpy.ops.object.light_add(type='AREA', location=(4, -4, 6))
light = bpy.context.object
light.data.energy = 1000
light.rotation_euler = (1, 0, 0.8)

# Add a camera
bpy.ops.object.camera_add(location=(0, -5, 2), rotation=(1.2, 0, 0))
camera = bpy.context.object
bpy.context.scene.camera = camera

# Add a light
bpy.ops.object.light_add(type='AREA', location=(4, -4, 6))
light = bpy.context.object
light.data.energy = 1000
light.rotation_euler = (1, 0, 0.8)


Tweaking the default 'scale' lets you modify the number of stars in the sky.







 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2026 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.