source/blender/blenloader/intern/readfile.c
static void library_link_end(Main *mainl,
                             FileData **fd,
                             Main *bmain,
                             const int flag,
                             Scene *scene,
                             ViewLayer *view_layer,
                             const View3D *v3d)
{
  Main *mainvar;
  Library *curlib;

  /* expander now is callback function */
  BLO_main_expander(expand_doit_library);

  /* make main consistent */
  BLO_expand_main(*fd, mainl);

  /* do this when expand found other libs */
  read_libraries(*fd, (*fd)->mainlist);

  curlib = mainl->curlib;

  /* make the lib path relative if required */
  if (flag & FILE_RELPATH) {
    /* use the full path, this could have been read by other library even */
    BLI_strncpy(curlib->filepath, curlib->filepath_abs, sizeof(curlib->filepath));

    /* uses current .blend file as reference */
    BLI_path_rel(curlib->filepath, BKE_main_blendfile_path_from_global());
  }

  blo_join_main((*fd)->mainlist);
  mainvar = (*fd)->mainlist->first;
  mainl = NULL; /* blo_join_main free's mainl, cant use anymore */

  lib_link_all(*fd, mainvar);

  /* Some versioning code does expect some proper userrefcounting, e.g. in conversion from
   * groups to collections... We could optimize out that first call when we are reading a
   * current version file, but again this is really not a bottle neck currently. so not worth
   * it. */
  BKE_main_id_refcount_recompute(mainvar, false);

  BKE_collections_after_lib_link(mainvar);

  /* Yep, second splitting... but this is a very cheap operation, so no big deal. */
  blo_split_main((*fd)->mainlist, mainvar);
  Main *main_newid = BKE_main_new();
  for (mainvar = ((Main *)(*fd)->mainlist->first)->next; mainvar; mainvar = mainvar->next) {
    BLI_assert(mainvar->versionfile != 0);
    /* We need to split out IDs already existing,
     * or they will go again through do_versions - bad, very bad! */
    split_main_newid(mainvar, main_newid);

    do_versions_after_linking(main_newid, (*fd)->reports);

    add_main_to_main(mainvar, main_newid);
  }

  BKE_main_free(main_newid);
  blo_join_main((*fd)->mainlist);
  mainvar = (*fd)->mainlist->first;
  MEM_freeN((*fd)->mainlist);

  /* This does not take into account old, deprecated data, so we also have to do it after
   * `do_versions_after_linking()`. */
  BKE_main_id_refcount_recompute(mainvar, false);

  /* After all data has been read and versioned, uses LIB_TAG_NEW. */
  ntreeUpdateAllNew(mainvar);

  placeholders_ensure_valid(mainvar);

  BKE_main_id_tag_all(mainvar, LIB_TAG_NEW, false);

  /* Make all relative paths, relative to the open blend file. */
  fix_relpaths_library(BKE_main_blendfile_path(mainvar), mainvar);

  /* Give a base to loose objects and collections.
   * Only directly linked objects & collections are instantiated by
   * #BLO_library_link_named_part & co,
   * here we handle indirect ones and other possible edge-cases. */
  if (flag & BLO_LIBLINK_NEEDS_ID_TAG_DOIT) {
    /* Should always be true. */
    if (scene != NULL) {
      add_collections_to_scene(mainvar, bmain, scene, view_layer, v3d, curlib, flag);
      add_loose_objects_to_scene(mainvar, bmain, scene, view_layer, v3d, curlib, flag);
      add_loose_object_data_to_scene(mainvar, bmain, scene, view_layer, v3d, flag);
    }

    /* Clear objects and collections instantiating tag. */
    library_link_clear_tag(mainvar, flag);
  }

  /* patch to prevent switch_endian happens twice */
  if ((*fd)->flags & FD_FLAGS_SWITCH_ENDIAN) {
    blo_filedata_free(*fd);
    *fd = NULL;
  }
}
/**
 * \param scene: The scene in which to instantiate objects/collections
 * (if NULL, no instantiation is done).
 * \param v3d: The active 3D viewport.
 * (only to define active layers for instantiated objects & collections, can be NULL).
 */
