Physics and Python script?

(DreamMaster) #1

How do you put physics and python together? I took physics class last year in soph. year (10th grade). I think I understood well enough to put some physics on my balls that I’m working on for compositing animation contest hosted by WeirdHat. The problem is, I do not know where to start and how to put the math formula in, telling the computer to follow it. You know what I mean? sighs Anyone? Perhaps you, theeth, could help me?

Anyways, thank you in advance… hopefully. :slight_smile:


(theeth) #2

Here’s some of the basics:

First of, you have to know that the script you are going to do will have to be called every frame changed, by the use of a script link (under the Script Buttons, next to the Display buttons, Press NEW and type the name of the script in the field).

Then, in that script, you can use the Blender api to manipulate any objects. Here’s some sort of example:

import Blender # import the Blender module
from math import sin, cos

RotSpeed = 0.01 # Rotation speed, in radian per frame

Speed = 0.01 # radius change, in Blender units per frame

Object = Blender.Object.GetSelected()[0]  # Get the first selected object, the active object

Frame = Blender.Get("curtime") # get the animation time, including motion blur offset

Object.Loc = [Speed * Frame * cos(Frame * RotSpeed, Speed * Frame * sin(Frame * RotSpeed), 0.0] # Place the object according to some parameters

That should help you a bit.


(Schlops) #3

But there’s one thing to change: Replace Get(Const.BP_CURTIME) with Get(‘curtime’) (Analog for BP_CURFRAME if there is anything with it)

(aayers) #4

Hi DreamMaster7,

Your project sounds exciting. I did something similar in 12th grade.

My advice before beginning getting your claws into Blender is to write your physics program in Python script and output some data to a text file. Verify your data before attempting to animate it! There are lots of neat data plotting programs available. My preference is GNUPLOT which you can download from There’s also a Python pluggin (it’s a pipe actually) which you can download from and use it to plot data while your physics program is running. :slight_smile:

If your program is developed separately, it’ll be easier later on to implement your script into Blender and hopefully the physics will be correct.



(DreamMaster) #5

Aye, Sir! I’ll complete it in 1 nanosecond! ::walks to her office and type rapidly::


Oops ::her computer explodes again:: Sir, I think I went too fast…


Just kidding about what I just mentioned. :slight_smile: Anyways, thank you for your tips! I’m sure it’s very useful… gulps but don’t get your hope up or anything! Don’t HOLD your breath! I might just be ended up as a loser like other python novice blenderhead!

:smiley: But I’ll try, at least I try.

(DreamMaster) #6

I forgot to ask you something… how do you make python to detect the collusion of a ball to three other balls that cause them to go off. You know what I am talking about do ya?

If you have seen my animation then you’ll understand better what I’m trying to say. Thanks again! :slight_smile:

(theeth) #7

If you know the radius of the ball, and there position (with Object.Loc), it’s simple geometry to find if the balls are intersecting.


(aayers) #8

How much do you know about vectors? I know I didn’t cover this stuff in grade 10, but the Pythagorean theorem is really handy…

Suppose you have two points at these coordinates on a 2D plane
P1 = (x,y)
P2 = (a,b)

You can find the distance between P1 and P2 with Pythagoras, right? If you don’t know what it is, it’s easy to look up.

Since you are a student, I will let you figure it out on your own. Figure out the distance for the 2D case first, then extend your knowledge to the 3D case, and then … implement your formula into your script.

I hope this will get you going.


(DreamMaster) #9

nods I’m listening alright… why make me do all hard work since I’m a student? :frowning: J/K 8) Ok thank you for your help.

(LethalSideP) #10

Hurrah, I’m not the only one on this forum who suffers from python trouble! CHEERS

If you’ve got spheres, the best help i can give you is the formula for any point on a 3D sphere:

(x-a)**2 + (y-b)**2 + (z-c)2=r2

where a is the x coordinate of the sphere, y is the y coordinate of the sphere, c is the z coordinate of the sphere, and r is the radius of the sphere. And **2 is squared, in case you’re not sure.

Using this, provided you know which spheres you want to run collision detection on, you can calculate their coordinates, and check to see if any of the spheres intersect. If you’re worried about having to run that calculation for every sphere you want to model, just put an if clause at the beginning of your math there checking to see if the spheres are within, oh, say 10 units of each other. If they are, check. If not, don’t bother.

Hope that helped a little.(/me fights off the memories of the maths exam he’s just failed for sure)

LethalSideParting/Karl/LSP (from Monday after this 11 letter change)