import c4d
from c4d import gui
import imp
import os
import warnings
warnings.filterwarnings("ignore")
dir, file = os.path.split(__file__)


count =0
stop = False
head = dir
tail = ""
stop = False
while tail != "Signal Scripts":
    count = count+1
    dir = head
    head, tail = os.path.split(head)
    if count >10:
        stop = True
        c4d.gui.MessageDialog("Error with Signal.\nPlease try to reinstall the scripts.")
        break 

try:
	api = imp.load_source('Functions.txt',os.path.join(dir,"res", "Functions.txt"))
	ID = imp.load_source('Ids.txt',os.path.join(dir,"res", "Ids.txt"))
except:
	stop = True
	c4d.gui.MessageDialog("Error with Signal.\nPlease try to reinstall the scripts.")
        

def main():
    if stop == True:
        return
    
    
    
    tags = doc.GetActiveTags()


    signals = []
    for tag in tags:
	if tag.GetType()==ID.SIGNAL:
		signals.append(tag)
    tags = signals
    if len(tags) <1: return
    place = 0
    button = {'id': [ID.KeyframeConvertToKeyframesNoUndo]}
    


   
    mintime = c4d.BaseTime(0)
    maxtime = c4d.BaseTime(0);
    
    fps = doc.GetFps()
    mintime = doc.GetMinTime()
    maxtime = doc.GetMaxTime()
    
    startframe =mintime.GetFrame(fps)
    endframe=maxtime.GetFrame(fps)
    if(endframe-startframe>1000):
    
        if c4d.gui.QuestionDialog("Are you sure you want to key frame "+ str(endframe-startframe) +" frames?")==False:
		return
    
    doc.StartUndo()
    for tag in tags:
	if tag[ID. EffectedObjectClassificaton] == c4d.Obase:
		doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, tag.GetObject())
	else:
		doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, tag[ID.LinkEffectedObject])
	doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, tag)

        place+=1
        
        
        
        
        #Start Here
        
        tag.Message(c4d.MSG_DESCRIPTION_COMMAND,button)
  

        
        
        #End here   
    c4d.EventAdd()
    doc.EndUndo()
    
    
    
if __name__=='__main__':
    main()
