"""
HB_ToggleMode V1.0

Last Modified: 6/25/2015
Works with CINEMA 4D R16.050 and up.
Copyright: Holger Biebrach, www.c4dstuff.com

Name-US: HB_ToggleMode
Description-US: Toggles Components Mode and does some other niffty things. (Details in Script)

This Script does following things:
1. Toggle Components Mode (Points/Edges/Polygons)
2. Selects all non parametric Objects that are child of selected Objects (eg. If you have selected a SubdivisionSurface the Script will 
select all Polygonobjects that are children)
2. Enables Displayfilter Splines (Only If splines are selected)
3. Enables Xray Display if Splines are Selected and keeps Generarors like Sweep selected so you can see through them.
4. Selects and Frames Instance-Referenz if Instance is selected and creates a temporal ModellingCamera

Video Tutorial:
https://youtu.be/eEcAttV56kE?t=10m46s


Name-DE: HB_ToggleMode
Description-DE: Wechselt zwischen Punkte/Kanten/Polygon Modus und macht ein paar andere praktische dinge.

ChangeLog:

10/5/2015 
-CorrectionDeformer works now

6/11/2015 Release Version

"""
import c4d
from c4d import documents, storage

def GetNextObject(op,root):
    
    if op.GetType()==c4d.Opolygon or op.GetType()==c4d.Ospline or op.GetType()==c4d.Osweep or op.GetType()==c4d.Olathe or op.GetType()==1026224 or op.GetType()==1024542:
        op.SetBit(c4d.BIT_ACTIVE)
    if op.GetType()==c4d.Ospline or op.GetType()==c4d.Osweep or op.GetType()==c4d.Olathe:
        drawBD = doc.GetActiveBaseDraw()
        drawBD[c4d.BASEDRAW_DATA_XRAY]=True
        drawBD[c4d.BASEDRAW_DISPLAYFILTER_SPLINE]=True

    if op==None:
        return None
  
    if op.GetDown():
        return op.GetDown()
  
    while not op.GetNext() and op!=root and op.GetUp()!=root and op.GetUp():
        op = op.GetUp()

    if op !=root:
        return op.GetNext()
 
 
def IterateChildren(objlist):
   
    for obj in objlist:
        
        if obj.GetType()!=c4d.Opolygon or obj.GetType()!=c4d.Ospline or obj.GetType()!=c4d.Osweep or obj.GetType()!=c4d.Olathe or obj.GetType()!=c4d.Osymmetry or op.GetType()==1024542:
            obj.DelBit(c4d.BIT_ACTIVE)
        
        root = obj
        while obj:
            obj = GetNextObject(obj,root)
            
    
    if obj is None:
        return

    return


# If selection is INSTANCE
        
def InstanceCheck(SelObj):
    for obj in SelObj:
        if obj.GetType()==c4d.Oinstance:
            reflink=obj[c4d.INSTANCEOBJECT_LINK]
            reflink.SetBit(c4d.BIT_ACTIVE)
            obj.DelBit(c4d.BIT_ACTIVE)
            
            #Setup Modelling Camera
            CamPos = doc.GetActiveBaseDraw()[c4d.BASEDRAW_DATA_CAMERA].GetMg()
            if not doc.SearchObject("HB_ModellingCamera_ROOT"):
                
                Camera_ROOT=c4d.BaseObject(c4d.Onull)
                Camera_ROOT.SetName("HB_ModellingCamera_ROOT")
        
                doc.AddUndo(c4d.UNDOTYPE_NEW, Camera_ROOT)
                Camera_ROOT[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR]=1
                doc.InsertObject(Camera_ROOT)
                
                WorkplaneCamera=c4d.BaseObject(c4d.Ocamera)
                WorkplaneCamera.SetName("ModellingCamera")
                doc.AddUndo(c4d.UNDOTYPE_NEW, WorkplaneCamera)
                WorkplaneCamera.InsertUnder(Camera_ROOT)
                WorkplaneCamera.SetMg(CamPos)
      
            
            doc.GetActiveBaseDraw()[c4d.BASEDRAW_DATA_CAMERA]=WorkplaneCamera
                    
            
            
            c4d.CallCommand(13038) # Frame Selected Elements
                
 
def main():

    if InstanceCheck(doc.GetSelection()):
        InstanceCheck(doc.GetSelection())
        c4d.CallCommand(13322) # Toggle Modeling Mode
        return
    
    IterateChildren(doc.GetSelection())    
    c4d.CallCommand(13322) # Toggle Modeling Mode
    c4d.EventAdd()

if __name__=='__main__':
    main()
