1
$\begingroup$

I have created the code for each planet seperately as it works better that way but I don't seem to know how I can make it orbit together.

Everything seems to be working fine but the Moon doesn't orbit the Earth

Here is Earth's code:

#earth

import bpy
import math

running = True
earth_orbit_time = 150 # full orbit frames
earth_orbit_size = (15, 15) # elipse if not same

context = bpy.context
scene = context.scene
# cursor to zero
context.scene.cursor.location = (0.0, 0.0, 0.0)

# creation of earth
EARTH = bpy.ops.mesh.primitive_ico_sphere_add(location=(0.0, 0.0, 0.0))

planet = context.object
EARTH_ORBITAL_RADIUS = bpy.ops.curve.primitive_nurbs_circle_add(radius=2)
earth_orbit = context.object
earth_x, earth_y = earth_orbit_size
earth_orbit.scale.x, earth_orbit.scale.y = earth_x, earth_y
bpy.ops.object.transform_apply(scale=running)

if earth_x != earth_y: # get focus of eliptical path
    direction_of_x_y_axis = 0 if earth_x > earth_y else 1
    scene.cursor_location[direction_of_x_y_axis] = math.sqrt(abs(earth_x * earth_x - earth_y * earth_y))
    bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
    orbit.location = (0, 0, 0)

# follow path
follow_path = planet.constraints.new('FOLLOW_PATH')
follow_path.target = earth_orbit
follow_path.use_fixed_location = running
follow_path.use_curve_follow = running
# drive the offset
fcurve = follow_path.driver_add("offset_factor")
fcurve.driver.expression = "(frame / %d) %% 1" % earth_orbit_time

Here is the Moon's code

#earth's moon

import bpy
import math

running = True
moon_orbit_time = 20 # full orbit frames
moon_orbit_size = (15,15) # elipse if not same

context = bpy.context
scene = context.scene
# cursor to zero
context.scene.cursor.location = (20.0, 20.0, 0.0)

# creation of moon
MOON = bpy.ops.mesh.primitive_ico_sphere_add(location=(0.0, 0.0, 0.0))

moon = context.object
MOON_ORBITAL_RADIUS = bpy.ops.curve.primitive_nurbs_circle_add(radius=0.25)
moon_orbit = context.object
moon_x, moon_y = moon_orbit_size
moon_orbit.scale.x, moon_orbit.scale.y = moon_x, moon_y
bpy.ops.object.transform_apply(scale=running)

if moon_x != moon_y: # get focus of eliptical path
    direction_of_x_y_axis = 0 if moon_x > moon_y else 1
    scene.cursor_location[direction_of_x_y_axis] = math.sqrt(abs(moon_x * moon_x - moon_y * moon_y))
    bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
    orbit.location = (20.0, 20.0, 0)

# follow path
follow_path = moon.constraints.new('FOLLOW_PATH')
follow_path.target = moon_orbit
follow_path.use_fixed_location = running
follow_path.use_curve_follow = running
# drive the offset
fcurve = follow_path.driver_add("offset_factor")
fcurve.driver.expression = "(frame / %d) %% 1" % moon_orbit_time

$\endgroup$
2
  • 1
    $\begingroup$ The idea here blender.stackexchange.com/a/91511/15543 was to match two planets barycentric orbits, then collection instance them for the next time. eg make the earth moon system, make a collection instance then orbit that around the sun. The simple solution for what you have would be parent the moon orbit to the earth. May also be of interest. blender.stackexchange.com/a/132928/15543 $\endgroup$
    – batFINGER
    Jan 25 '20 at 13:00
  • 1
    $\begingroup$ Not sure I agree with "I have created the code for each planet seperately as it works better that way" tho $\endgroup$
    – batFINGER
    Jan 25 '20 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.