www.xbdev.net
xbdev - software development
Thursday June 26, 2025
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).


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 = (-10000)
mapping.location = (-8000)
voronoi.location = (-6000)
color_ramp.location = (-4000)
background.location = (-2000)
output.location = (00)

# 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 = (1111)  # 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, -52), rotation=(1.200))
camera bpy.context.object
bpy
.context.scene.camera camera

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

# Add a camera
bpy.ops.object.camera_add(location=(0, -52), rotation=(1.200))
camera bpy.context.object
bpy
.context.scene.camera camera

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


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







 
Advert (Support Website)

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