Skip to content

Instantly share code, notes, and snippets.

@sambler
Created November 21, 2016 17:23
Show Gist options
  • Save sambler/38a8204185d979a6a53ff1d0c10d0a27 to your computer and use it in GitHub Desktop.
Save sambler/38a8204185d979a6a53ff1d0c10d0a27 to your computer and use it in GitHub Desktop.
Create a mesh of satellites spinning around
# made in answer to http://blender.stackexchange.com/q/67612/935
# create a mesh of satellites based on http://space.stackexchange.com/q/19055
import bpy, bpy_extras, bmesh
import math, mathutils
if False: # change to True for final test - needs about 6GB free ram
# full data
# num_planes, num_sats, radius, frames
# frames is number of frames that the orbital plane takes to rotate 360
sat_set = [
[32,50,1150, 200],
[32,50,1110, 200],
[ 8,50,1130, 200],
[ 5,75,1275, 200],
[ 6,75,1325, 200],
]
else:
# small test set - needs about 1GB
sat_set = [
[16,8,1150, 200],
[16,8,1110, 200],
[ 8,8,1130, 200],
[ 5,8,1275, 200],
[ 6,8,1325, 200],
]
set_scale = 0.01
armature_name = 'Controller'
orbital_name = 'OrbitalPlane'
c = bpy.context
if c.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
ad = bpy.data.armatures.new(armature_name)
cntrl = bpy.data.objects.new(armature_name, ad)
c.scene.objects.link(cntrl)
def duplicateonverts(par):
bm = bmesh.new()
bm.from_mesh(par.data)
src = [v for v in bm.verts]
for v in src:
loc = mathutils.Matrix.Translation(v.co)
rot = v.normal.to_track_quat('-Y','Z')
mat = mathutils.Matrix.Identity(3)
mat.rotate(rot)
mat = loc * mat.to_4x4() * mat.Scale(set_scale*20,4)
bmesh.ops.create_monkey(bm, matrix=mat)
bm.to_mesh(par.data)
bm.free()
for di,d in enumerate(sat_set): # each deployment
for oi,o in enumerate(range(d[0])): # orbital planes
n = 'op_'+str(di)+'_'+str(oi)
rot = (math.radians(90),0,math.radians((360/d[0])*oi))
bpy.ops.mesh.primitive_circle_add(vertices=d[1],
radius=d[2]*set_scale,
rotation=rot )
duplicateonverts(c.active_object)
c.active_object.name = n
c.active_object.vertex_groups.new(n)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.object.vertex_group_assign()
bpy.ops.object.mode_set(mode='OBJECT')
cntrl.select = True
c.scene.objects.active = cntrl
bpy.ops.object.mode_set(mode='EDIT')
b = cntrl.data.edit_bones.new(n)
b.head = (0.0, 0.0, 0.0)
b.tail = (0.0, 0.0, 1.0)
b.roll = rot[2]
bn = b.name
bpy.ops.object.mode_set(mode='POSE')
b = cntrl.pose.bones[bn]
b.rotation_mode = 'XYZ'
b.rotation_euler.z = 0.0
b.keyframe_insert('rotation_euler', index=2, frame=0)
cntrl.animation_data.action.fcurves[-1].keyframe_points[-1].interpolation = 'LINEAR'
b.rotation_euler.z = math.radians(360)
b.keyframe_insert('rotation_euler', index=2, frame=d[3])
cntrl.animation_data.action.fcurves[-1].keyframe_points[-1].interpolation = 'LINEAR'
cntrl.animation_data.action.fcurves[-1].extrapolation = 'LINEAR'
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_pattern(pattern='op_*')
c.scene.objects.active = c.selected_objects[0]
bpy.ops.object.join()
c.active_object.name = orbital_name
m = c.active_object.modifiers.new('Armature', type='ARMATURE')
m.object = cntrl
m.use_vertex_groups = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment