Created
July 5, 2022 10:55
-
-
Save aobond2/ead432a53c76eece11f764183d7b1469 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import pi | |
from random import random | |
import bpy | |
def delete_object(name): | |
if name in bpy.data.objects: | |
obj = bpy.data.objects[name] | |
obj.select_set(True) | |
bpy.ops.object.delete(use_global=False) | |
def create_emission_shader(color, strength, mat_name): | |
mat = bpy.data.materials.new(mat_name) | |
mat.use_nodes = True | |
#clear all existing nodes | |
nodes = mat.node_tree.nodes | |
nodes.clear() | |
#add emission nodes | |
node_emission = nodes.new(type="ShaderNodeEmission") | |
node_emission.inputs[0].default_value = color | |
node_emission.inputs[1].default_value = strength | |
node_output = nodes.new(type="ShaderNodeOutputMaterial") | |
links = mat.node_tree.links | |
link = links.new(node_emission.outputs[0], node_output.inputs[0]) | |
return mat | |
def create_sphere(radius, distance_to_sun, obj_name): | |
obj = bpy.ops.mesh.primitive_uv_sphere_add ( | |
radius = radius, | |
location=(distance_to_sun,0,0), | |
scale=(1, 1, 1) | |
) | |
bpy.context.object.name = obj_name | |
bpy.ops.object.shade_smooth() | |
return bpy.context.object | |
def create_torus(radius, obj_name): | |
obj = bpy.ops.mesh.primitive_torus_add( | |
location=(0,0,0), | |
major_radius=radius, | |
minor_radius = 0.1, | |
major_segments=60 | |
) | |
bpy.context.object.name = obj_name | |
bpy.ops.object.shade_smooth() | |
return bpy.context.object | |
def find_3dview_space(): | |
area = None | |
for a in bpy.data.window_managers[0].windows[0].screen.areas: | |
if a.type == "VIEW_3D": | |
area = a | |
break | |
return area.spaces[0] if area else bpy.context.space_data | |
def setup_scene(): | |
#set black background | |
bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[0].default_value = (0, 0, 0,1) | |
#set render engine to EEVEE | |
scene = bpy.context.scene | |
scene.render.engine = "BLENDER_EEVEE" | |
scene.eevee.use_bloom = True | |
#set default start end frame for animation | |
scene.frame_start = START_FRAME | |
scene.frame_end = END_FRAME | |
scene.frame_current = START_FRAME | |
#get current 3D view | |
space = find_3dview_space() | |
#setup viewport | |
space.shading.type = 'RENDERED' | |
space.overlay.show_floor = False | |
space.overlay.show_axis_y = False | |
space.overlay.show_axis_x = False | |
space.overlay.show_cursor = False | |
space.overlay.show_object_origins = False | |
N_PLANETS = 6 | |
START_FRAME = 1 | |
END_FRAME = 200 | |
#call scene setup | |
setup_scene() | |
delete_object("Sun") | |
for n in range (N_PLANETS): | |
delete_object("Planet-{:02d}".format(n)) | |
delete_object("Radius-{:02d}".format(n)) | |
for m in bpy.data.materials: | |
bpy.data.materials.remove(m) | |
ring_mat = create_emission_shader((1, 1, 1, 1), 1, "RingMat") | |
for n in range(N_PLANETS): | |
r = 1 + random() * 4 | |
d = 30 + n * 12 + (random() * 4 - 2) | |
planet = create_sphere(r, d, "Planet-{:02d}".format(n)) | |
planet.data.materials.append(create_emission_shader((random(), random(), 1, 1), 2, "PlanetMat-{:02d}".format(n))) | |
ring = create_torus(d, "Radius-{:02d}".format(n)) | |
ring.data.materials.append(ring_mat) | |
#rotate planet | |
bpy.context.view_layer.objects.active = planet | |
planet.select_set(True) | |
bpy.ops.object.origin_set(type="ORIGIN_CURSOR", center="MEDIAN") | |
#animation code | |
planet.animation_data_create() | |
planet.animation_data.action = bpy.data.actions.new(name="RotationAction") | |
fcurve = planet.animation_data.action.fcurves.new( | |
data_path="rotation_euler", index=2 | |
) | |
k1 = fcurve.keyframe_points.insert( | |
frame=START_FRAME, | |
value = 0 | |
) | |
k1.interpolation = "LINEAR" | |
k2 = fcurve.keyframe_points.insert( | |
frame=END_FRAME, | |
value = (2 + random() * 2) * pi | |
) | |
k2.interpolation = "LINEAR" | |
sun = create_sphere(12, 0, "Sun") | |
sun.data.materials.append( | |
create_emission_shader((1, 0.66, 0.08, 1), 10, "SunMat") | |
) | |
bpy.ops.object.select_all(action='DESELECT') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment