#######################################################################################################################

__author__ = "Boris Martinez Castillo"
__version__ = "1.0.1"
__maintainer__ = "Boris Martinez Castillo"
__email__ = "boris.vfx@outlook.com"

########################################################################################################################

def is_gizmo(node):
    return type(node) == nuke.Gizmo
    

def gizmo_exporter(node,path):

    node = node
    name = node['name'].getValue()
    path = path + name + "." + "gizmo"
    print "PATH: ", path
    nuke.nodeCopy(path)

    return
 
 
def batch_gizmo_exporter(path):
    
    print path 
    
    node_list = []

    for node in nuke.selectedNodes():
        node_list.append(node['name'].getValue())
        node['selected'].setValue(False)

    for name in node_list:
        n = nuke.toNode(name)
        print name
        n['selected'].setValue(True)
        print "IS GIZMO: {}".format(is_gizmo(n))
        if is_gizmo(n):
            new_group = n.makeGroup()
            new_group['name'].setValue(name + "_togroup")
            new_group['selected'].setValue(True)
            gizmo_exporter(new_group,path)
            nuke.delete(new_group)
        else:
            gizmo_exporter(n,path)
            n['selected'].setValue(False)
                       
                       
class Panel(nukescripts.PythonPanel):

    def __init__(self):
        nukescripts.PythonPanel.__init__(self,"b_gizmo_snatcher")
        
        self.size = self.setMinimumSize(650,160)
        
        #CREATE KNOBS
        
        self.filepath = nuke.File_Knob('snatch path: ')
        self.author = nuke.Text_Knob("www.boris-mc.com")
        self._help = nuke.Help_Knob("just select your snatch folder and click ok!","help")
       
        #ADD KNOBS
        
        for i in (self.filepath,self.author,self._help):
            self.addKnob(i)
        
        
def main_function():
    
    panel = Panel()
    
    if not panel.showModalDialog():
        print "script aborted"
        return
       
    else:
        path = panel.filepath.getValue()
        batch_gizmo_exporter(path)    
        
            
if __name__ ==  "__main__":
    main_function()
    
        
