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
    
  
    doc.StartUndo()
    
    for obj in doc.GetActiveObjects(False):
        #start here

	doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
       
        rotx = obj[c4d.ID_BASEOBJECT_REL_ROTATION,c4d.VECTOR_X]
        tag = api.LoadPreset(obj,c4d.Obase,"BounceRandomRotationX")
	try:
		tag[api.GetVariationId(tag,ID.EndValue)[1]]+= rotx
	except:
		pass

	scale = obj[c4d.ID_BASEOBJECT_REL_SCALE]
        tag = api.LoadPreset(obj,c4d.Obase,"BounceSquashStretch")
	try:
        	tag[api.GetVariationId(tag,ID. EndValue)[1]]+= scale
	except:
		pass

	rotz = obj[c4d.ID_BASEOBJECT_REL_ROTATION,c4d.VECTOR_Z]
        tag = api.LoadPreset(obj,c4d.Obase,"BounceFLIPS")
	try:
        	tag[api.GetVariationId(tag,ID.EndValue)[1]]+= rotz
	except:
		pass

	pos =obj[c4d.ID_BASEOBJECT_REL_POSITION]
        BounceY = api.LoadPreset(obj,c4d.Obase,"BouncePositionY")
	try:
		BounceY[api.GetVariationId(BounceY,ID.EndValue)[1]]+= pos
	except:
		pass

        api.LoadPreset(BounceY,c4d.Tbase,"BounceVariationinYTag")
        
        #End here
    doc.EndUndo()
    c4d.EventAdd()
if __name__=='__main__':
    main()
