import c4d
from c4d import gui
import imp
import os
import warnings
import random
import time
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
    
    
    FPS = doc.GetFps()
    tags = doc.GetActiveTags()
    random.seed(time.time())
    
    objs = []
    singleObjs = []

    signals = []
    for tag in tags:
	if tag.GetType()==ID.SIGNAL:
		signals.append(tag)
    tags = signals
    if len(tags) <1: return

    for tag in tags:
        objs.append(tag.GetObject())
    
    for obj in objs:
        if obj not in singleObjs:
            singleObjs.append(obj)
    doc.StartUndo()
    for obj in singleObjs:
        allObjTags = obj.GetTags()
        randomInt = random.randrange(0, 500)
        randomInt = c4d.BaseTime((randomInt),FPS)
        for tag in allObjTags:
            if tag in tags:
                doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL, tag)
                if tag.GetType()!=ID.SIGNAL: continue 
      
                
                #Start Here
                
                
                noises = api.GetTabs(tag,ID.Noise)#Gets all Sin tabs     tag, then the type of tab
                allMods = api.GetTabs(tag)#Gets all tabs
                tag[ID.Offset] = randomInt
                for mod in allMods:
                    SeedId = api.GetId(tag,ID.ModTimeOffset,mod)
                    tag[SeedId] = randomInt

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