i have managed to get ODE working. ( this is used for hinge and collision detection ). here is some code i have cobbled together from the python ( pyODE ) examples and the ode docs.
the cube does not seem to bouce correctly ( help ? ) but otherwise seems good.
the source is as follows ( more info below the code ):
import Blender
from Blender import Scene
import ode
import math
from math import *
scene=Scene.GetCurrent()
scene.currentFrame(1)
sphere=Blender.Object.Get('Sphere')
cube=Blender.Object.Get('Cube')
world=ode.World()
world.setGravity( (0,-9.81,0) )
world.setERP(0.8)
world.setCFM(1E-5)
space=ode.Space()
floor=ode.GeomPlane(space,(0,1,0),0)
contactgroup=ode.JointGroup()
sbody=ode.Body(world)
M=ode.Mass()
M.setSphere(2500.0,0.05)
M.mass=1.0
sbody.setMass(M)
geom=ode.GeomSphere(space,0.05)
geom.setBody(sbody)
sbody.setPosition( (sphere.LocX,sphere.LocZ,sphere.LocY) )
sbody.addForce( (0,0,0) )
cbody=ode.Body(world)
cM=ode.Mass()
cM.setBox(2500.0,0.1,0.1,0.1)
cM.mass=1.0
cbody.setMass(cM)
cgeom=ode.GeomBox(space,lengths=(0.1,0.1,0.1))
cgeom.setBody(cbody)
cbody.setPosition( (cube.LocX,cube.LocZ,cube.LocY) )
cbody.addForce( (0,0,0) )
total_time= 0.0
fps=25
dt=1.0/fps
endtime=float(scene.endFrame()/fps)
print "----------------------------"
print endtime
def near_callback(args,geom1,geom2):
contacts=ode.collide(geom1,geom2)
world,contact_group=args
for c in contacts:
c.setBounce(0.8)
c.setMu(2000)
j=ode.ContactJoint(world,contactgroup,c)
j.attach(geom1.getBody(),geom2.getBody())
def transformBlender(obj,body):
x,y,z=body.getPosition()
u,v,w=body.getLinearVel()
t=body.getRotation()
rot=[t[0],t[1],t[2],0,t[3],t[4],t[5],0,t[6],t[7],t[8],0,0,0,1]
angle_y=D=asin(math.asin(rot[2]))
C=math.cos(angle_y)
angle_y=math.radians(angle_y)
if fabs(C) > 0.005:
tx=rot[10]/C
ty=-rot[6]/C
angle_x=math.radians(math.atan2(ty,tx))
tx=rot[0]/C
ty=-rot[1]/C
angle_z=math.radians(atan2(ty,tx))
else:
angle_x=0
tx=rot[5]
ty=rot[4]
angle_z=math.radians(atan2(ty,tx))
if angle_x<0: angle_x=angle_x+360
if angle_y<0: angle_y=angle_y+360
if angle_z<0: angle_z=angle_z+360
obj.rot=(angle_x,-angle_z,angle_x)
obj.setLocation(x,z,y)
print "obj %6.3f,%6.3f,%6.3f %6.3f,%6.3f,%6.3f" % (x,z,y,angle_x,angle_z,angle_y)
while total_time<endtime:
print "Blender frame= %1.2f" % (total_time/dt)
scene.currentFrame(int(total_time/dt))
space.collide((world,contactgroup),near_callback)
transformBlender(sphere,sbody)
transformBlender(cube,cbody)
Blender.Redraw()
world.step(dt)
contactgroup.empty()
total_time+=dt
you must create two objects in blender: a sphere called sphere and a cube called cube when you run the script the cube and sphere will fall and bounce from theyre current positions. ( so move them high before each run )
issues:
how can i store the animation to blender? searching for ode in this forum shows that some other people have attempted this but none seem to shout success except for a mac script ( which i havent found yet ). this makes me ask why from python we dont have accesss to the same functions the user has available from the menu ( i would love to be able to create primitives, store loc straight to key frame etc straight from python ).
has anyone used the hinge part of ode? is cloth not just a grid of hinges?
is there an easier way to work out the rotation from the rotation matricies?
questions not directly related to ode:
can anyone point me to any references explaining how soft bodies work? especially detail on how to create a squishy bouncing ball
has anyone used numeric with python and blender?
a stupid idea i just had:
if anyone wants to create an external interface to a blender pyton script why not use sockets or python objects! python objects being better in that it gives you direct access to classes. the front end could even be run on a different computer. ( i can think of no real situations where you would want to do this )
cheers!