Bounce Expression on Python in Cinema 4D

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  02 February 2013
Question Bounce Expression on Python in Cinema 4D


This is one of the most useful scripts for After Effects, that adds oscillatory train to any animated property.

n = 0;
if (numKeys > 0){
n = nearestKey(time).index;
if (key(n).time > time){
if (n == 0){
t = 0;
t = time - key(n).time;
if (n > 0){
v = velocityAtTime(key(n).time - thisComp.frameDuration/10);
amp = .5;
freq = 4.0;
decay = 2.0;
value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);

Script can be added as expression to any property move, size, rotation, opacity, color, etc - and influence this feature with the keys, adding oscillatory train in the end. So, I'm trying to write this script on Python in Cinema 4D. And here is what I got.

import math
import c4d
#Welcome to the world of Python

def main():
obj = doc.SearchObject("Test_Cube")
track = obj.GetFirstCTrack()
curve = track.GetCurve()
numkeys = curve.GetKeyCount()
n = 0
time = doc.GetTime()
fps = doc.GetFps()
curFrame = time.GetFrame(fps)
lst = []
for i in range(0, numkeys):
key = curve.GetKey(i).GetValue()
keytime = curve.GetKey(i).GetTime().GetFrame(fps)
lstS = []
for i in range(0,len(lst)):
sub = abs(lst-curFrame)
for i, j in enumerate(lstS):
if j == min(lstS):
MinInd = i
nearestKey = lst[MinInd]
for i, j in enumerate(lst):
if j == nearestKey:
nearestKeyIndex = i
if numkeys > 0:
n = nearestKeyIndex
if nearestKey > curFrame:
n = n-1
if n == 0:
t = 0
t = curFrame - nearestKey
lastKeyValue = curve.GetKey(numkeys-1).GetValue()
previousKeyValue = curve.GetKey(numkeys-2).GetValue()
curValue = curve.GetValue(time,fps)
if n > 0:
v = (lastKeyValue - previousKeyValue)*fps
amp = 0.5
freq = 4
decay = 2
curValue + v*amp*math.sin(freq*t*2*3.14)/math.exp(decay*t)

In Cinema 4D there are no such functions as [I]nearestKey
and velocityAtTime unlike After Effects. My script does not work in Cinema 4D. For writing it I used the Python tag. Compile says "No errors". This script is Executed. But not the desired effect. There is no change in the object's animation. I count on your help guys.
P.S. I am sorry for my bad English
  02 February 2013
1. Post your code in a readable format, either by using [kode][/kode] tags or better by providing a link like At least I am unwilling to read your code in this form.

2. To get the closest key you have just to loop through your curve and get the best key match.

3. The velocity is the length of the vector CurVector - PrevVector. If you want the position velocity, cache the global position of your point(s) and then calculate the velocity with it.

Edit : ok, the picture an be enlarged, still not an optimal solution for more complex code, when you might want to execute it for yourself. You main error seems to be that you never write your values back or do anything at all with them. curValue just sits there and does nothing. I also do not really understand, what you are trying to do with all loops and the mapping in your code, to get the closest key just loop once through your curve and get the best key.time/currenttime match.

Last edited by littledevil : 02 February 2013 at 02:39 PM.
  02 February 2013
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 03:17 PM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.