What about this code:

Hello!

On BlenderBuch.de, a German page mainly on Pyhton, I found that code:

001:  ###################################################################
002:  # grav1.py, 11.99 Martin Strubel
003:  # Erster Ansatz einer (weniger exakten) Gravitations-Simulation
004:  ###################################################################
005:  # aenderbare Parameter
006:  g = 0.1         # Gravitationswert, am besten zwischen 0.01 und 0.5
007:  elast = 0.7     # Elastizitaet
008:  threshold = 0.5
009:  ###################################################################
010:
011:  from Blender import *
012:
013:  time = Get(Const.BP_CURTIME) - 1.0
014:  ob = link
015:
016:  if (time == 0.0):
017:    try:    # existiert schon ein init-Wert fuer das Objekt "ob" ?
018:      if(init.has_key(ob.name)):
019:        ob.LocX = init[ob.name][0]
020:        ob.LocY = init[ob.name][1]
021:        ob.LocZ = init[ob.name][2]
022:        ob.dLocZ = 0.0
023:      else: # wenn nicht, dann setzen:
024:        init[ob.name] = [ob.LocX, ob.LocY, ob.LocZ]
025:    except: # falls das init-dictionary noch gar nicht existiert
026:      init = {}
027:      otime = {}
028:      init[ob.name] = [ob.LocX, ob.LocY, ob.LocZ]
029:  else:
030:    dt = time - otime[ob.name]
031:    if (dt != 0):
032:      ob.LocZ = ob.LocZ + ob.dLocZ * dt
033:      ob.dLocZ = ob.dLocZ - g * dt
034:      if (ob.LocZ < 0.0):
035:        ob.LocZ = 0.0
036:        ob.dLocZ = - elast * ob.dLocZ
037:        if (abs(ob.dLocZ) < threshold):
038:          ob.dLocZ = 0.0
039:  otime[ob.name] = time

But it does neither work on 2.25 nor on 2.23.
the blend file is the grav.blend in this archive

I quite a “newbie” concerning python, but perhaps you could be so kind and explain the changes in the Blender-Class to me (that seems to be the problem, for line 13 causes the trouble).

Thank you,
Darchethiel.

001:  ###################################################################
002:  # grav1.py, 11.99 Martin Strubel
003:  # Erster Ansatz einer (weniger exakten) Gravitations-Simulation
004:  ###################################################################
005:  # aenderbare Parameter
006:  g = 0.1         # Gravitationswert, am besten zwischen 0.01 und 0.5
007:  elast = 0.7     # Elastizitaet
008:  threshold = 0.5
009:  ###################################################################
010:
011:  from Blender import *
012:
013:  time = Get(Const.BP_CURTIME) - 1.0
014:  ob = link
015:
016:  if (time == 0.0):
017:    try:    # existiert schon ein init-Wert fuer das Objekt "ob" ?
018:      if(init.has_key(ob.name)):
019:        ob.LocX = init[ob.name][0]
020:        ob.LocY = init[ob.name][1]
021:        ob.LocZ = init[ob.name][2]
022:        ob.dLocZ = 0.0
023:      else: # wenn nicht, dann setzen:
024:        init[ob.name] = [ob.LocX, ob.LocY, ob.LocZ]
025:    except: # falls das init-dictionary noch gar nicht existiert
026:      init = {}
027:      otime = {}
028:      init[ob.name] = [ob.LocX, ob.LocY, ob.LocZ]
029:  else:
030:    dt = time - otime[ob.name]
031:    if (dt != 0):
032:      ob.LocZ = ob.LocZ + ob.dLocZ * dt
033:      ob.dLocZ = ob.dLocZ - g * dt
034:      if (ob.LocZ < 0.0):
035:        ob.LocZ = 0.0
036:        ob.dLocZ = - elast * ob.dLocZ
037:        if (abs(ob.dLocZ) < threshold):
038:          ob.dLocZ = 0.0
039:  otime[ob.name] = time

This code uses the API, I believe, from Blender 1.8. Under 2.23 and 2.25, try this for line 13:

time = Blender.Get('curtime') - 1

I’m not quite sure about the rest, it looks like it could work (Only about 3 months of Python experience here).

Just curious…did you get any syntax errors involving the format ? …the lines being numbered ?..anyway, I tried the code…new to Python myself…not working for me.

you have to take off the line numbers indeed.

Martin