1
$\begingroup$

I am learning to work with 3D objects using Python and Blender. I want to simulate gravitational two body problem for Sun and earth and I know it is possible in Python but I want to make it attractive using blender using materials and texture. Is it possible to run all the mathematical calculations in Python and inject in blender to make it in an attractive way?

$\endgroup$
3
$\begingroup$

I won't pretend to know all required physics and the good initial conditions, but that can be done using the Python script.

The idea is to setup a function (a handler) which is fired by Blender each time frame (current time) is changing. This is done by "bpy.app.handlers.frame_change_pre.append( theFunction )" which mean "theFunction" is called before the change of the frame.

The result is the following:

enter image description here

The code:

import bpy
from mathutils import Vector

#get first object and arbitrary location
o1 = bpy.data.objects['o1']
o1.location = Vector( (-5,0,0 ) )

#same for second one
o2 = bpy.data.objects['o2']
o2.location = Vector( (5,0,0 ) )

#define constants: G and masses
G = 6.6740831e-11
m1 = 1e+11
m2 = 1e+05

#initial speeds
v1 = Vector( ( 0, 0.01, 0 ) )
v2 = Vector( ( 0, -0.5, 0 ) )

#frame rate for calculation
frame_rate = 1 #24 (normally 24 but 1 is faster)
seconds_per_frame = 1 / frame_rate

def two_bodies(scene):
    #import globals in the function scope
    global o1, o2, G, m1, m2, v1, v2
    global seconds_per_frame
    
    #calc current direction between the objects
    direction = o1.location - o2.location
    
    #calc the squared distance 
    d_squared = direction.length_squared
    #keep the direction of the strength
    direction.normalize()
    
    #calc new speed vectors
    v1 = v1 - (direction * (G * m2 / d_squared) * seconds_per_frame)
    v2 = v2 + (direction * (G * m1 / d_squared) * seconds_per_frame)
    
    #calc new locations
    o1.location += v1 * seconds_per_frame
    o2.location += v2 * seconds_per_frame

#get rid of previously set frame_change_pre handlers (if not the same handler may be fired n times)
bpy.app.handlers.frame_change_pre.clear()
#install "two_bodies" as current handler
bpy.app.handlers.frame_change_pre.append(two_bodies)

The blend file is below. To use it:

  • In the text part, use the "run script" button in order to setup initial conditions and the handler itself
  • In the 3D view part, use AltA to run the animation

| improve this answer | |
$\endgroup$
  • $\begingroup$ I really appreciate your help. Thank you. Btw, are there any manuals on the code writing part?I am not quite familiar with the Python programming. I tried searching for youtube videos and there are only few videos which is about animation with python and blender. Besides, I tried doing one watching the video and it didn't work. Need to learn from the basics. $\endgroup$ – Rima Apr 4 '18 at 16:47
  • $\begingroup$ Surely you've seen this docs.blender.org/api/current. About Python in general, I can only tell about my little experience. I knew other languages before, but the main point was to understand what is often called as "pythonic", so lists, lists comprehension, dictionaries, set, but I'm still a beginner or occasional user of Python... so I'm not a reference, sorry $\endgroup$ – lemon Apr 4 '18 at 16:57
  • $\begingroup$ I am not sure why it doesn't run in mine. The error is 'Python script fail,look in console for now.' The error is in line 8 for first object. In console,it shows built modules and I think bpy.data.objects[] is not built in module. I cannot think of any other reason. $\endgroup$ – Rima Apr 5 '18 at 5:47
  • $\begingroup$ Have you previously created the 2 spheres with the right names? In my file they are called o1 and o2. $\endgroup$ – lemon Apr 5 '18 at 5:52
  • $\begingroup$ I don't understand what you mean by right names. Sorry i am very new to blender. $\endgroup$ – Rima Apr 5 '18 at 5:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.