Output Current Sound Track Name
-
Hello,
"soundwaves" are organized as animation tracks (CTrack) referencing sound files.
You can check if the current frame is in the range of a given CTrack. This range is the result of the start position of the track plus the length of the sound file.
The length of the sound file can be obtained from the track, but the value is stored as a string. So you would have to convert that into a time value (BaseTime).
Such a check can look like this:
start = track[c4d.CID_SOUND_START] # get length # length is stored as a string; need to convert it to int length = track[c4d.CID_SOUND_LENGTH] length = length[:-2] length = int(float(length)) fps = doc.GetFps() end = start + c4d.BaseTime(length, fps) # check if the current frame is within the range of the sound clip curTime = doc.GetTime() if curTime >= start and curTime < end: print("Uses Track: " + track.GetName()) else: print("no hit")
Of course, you have to loop over all sound tracks associated with a given object.
You also find information on CTracks in the C++ documentation: CTrack Manual.
best wishes,
Sebastian -
Thanks so much @s_bach, this is really helpful!
-
@s_bach I'm not super savvy when it comes to programming. If I had 18 or so tracks, what would be the proper way to write this code so it loops through all of them?
-
Hello,
the tracks of an object are stored in a list. You can access the first track using GetFirstCTrack(). Then you can loop over all tracks with an ordinary while() loop and GetNext().
track = op.GetFirstCTrack() while track is not None: print(track.GetName()) track = track.GetNext()
See also the example in the C++ CTrack documentation.
best wishes,
Sebastian -
@s_bach So something like this?
track = op.GetFirstCTrack() while track is not None: start = track[c4d.CID_SOUND_START] # get length # length is stored as a string; need to convert it to int length = track[c4d.CID_SOUND_LENGTH] length = length[:-2] length = int(float(length)) fps = doc.GetFps() end = start + c4d.BaseTime(length, fps) # check if the current frame is within the range of the sound clip curTime = doc.GetTime() if curTime >= start and curTime < end: print("Uses Track: " + track.GetName()) else: print("no hit")
I'm really struggling with wrapping my head around this
-
Well, you indentation is off. And you are missing
track = track.GetNext()
at the end.
Besides that, this should work:
track = op.GetFirstCTrack() while track is not None: print(track.GetName()) start = track[c4d.CID_SOUND_START] # get length # length is stored as a string; need to convert it to int length = track[c4d.CID_SOUND_LENGTH] length = length[:-2] length = int(float(length)) fps = doc.GetFps() end = start + c4d.BaseTime(length, fps) # check if the current frame is within the range of the sound clip curTime = doc.GetTime() if curTime >= start and curTime < end: print("Uses Track: " + track.GetName()) else: print("no hit") track = track.GetNext()
I strongly suggest to get familiar with the basics of Python first.
best wishes,
Sebastian -
Thanks @s_bach, I really appreciate your help. I’m more of a motion designer, but wanted to tailor the scene I’m working on to make it easier.
HTML/CSS come really easy to me, but my brain just comes to a halt when I try and get my head around python
-
Also, with this code, I am getting the spinning beachball of death
It must be trapped in the loop maybe? -
Hello,
are you sure the indentation of your code is correct? Do you call
track = track.GetNext()
at the end of your loop?
What is the context of your code? Are you calling it whining a script or a Python Tag? Please post the full code you are actually using.
If a "while" loop is not suitable, you could also use GetCTracks() to get a list of all the tracks.
tracks = op.GetCTracks() for track in tracks: print(track.GetName())
best wishes,
Sebastian -
@s_bach Okay it's kind of working now, but is only outputting the last track in the list. I am using this script in a python tag:
import c4d #Welcome to the world of Python def main(): # find object in scene, store in obj obj = doc.SearchObject('VO') # store some variables track = obj.GetCTracks() obj[c4d.ID_USERDATA,4] = track[0].GetName() track[0][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,6] track[0][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,7] track[0][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,14] obj[c4d.ID_USERDATA,10] = track[1].GetName() track[1][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,11] track[1][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,12] track[1][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,9] obj[c4d.ID_USERDATA,15] = track[2].GetName() track[2][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,16] track[2][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,17] track[2][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,18] obj[c4d.ID_USERDATA,3] = track[3].GetName() track[3][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,20] track[3][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,21] track[3][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,22] obj[c4d.ID_USERDATA,24] = track[4].GetName() track[4][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,25] track[4][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,26] track[4][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,27] obj[c4d.ID_USERDATA,29] = track[5].GetName() track[5][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,30] track[5][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,31] track[5][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,32] obj[c4d.ID_USERDATA,34] = track[6].GetName() track[6][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,35] track[6][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,36] track[6][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,37] obj[c4d.ID_USERDATA,39] = track[7].GetName() track[7][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,40] track[7][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,41] track[7][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,42] obj[c4d.ID_USERDATA,44] = track[8].GetName() track[8][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,45] track[8][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,46] track[8][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,47] obj[c4d.ID_USERDATA,49] = track[9].GetName() track[9][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,50] track[9][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,51] track[9][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,52] obj[c4d.ID_USERDATA,54] = track[10].GetName() track[10][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,55] track[10][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,56] track[10][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,57] obj[c4d.ID_USERDATA,59] = track[11].GetName() track[11][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,60] track[11][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,61] track[11][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,62] obj[c4d.ID_USERDATA,64] = track[12].GetName() track[12][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,65] track[12][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,66] track[12][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,67] obj[c4d.ID_USERDATA,69] = track[13].GetName() track[13][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,70] track[13][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,71] track[13][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,72] obj[c4d.ID_USERDATA,74] = track[14].GetName() track[14][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,75] track[14][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,76] track[14][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,77] obj[c4d.ID_USERDATA,79] = track[15].GetName() track[15][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,80] track[15][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,81] track[15][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,82] obj[c4d.ID_USERDATA,84] = track[16].GetName() track[16][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,85] track[16][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,86] track[16][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,87] obj[c4d.ID_USERDATA,89] = track[17].GetName() track[17][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,90] track[17][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,91] track[17][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,92] # outputs the current track name where playhead is traks = obj.GetCTracks() for trak in traks: start = trak[c4d.CID_SOUND_START] # get length # length is stored as a string; need to convert it to int length = trak[c4d.CID_SOUND_LENGTH] length = length[:-2] length = int(float(length)) fps = doc.GetFps() end = start + c4d.BaseTime(length, fps) # check if the current frame is within the range of the sound clip curTime = doc.GetTime() if curTime >= start and curTime < end: obj[c4d.ID_USERDATA,19] = trak.GetName() else: obj[c4d.ID_USERDATA,19] = "No Track" trak = trak.GetNext()
-
Hello,
it seems you just write the name of the last track into your user data field:
obj[c4d.ID_USERDATA,19] = trak.GetName()
Since you do not extend that string, it can only show one name.
best wishes,
Sebastian