def METHOD_NAME(app, doctree):
    # first generate figure numbers for each figure
    env = app.builder.env
    docname_figs = getattr(env, 'docname_figs', {})
    docnames_by_figname = getattr(env, 'docnames_by_figname', {})
    for figure_info in doctree.traverse(lambda n: isinstance(n, nodes.figure) or \
                                                  isinstance(n, subfig.subfigend) or \
                                                  isinstance(n, figtable.figtable)):
        for id in figure_info['ids']:
            fig_docname = docnames_by_figname[id] = env.docname
            if fig_docname not in docname_figs:
                docname_figs[fig_docname] = OrderedDict()
            if isinstance(figure_info.parent, subfig.subfig):
                mainid = figure_info.parent['mainfigid']
            else:
                mainid = id
            if mainid not in docname_figs[fig_docname]:
                docname_figs[fig_docname][mainid] = OrderedSet()
            if isinstance(figure_info.parent, subfig.subfig):
                docname_figs[fig_docname][mainid].add(id)
    env.docnames_by_figname = docnames_by_figname
    env.docname_figs = docname_figs