PDA

View Full Version : Bounce Expression on Python in Cinema 4D


WotS
02-07-2013, 12:02 AM
Hello.

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){
n--;
}
}
if (n == 0){
t = 0;
}else{
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);
}else{
value;
}

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)
lst.append(keytime)
lstS = []
for i in range(0,len(lst)):
sub = abs(lst-curFrame)
lstS.append(sub)
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
else:
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)
else:
curValue
http://cs419629.userapi.com/v419629100/24a8/KbzWb_iTeUs.jpg

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

littledevil
02-07-2013, 02:23 PM
1. Post your code in a readable format, either by using tags or better by providing a link like http://codepad.org/. 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.

CGTalk Moderation
02-07-2013, 02:23 PM
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.