\param scene: The scene in which to instantiate objects/collections
(if NULL, no instantiation is done).
\param v3d: The active 3D viewport.
(only to define active layers for instantiated objects & collections, can be NULL).
\ param scene : The scene in which to instantiate objects / collections ( if NULL no instantiation is done ) . \ param v3d : The active 3D viewport . ( only to define active layers for instantiated objects & collections can be NULL ) .
static void library_link_end(Main *mainl,
                             FileData **fd,
                             Main *bmain,
                             const int flag,
                             Scene *scene,
                             ViewLayer *view_layer,
                             const View3D *v3d)
{
  Main *mainvar;
  Library *curlib;
  BLO_main_expander(expand_doit_library);
  BLO_expand_main(*fd, mainl);
  read_libraries(*fd, (*fd)->mainlist);
  curlib = mainl->curlib;
  if (flag & FILE_RELPATH) {
    BLI_strncpy(curlib->filepath, curlib->filepath_abs, sizeof(curlib->filepath));
    BLI_path_rel(curlib->filepath, BKE_main_blendfile_path_from_global());
  }
  blo_join_main((*fd)->mainlist);
  mainvar = (*fd)->mainlist->first;
  mainl = NULL; 
  lib_link_all(*fd, mainvar);
  BKE_main_id_refcount_recompute(mainvar, false);
  BKE_collections_after_lib_link(mainvar);
  blo_split_main((*fd)->mainlist, mainvar);
  Main *main_newid = BKE_main_new();
  for (mainvar = ((Main *)(*fd)->mainlist->first)->next; mainvar; mainvar = mainvar->next) {
    BLI_assert(mainvar->versionfile != 0);
    split_main_newid(mainvar, main_newid);
    do_versions_after_linking(main_newid, (*fd)->reports);
    add_main_to_main(mainvar, main_newid);
  }
  BKE_main_free(main_newid);
  blo_join_main((*fd)->mainlist);
  mainvar = (*fd)->mainlist->first;
  MEM_freeN((*fd)->mainlist);
  BKE_main_id_refcount_recompute(mainvar, false);
  ntreeUpdateAllNew(mainvar);
  placeholders_ensure_valid(mainvar);
  BKE_main_id_tag_all(mainvar, LIB_TAG_NEW, false);
  fix_relpaths_library(BKE_main_blendfile_path(mainvar), mainvar);
  if (flag & BLO_LIBLINK_NEEDS_ID_TAG_DOIT) {
    if (scene != NULL) {
      add_collections_to_scene(mainvar, bmain, scene, view_layer, v3d, curlib, flag);
      add_loose_objects_to_scene(mainvar, bmain, scene, view_layer, v3d, curlib, flag);
      add_loose_object_data_to_scene(mainvar, bmain, scene, view_layer, v3d, flag);
    }
    library_link_clear_tag(mainvar, flag);
  }
  if ((*fd)->flags & FD_FLAGS_SWITCH_ENDIAN) {
    blo_filedata_free(*fd);
    *fd = NULL;
  }
}
static void library_link_end ( Main * mainl , FileData * * fd , Main * bmain , const int flag , Scene * scene , ViewLayer * view_layer , const View3D * v3d ) { Main * mainvar ; Library * curlib ; BLO_main_expander ( expand_doit_library ) ; BLO_expand_main ( * fd , mainl ) ; read_libraries ( * fd , ( * fd ) -> mainlist ) ; curlib = mainl -> curlib ; if ( flag & FILE_RELPATH ) { BLI_strncpy ( curlib -> filepath , curlib -> filepath_abs , sizeof ( curlib -> filepath ) ) ; BLI_path_rel ( curlib -> filepath , BKE_main_blendfile_path_from_global ( ) ) ; } blo_join_main ( ( * fd ) -> mainlist ) ; mainvar = ( * fd ) -> mainlist -> first ; mainl = NULL ; lib_link_all ( * fd , mainvar ) ; BKE_main_id_refcount_recompute ( mainvar , false ) ; BKE_collections_after_lib_link ( mainvar ) ; blo_split_main ( ( * fd ) -> mainlist , mainvar ) ; Main * main_newid = BKE_main_new ( ) ; for ( mainvar = ( ( Main * ) ( * fd ) -> mainlist -> first ) -> next ; mainvar ; mainvar = mainvar -> next ) { BLI_assert ( mainvar -> versionfile != 0 ) ; split_main_newid ( mainvar , main_newid ) ; do_versions_after_linking ( main_newid , ( * fd ) -> reports ) ; add_main_to_main ( mainvar , main_newid ) ; } BKE_main_free ( main_newid ) ; blo_join_main ( ( * fd ) -> mainlist ) ; mainvar = ( * fd ) -> mainlist -> first ; MEM_freeN ( ( * fd ) -> mainlist ) ; BKE_main_id_refcount_recompute ( mainvar , false ) ; ntreeUpdateAllNew ( mainvar ) ; placeholders_ensure_valid ( mainvar ) ; BKE_main_id_tag_all ( mainvar , LIB_TAG_NEW , false ) ; fix_relpaths_library ( BKE_main_blendfile_path ( mainvar ) , mainvar ) ; if ( flag & BLO_LIBLINK_NEEDS_ID_TAG_DOIT ) { if ( scene != NULL ) { add_collections_to_scene ( mainvar , bmain , scene , view_layer , v3d , curlib , flag ) ; add_loose_objects_to_scene ( mainvar , bmain , scene , view_layer , v3d , curlib , flag ) ; add_loose_object_data_to_scene ( mainvar , bmain , scene , view_layer , v3d , flag ) ; } library_link_clear_tag ( mainvar , flag ) ; } if ( ( * fd ) -> flags & FD_FLAGS_SWITCH_ENDIAN ) { blo_filedata_free ( * fd ) ; * fd = NULL ; } }
\param scene: The scene in which to instantiate objects/collections
 (if NULL, no instantiation is done).
\ param scene : The scene in which to instantiate objects / collections ( if NULL no instantiation is done ) .
/* expander now is callback function */ /* make main consistent */ /* do this when expand found other libs */ /* make the lib path relative if required */ /* use the full path, this could have been read by other library even */ /* uses current .blend file as reference */ /* blo_join_main free's mainl, cant use anymore */ /* Some versioning code does expect some proper userrefcounting, e.g. in conversion from
   * groups to collections... We could optimize out that first call when we are reading a
   * current version file, but again this is really not a bottle neck currently. so not worth
   * it. */ /* Yep, second splitting... but this is a very cheap operation, so no big deal. */ /* We need to split out IDs already existing,
     * or they will go again through do_versions - bad, very bad! */ /* This does not take into account old, deprecated data, so we also have to do it after
   * `do_versions_after_linking()`. */ /* After all data has been read and versioned, uses LIB_TAG_NEW. */ /* Make all relative paths, relative to the open blend file. */ /* Give a base to loose objects and collections.
   * Only directly linked objects & collections are instantiated by
   * #BLO_library_link_named_part & co,
   * here we handle indirect ones and other possible edge-cases. */ /* Should always be true. */ /* Clear objects and collections instantiating tag. */ /* patch to prevent switch_endian happens twice */