ode, kinetics kit,anything else dynamic ( was ode sample )

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 :wink: 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 :wink: )

cheers!

Are you sure that you need math.radians()?
Without this function, it works very well under win98.

you can also replace .radians with .sin , for example, and it’ll sure look funny - but working.

great! thanks for the replies!

i shall try just as soon as i put some eyeballs in a head…

But you need degrees for the rot object’s value.

If the sphere touchs the cube, there is a math domain error here:
angle_y=D=asin(math.asin(rot[2]))

http://www.zoo-logique.org/3D.Blender/blenderfiles/odetest2.blend
http://www.zoo-logique.org/3D.Blender/blenderfiles/odetest3.blend

mmm … I did it with .sin, but there was no error here on my box … only funny moves.

odetest3 looks quite good…

http://www.zoo-logique.org/3D.Blender/blenderfiles/odetest4.blend
This one seems better.

now i have finished animgrid i can look at this again…

/me starts downloading blends

no /me doesnt :frowning:

if i try to open the blends i get this:

streamGlue header read. Magic confirmed
streamGlue header CRC correct
read action 4 will get 21075 raw bytes
streamGlue header read. Magic confirmed
streamGlue header CRC correct
read action 6 will get 21059 raw bytes
BLO_in_de_flateHeaderStruct Magic confirmed
BLO_in_de_flateHeader CRC correct
BLO_inflate_process gets 21035 compressed bytes, will be 65252 uncompressed
Note: inflate returned Z_STREAM_END
inflated 21035 to 65252 (flushes) err=0
streamGlue header read. Magic confirmed
streamGlue header CRC correct
read action 2 will get 65236 raw bytes
Failed adler checksum

i seem to remember reading something about it … help?

It is just compressed files.
Try to re-download the odetest4.blend

jms, oh wow yes thats very nice.

now it is possible to use triangle meshes with ode isnt it? is it worth writing a routine or are they going to implemenent it in a few blender versions time?

with motors and hinges it would be quite possible to make something like moving cogs no?

i would also obv like to store this info for animations. ipo curves would seem to be the obv answer but at the moment i cant think how to do it. plotting every frame would be easy i guess but not very nice.

did i here something about a python hook for when the frame changed? or was it just on someones wish list…

Create and fill ipos? hum… Maybe.

A test without ipo.
http://www.zoo-logique.org/3D.Blender/animations/ODEtest.AVI

how did you do that?!

http://www.zoo-logique.org/3D.Blender/blenderfiles/odetest5.blend
First run the script . After that go to the render window and press the ANIM button.

The original script has written an other script in an other text buffer
named Text and this last one is yet linked to the camera.

thats a very sneaky method!

the script links must of been what i was talking about earlier.

i cant decide how good or bad storing the data is in a text window. its seems wrong but …

it seems more sensible to store the info to a file but then you just have to load the file each time.

im trying to think about a class to store start and end times and such so animations can be stored as “just the interesting bit” but its getting too late. im going to give my wizard hair

remove these three lines:

 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

it works a whole load nicer.

to store as an ipo curve, would i be right in saying that you(me) just need to calculate the vector at each point and only plot a point on the curve when this changes? im desperately trying to remember maths lessons.

i need to see how ( and if ) ipo curves work first :slight_smile:

jms: when i press the animate button it only renders about two frames and gives up. also quite often it says that the Pos var has not been set.

It works for me but you can try this new one :
http://www.zoo-logique.org/3D.Blender/blenderfiles/odetest5.blend

still no joy jms - i need to get cvs blender. think i need new automake and autoconf. oh well - had to happen sometime and im just dying to try my hand and adding things to the python api.

going to “finish” my animgrid script first. just got a couple of features in the pipe line which i want for my wizard wip.

and im def not going to mention l-systems, soft bodies or cloth…

the last jms’s method works flawlessly here

dunkfordyce, what wizards are you making?