# -*- coding: utf-8 -*-
# bh_keyLightBrightnessFromTextFile - by Horganovski
# refined by Niklas Rosenstein <rosensteinniklas@googlemail.com>
# from https://www.c4dcafe.com/ipb/forums/topic/68561-python-example-create-animation-from-text-file/
import os
import re
import c4d
line_match = re.compile('^s*(-|+)?(d*.?d*)')
def readKeysFile(path):
if not os.path.exists(path):
return None
keys = []
with open(path) as fl:
for line in fl:
value = line_match.match(line)
if value and value.group(2):
string = (value.group(1) or '+') + value.group(2)
keys.append(float(string))
else:
keys.append(None)
return keys
def main():
path = c4d.storage.LoadDialog()
if not path:
return
offset = None
while True:
offset = c4d.gui.InputDialog('Enter the start-frame:')
if not offset:
return
try:
offset = int(offset)
break
except ValueError:
offset = None
c4d.gui.MessageDialog('Not a number.')
keys = readKeysFile(path)
if not keys:
c4d.gui.MessageDialog("No keys loaded.")
return
fps = doc.GetFps()
objects = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
for op in objects:
track = op.FindCTrack(c4d.LIGHT_BRIGHTNESS)
if not track:
track = c4d.CTrack(op, c4d.LIGHT_BRIGHTNESS)
op.InsertTrackSorted(track)
curve = track.GetCurve()
for i, key_value in enumerate(keys):
if key_value is None:
pass
else:
frame = offset + i
frame_time = c4d.BaseTime(frame, fps)
key_dict = curve.AddKey(frame_time)
key_dict['key'].SetValue(curve, key_value)
c4d.EventAdd()
main()