def updateSessionVariable(var_name, node_path):

    
    curSource = hou.sessionModuleSource()
    lines = curSource.split("\n")

    for i, line in enumerate(lines):
        if line.split('=')[0].strip(' ') == var_name:
            line = "%s = '%s'" % (var_name, node_path)
            lines[i] = line
    data = '\n'.join(lines)
    # print data

    hou.setSessionModuleSource(data)
    def save_bookmarks_to_session(self, event_type, *args, **kwargs):
        if event_type not in [hou.hipFileEventType.BeforeSave, hou.hipFileEventType.BeforeClear, "Widget Closed"]:
            return
        selection = range(self.tab_widget.count())
        data = prepare_save_data(self.tab_widget, selection)
        code = ("# HOUDINI BOOKMARKS START \n"
                        "def get_houdini_bookmarks_data(): \n"
                        "   return "+str(data)+"\n"
                        "# HOUDINI BOOKMARS END \n")

        old_data = hou.sessionModuleSource()
        old_data = re.sub("# HOUDINI BOOKMARKS START.*# HOUDINI BOOKMARS END", "", old_data, flags=re.DOTALL)
        if hasattr(hou.session, "get_houdini_bookmarks_data"):
            del(hou.session.get_houdini_bookmarks_data)
        hou.setSessionModuleSource(old_data + "\n" + code)
def deleteSessionVariable(var_name):
    # print 'utils function : delete variable'
    curSource = hou.sessionModuleSource()
    lines = curSource.split("\n")

    for i, line in enumerate(lines):
        if line.split('=')[0].strip(' ') == var_name:
            del lines[i]
            break
    data = '\n'.join(lines)
    # print data

    hou.setSessionModuleSource(data)

    # then the value stays in memory, so explicitly delete it
    cmdString = 'del hou.session.%s' %(var_name)
    try:
        exec(cmdString)
    except:
        print 'unable to delete variable "%s"from memory' % (var_name)
def add_watcher(selection, type_="parm"):
    """ Create a file with the current parameter contents and 
        create a file watcher, if not already created and found in hou.Session,
        add the file to the list of watched files.

        Link the file created to a parameter where the tool has been executed from
        and when the file changed, edit the parameter contents with text contents.
    """

    file_path = get_file_name(selection, type_=type_)

    if type_ == "parm":
        # fetch parm content, either raw value or expression if any
        try:
            data = selection.expression()
        except hou.OperationFailed:
            data = str(selection.eval())
    elif type_ == "python_node":
        data = selection.type().definition().sections()["PythonCook"].contents(
        )

    elif "extra_section|" in type_:

        sec_name = type_.split('|')[-1]
        sec = selection.type().definition().sections().get(sec_name)
        if not sec:
            print("Error: No section {} found.".format(sec))
        data = sec.contents()

    elif type_ == "__temp__python_source_editor":

        data = hou.sessionModuleSource()

    with open(file_path, 'w') as f:
        f.write(data)

    vsc = get_external_editor()
    if not vsc:
        hou.ui.setStatusMessage("No external editor set",
                                severity=hou.severityType.Error)
        return

    p = QtCore.QProcess(parent=hou.ui.mainQtWindow())
    p.start(vsc, [file_path])

    watcher = get_file_watcher()

    if not watcher:

        watcher = QtCore.QFileSystemWatcher([file_path],
                                            parent=hou.ui.mainQtWindow())
        watcher.fileChanged.connect(filechanged)
        hou.session.FILE_WATCHER = watcher

    else:
        if not file_path in watcher.files():

            watcher.addPath(file_path)

    parms_bindings = get_parm_bindings()
    if not parms_bindings:
        hou.session.PARMS_BINDINGS = {}
        parms_bindings = hou.session.PARMS_BINDINGS

    if not file_path in parms_bindings.keys():

        parms_bindings[file_path] = selection

        # add "on removed" callback to remove file from watcher
        # when node is deleted
        if type_ == "python_node" or "extra_section|" in type_:

            selection.addEventCallback((hou.nodeEventType.BeingDeleted, ),
                                       _node_deleted)

    clean_files()