| 
									
										
										
										
											2022-02-11 09:07:11 +11:00
										 |  |  | /* SPDX-License-Identifier: GPL-2.0-or-later */ | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup pythonintern | 
					
						
							| 
									
										
										
										
											2011-11-05 08:21:12 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file exposed blend file library appending/linking to python, typically | 
					
						
							|  |  |  |  * this would be done via RNA api but in this case a hand written python api | 
					
						
							| 
									
										
										
										
											2019-06-21 09:50:23 +10:00
										 |  |  |  * allows us to use Python's context manager (`__enter__` and `__exit__`). | 
					
						
							| 
									
										
										
										
											2011-11-05 08:21:12 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-06-21 09:50:23 +10:00
										 |  |  |  * Everything here is exposed via `bpy.data.libraries.load(...)` which returns | 
					
						
							| 
									
										
										
										
											2011-11-05 08:40:07 +00:00
										 |  |  |  * a context manager. | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:39:31 +01:00
										 |  |  | #include "BLI_ghash.h"
 | 
					
						
							| 
									
										
										
										
											2012-07-08 06:00:27 +00:00
										 |  |  | #include "BLI_linklist.h"
 | 
					
						
							|  |  |  | #include "BLI_path_util.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "BLI_string.h"
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2012-07-08 06:00:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  | #include "BKE_blendfile_link_append.h"
 | 
					
						
							| 
									
										
										
										
											2021-03-09 01:01:31 +11:00
										 |  |  | #include "BKE_context.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:37:00 +01:00
										 |  |  | #include "BKE_idtype.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-10 12:58:59 +01:00
										 |  |  | #include "BKE_lib_id.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-07 18:00:24 +01:00
										 |  |  | #include "BKE_main.h"
 | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | #include "BKE_report.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "DNA_space_types.h" /* FILE_LINK, FILE_RELPATH */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 17:00:10 +11:00
										 |  |  | #include "BLO_readfile.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-08 12:56:11 +01:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 21:11:29 +11:00
										 |  |  | #include "bpy_capi_utils.h"
 | 
					
						
							| 
									
										
										
										
											2012-09-15 01:52:28 +00:00
										 |  |  | #include "bpy_library.h"
 | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-04 18:34:20 +10:00
										 |  |  | #include "../generic/py_capi_utils.h"
 | 
					
						
							| 
									
										
										
										
											2015-01-06 16:42:22 +11:00
										 |  |  | #include "../generic/python_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-05 17:56:54 +00:00
										 |  |  | /* nifty feature. swap out strings for RNA data */ | 
					
						
							|  |  |  | #define USE_RNA_DATABLOCKS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 15:21:14 +00:00
										 |  |  | #ifdef USE_RNA_DATABLOCKS
 | 
					
						
							|  |  |  | #  include "RNA_access.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #  include "bpy_rna.h"
 | 
					
						
							| 
									
										
										
										
											2011-05-24 15:21:14 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2021-06-24 15:56:58 +10:00
										 |  |  |   PyObject_HEAD /* Required Python macro. */ | 
					
						
							| 
									
										
										
										
											2021-06-24 17:10:22 +10:00
										 |  |  |   /* Collection iterator specific parts. */ | 
					
						
							|  |  |  |   char relpath[FILE_MAX]; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   char abspath[FILE_MAX]; /* absolute path */ | 
					
						
							|  |  |  |   BlendHandle *blo_handle; | 
					
						
							| 
									
										
										
										
											2021-07-15 15:44:33 +02:00
										 |  |  |   /* Referenced by `blo_handle`, so stored here to keep alive for long enough. */ | 
					
						
							| 
									
										
										
										
											2021-11-11 14:29:14 +01:00
										 |  |  |   ReportList reports; | 
					
						
							| 
									
										
										
										
											2021-07-15 15:44:33 +02:00
										 |  |  |   BlendFileReadReport bf_reports; | 
					
						
							| 
									
										
										
										
											2021-11-11 14:29:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   int flag; | 
					
						
							|  |  |  |   PyObject *dict; | 
					
						
							| 
									
										
										
										
											2021-03-09 01:01:31 +11:00
										 |  |  |   /* Borrowed reference to the `bmain`, taken from the RNA instance of #RNA_BlendDataLibraries.
 | 
					
						
							|  |  |  |    * Defaults to #G.main, Otherwise use a temporary #Main when `bmain_is_temp` is true. */ | 
					
						
							| 
									
										
										
										
											2021-03-04 23:13:07 +11:00
										 |  |  |   Main *bmain; | 
					
						
							| 
									
										
										
										
											2021-03-09 01:01:31 +11:00
										 |  |  |   bool bmain_is_temp; | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | } BPy_Library; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 23:13:07 +11:00
										 |  |  | static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kwds); | 
					
						
							| 
									
										
										
										
											2017-08-20 19:04:16 +10:00
										 |  |  | static PyObject *bpy_lib_enter(BPy_Library *self); | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *args); | 
					
						
							| 
									
										
										
										
											2011-03-14 01:00:41 +00:00
										 |  |  | static PyObject *bpy_lib_dir(BPy_Library *self); | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyMethodDef bpy_lib_methods[] = { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     {"__enter__", (PyCFunction)bpy_lib_enter, METH_NOARGS}, | 
					
						
							|  |  |  |     {"__exit__", (PyCFunction)bpy_lib_exit, METH_VARARGS}, | 
					
						
							|  |  |  |     {"__dir__", (PyCFunction)bpy_lib_dir, METH_NOARGS}, | 
					
						
							|  |  |  |     {NULL} /* sentinel */ | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void bpy_lib_dealloc(BPy_Library *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   Py_XDECREF(self->dict); | 
					
						
							|  |  |  |   Py_TYPE(self)->tp_free(self); | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static PyTypeObject bpy_lib_Type = { | 
					
						
							| 
									
										
										
										
											2022-11-07 22:34:35 +11:00
										 |  |  |     PyVarObject_HEAD_INIT(NULL, 0) | 
					
						
							|  |  |  |     /*tp_name*/ "bpy_lib", | 
					
						
							|  |  |  |     /*tp_basicsize*/ sizeof(BPy_Library), | 
					
						
							|  |  |  |     /*tp_itemsize*/ 0, | 
					
						
							|  |  |  |     /*tp_dealloc*/ (destructor)bpy_lib_dealloc, | 
					
						
							|  |  |  |     /*tp_vectorcall_offset*/ 0, | 
					
						
							|  |  |  |     /*tp_getattr*/ NULL, | 
					
						
							|  |  |  |     /*tp_setattr*/ NULL, | 
					
						
							|  |  |  |     /*tp_as_async*/ NULL, | 
					
						
							|  |  |  |     /*tp_repr*/ NULL, | 
					
						
							|  |  |  |     /*tp_as_number*/ NULL, | 
					
						
							|  |  |  |     /*tp_as_sequence*/ NULL, | 
					
						
							|  |  |  |     /*tp_as_mapping*/ NULL, | 
					
						
							|  |  |  |     /*tp_hash*/ NULL, | 
					
						
							|  |  |  |     /*tp_call*/ NULL, | 
					
						
							|  |  |  |     /*tp_str*/ NULL, | 
					
						
							|  |  |  |     /*tp_getattro*/ PyObject_GenericGetAttr, | 
					
						
							|  |  |  |     /*tp_setattro*/ NULL, | 
					
						
							|  |  |  |     /*tp_as_buffer*/ NULL, | 
					
						
							|  |  |  |     /*tp_flags*/ Py_TPFLAGS_DEFAULT, | 
					
						
							|  |  |  |     /*tp_doc*/ NULL, | 
					
						
							|  |  |  |     /*tp_traverse*/ NULL, | 
					
						
							|  |  |  |     /*tp_clear*/ NULL, | 
					
						
							|  |  |  |     /*tp_richcompare*/ NULL, | 
					
						
							|  |  |  |     /*tp_weaklistoffset*/ 0, | 
					
						
							|  |  |  |     /*tp_iter*/ NULL, | 
					
						
							|  |  |  |     /*tp_iternext*/ NULL, | 
					
						
							|  |  |  |     /*tp_methods*/ bpy_lib_methods, | 
					
						
							|  |  |  |     /*tp_members*/ NULL, | 
					
						
							|  |  |  |     /*tp_getset*/ NULL, | 
					
						
							|  |  |  |     /*tp_base*/ NULL, | 
					
						
							|  |  |  |     /*tp_dict*/ NULL, | 
					
						
							|  |  |  |     /*tp_descr_get*/ NULL, | 
					
						
							|  |  |  |     /*tp_descr_set*/ NULL, | 
					
						
							|  |  |  |     /*tp_dictoffset*/ offsetof(BPy_Library, dict), | 
					
						
							|  |  |  |     /*tp_init*/ NULL, | 
					
						
							|  |  |  |     /*tp_alloc*/ NULL, | 
					
						
							|  |  |  |     /*tp_new*/ NULL, | 
					
						
							|  |  |  |     /*tp_free*/ NULL, | 
					
						
							|  |  |  |     /*tp_is_gc*/ NULL, | 
					
						
							|  |  |  |     /*tp_bases*/ NULL, | 
					
						
							|  |  |  |     /*tp_mro*/ NULL, | 
					
						
							|  |  |  |     /*tp_cache*/ NULL, | 
					
						
							|  |  |  |     /*tp_subclasses*/ NULL, | 
					
						
							|  |  |  |     /*tp_weaklist*/ NULL, | 
					
						
							|  |  |  |     /*tp_del*/ NULL, | 
					
						
							|  |  |  |     /*tp_version_tag*/ 0, | 
					
						
							|  |  |  |     /*tp_finalize*/ NULL, | 
					
						
							|  |  |  |     /*tp_vectorcall*/ NULL, | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | PyDoc_STRVAR( | 
					
						
							|  |  |  |     bpy_lib_load_doc, | 
					
						
							| 
									
										
										
										
											2021-02-04 13:04:54 +11:00
										 |  |  |     ".. method:: load(filepath, link=False, relative=False, assets_only=False)\n" | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     "\n" | 
					
						
							|  |  |  |     "   Returns a context manager which exposes 2 library objects on entering.\n" | 
					
						
							|  |  |  |     "   Each object has attributes matching bpy.data which are lists of strings to be linked.\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "   :arg filepath: The path to a blend file.\n" | 
					
						
							|  |  |  |     "   :type filepath: string\n" | 
					
						
							|  |  |  |     "   :arg link: When False reference to the original file is lost.\n" | 
					
						
							|  |  |  |     "   :type link: bool\n" | 
					
						
							|  |  |  |     "   :arg relative: When True the path is stored relative to the open blend file.\n" | 
					
						
							| 
									
										
										
										
											2021-02-04 13:04:54 +11:00
										 |  |  |     "   :type relative: bool\n" | 
					
						
							|  |  |  |     "   :arg assets_only: If True, only list data-blocks marked as assets.\n" | 
					
						
							|  |  |  |     "   :type assets_only: bool\n"); | 
					
						
							| 
									
										
										
										
											2021-03-04 23:13:07 +11:00
										 |  |  | static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kw) | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-09 01:01:31 +11:00
										 |  |  |   Main *bmain_base = CTX_data_main(BPY_context_get()); | 
					
						
							| 
									
										
										
										
											2021-03-04 23:13:07 +11:00
										 |  |  |   Main *bmain = self->ptr.data; /* Typically #G_MAIN */ | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   BPy_Library *ret; | 
					
						
							| 
									
										
										
										
											2022-05-17 11:38:05 +10:00
										 |  |  |   const char *filepath = NULL; | 
					
						
							| 
									
										
										
										
											2021-01-28 18:04:10 +01:00
										 |  |  |   bool is_rel = false, is_link = false, use_assets_only = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static const char *_keywords[] = {"filepath", "link", "relative", "assets_only", NULL}; | 
					
						
							| 
									
										
										
										
											2022-04-08 09:41:28 +10:00
										 |  |  |   static _PyArg_Parser _parser = { | 
					
						
							|  |  |  |       "s" /* `filepath` */ | 
					
						
							|  |  |  |       /* Optional keyword only arguments. */ | 
					
						
							|  |  |  |       "|$" | 
					
						
							|  |  |  |       "O&" /* `link` */ | 
					
						
							|  |  |  |       "O&" /* `relative` */ | 
					
						
							|  |  |  |       "O&" /* `assets_only` */ | 
					
						
							|  |  |  |       ":load", | 
					
						
							|  |  |  |       _keywords, | 
					
						
							|  |  |  |       0, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2021-01-28 18:04:10 +01:00
										 |  |  |   if (!_PyArg_ParseTupleAndKeywordsFast(args, | 
					
						
							|  |  |  |                                         kw, | 
					
						
							|  |  |  |                                         &_parser, | 
					
						
							| 
									
										
										
										
											2022-05-17 11:38:05 +10:00
										 |  |  |                                         &filepath, | 
					
						
							| 
									
										
										
										
											2021-01-28 18:04:10 +01:00
										 |  |  |                                         PyC_ParseBool, | 
					
						
							|  |  |  |                                         &is_link, | 
					
						
							|  |  |  |                                         PyC_ParseBool, | 
					
						
							|  |  |  |                                         &is_rel, | 
					
						
							|  |  |  |                                         PyC_ParseBool, | 
					
						
							|  |  |  |                                         &use_assets_only)) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ret = PyObject_New(BPy_Library, &bpy_lib_Type); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-17 11:38:05 +10:00
										 |  |  |   BLI_strncpy(ret->relpath, filepath, sizeof(ret->relpath)); | 
					
						
							|  |  |  |   BLI_strncpy(ret->abspath, filepath, sizeof(ret->abspath)); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   BLI_path_abs(ret->abspath, BKE_main_blendfile_path(bmain)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 23:13:07 +11:00
										 |  |  |   ret->bmain = bmain; | 
					
						
							| 
									
										
										
										
											2021-03-09 01:01:31 +11:00
										 |  |  |   ret->bmain_is_temp = (bmain != bmain_base); | 
					
						
							| 
									
										
										
										
											2021-03-04 23:13:07 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   ret->blo_handle = NULL; | 
					
						
							| 
									
										
										
										
											2021-01-28 18:04:10 +01:00
										 |  |  |   ret->flag = ((is_link ? FILE_LINK : 0) | (is_rel ? FILE_RELPATH : 0) | | 
					
						
							|  |  |  |                (use_assets_only ? FILE_ASSETS_ONLY : 0)); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 18:39:07 +01:00
										 |  |  |   ret->dict = _PyDict_NewPresized(INDEX_ID_MAX); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return (PyObject *)ret; | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject *_bpy_names(BPy_Library *self, int blocktype) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   PyObject *list; | 
					
						
							|  |  |  |   LinkNode *l, *names; | 
					
						
							|  |  |  |   int totnames; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-28 18:04:10 +01:00
										 |  |  |   names = BLO_blendhandle_get_datablock_names( | 
					
						
							|  |  |  |       self->blo_handle, blocktype, (self->flag & FILE_ASSETS_ONLY) != 0, &totnames); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   list = PyList_New(totnames); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (names) { | 
					
						
							|  |  |  |     int counter = 0; | 
					
						
							|  |  |  |     for (l = names; l; l = l->next) { | 
					
						
							|  |  |  |       PyList_SET_ITEM(list, counter, PyUnicode_FromString((char *)l->link)); | 
					
						
							|  |  |  |       counter++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-08 15:00:31 +01:00
										 |  |  |     BLI_linklist_freeN(names); /* free linklist *and* each node's data */ | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return list; | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-20 19:04:16 +10:00
										 |  |  | static PyObject *bpy_lib_enter(BPy_Library *self) | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   PyObject *ret; | 
					
						
							|  |  |  |   BPy_Library *self_from; | 
					
						
							| 
									
										
										
										
											2021-03-04 18:39:07 +01:00
										 |  |  |   PyObject *from_dict = _PyDict_NewPresized(INDEX_ID_MAX); | 
					
						
							| 
									
										
										
										
											2021-11-11 14:29:14 +01:00
										 |  |  |   ReportList *reports = &self->reports; | 
					
						
							|  |  |  |   BlendFileReadReport *bf_reports = &self->bf_reports; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 14:29:14 +01:00
										 |  |  |   BKE_reports_init(reports, RPT_STORE); | 
					
						
							|  |  |  |   memset(bf_reports, 0, sizeof(*bf_reports)); | 
					
						
							|  |  |  |   bf_reports->reports = reports; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 14:29:14 +01:00
										 |  |  |   self->blo_handle = BLO_blendhandle_from_file(self->abspath, bf_reports); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (self->blo_handle == NULL) { | 
					
						
							| 
									
										
										
										
											2021-11-11 14:29:14 +01:00
										 |  |  |     if (BPy_reports_to_error(reports, PyExc_IOError, true) != -1) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |       PyErr_Format(PyExc_IOError, "load: %s failed to open blend file", self->abspath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-07 12:41:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   int i = 0, code; | 
					
						
							|  |  |  |   while ((code = BKE_idtype_idcode_iter_step(&i))) { | 
					
						
							|  |  |  |     if (BKE_idtype_idcode_is_linkable(code)) { | 
					
						
							|  |  |  |       const char *name_plural = BKE_idtype_idcode_to_name_plural(code); | 
					
						
							|  |  |  |       PyObject *str = PyUnicode_FromString(name_plural); | 
					
						
							|  |  |  |       PyObject *item; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       PyDict_SetItem(self->dict, str, item = PyList_New(0)); | 
					
						
							|  |  |  |       Py_DECREF(item); | 
					
						
							|  |  |  |       PyDict_SetItem(from_dict, str, item = _bpy_names(self, code)); | 
					
						
							|  |  |  |       Py_DECREF(item); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Py_DECREF(str); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* create a dummy */ | 
					
						
							|  |  |  |   self_from = PyObject_New(BPy_Library, &bpy_lib_Type); | 
					
						
							|  |  |  |   BLI_strncpy(self_from->relpath, self->relpath, sizeof(self_from->relpath)); | 
					
						
							|  |  |  |   BLI_strncpy(self_from->abspath, self->abspath, sizeof(self_from->abspath)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   self_from->blo_handle = NULL; | 
					
						
							|  |  |  |   self_from->flag = 0; | 
					
						
							|  |  |  |   self_from->dict = from_dict; /* owns the dict */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* return pair */ | 
					
						
							|  |  |  |   ret = PyTuple_New(2); | 
					
						
							|  |  |  |   PyTuple_SET_ITEMS(ret, (PyObject *)self_from, (PyObject *)self); | 
					
						
							|  |  |  |   Py_INCREF(self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 14:29:14 +01:00
										 |  |  |   BKE_reports_clear(reports); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return ret; | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | static void bpy_lib_exit_warn_idname(BPy_Library *self, | 
					
						
							|  |  |  |                                      const char *name_plural, | 
					
						
							|  |  |  |                                      const char *idname) | 
					
						
							| 
									
										
										
										
											2011-05-24 15:21:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   PyObject *exc, *val, *tb; | 
					
						
							|  |  |  |   PyErr_Fetch(&exc, &val, &tb); | 
					
						
							|  |  |  |   if (PyErr_WarnFormat(PyExc_UserWarning, | 
					
						
							|  |  |  |                        1, | 
					
						
							|  |  |  |                        "load: '%s' does not contain %s[\"%s\"]", | 
					
						
							|  |  |  |                        self->abspath, | 
					
						
							|  |  |  |                        name_plural, | 
					
						
							|  |  |  |                        idname)) { | 
					
						
							|  |  |  |     /* Spurious errors can appear at shutdown */ | 
					
						
							|  |  |  |     if (PyErr_ExceptionMatches(PyExc_Warning)) { | 
					
						
							|  |  |  |       PyErr_WriteUnraisable((PyObject *)self); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   PyErr_Restore(exc, val, tb); | 
					
						
							| 
									
										
										
										
											2011-05-24 15:21:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   PyObject *exc, *val, *tb; | 
					
						
							|  |  |  |   PyErr_Fetch(&exc, &val, &tb); | 
					
						
							|  |  |  |   if (PyErr_WarnFormat(PyExc_UserWarning, | 
					
						
							|  |  |  |                        1, | 
					
						
							|  |  |  |                        "load: '%s' expected a string type, not a %.200s", | 
					
						
							|  |  |  |                        self->abspath, | 
					
						
							|  |  |  |                        Py_TYPE(item)->tp_name)) { | 
					
						
							|  |  |  |     /* Spurious errors can appear at shutdown */ | 
					
						
							|  |  |  |     if (PyErr_ExceptionMatches(PyExc_Warning)) { | 
					
						
							|  |  |  |       PyErr_WriteUnraisable((PyObject *)self); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   PyErr_Restore(exc, val, tb); | 
					
						
							| 
									
										
										
										
											2011-05-24 15:21:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  | struct LibExitLappContextItemsIterData { | 
					
						
							|  |  |  |   short idcode; | 
					
						
							|  |  |  |   BPy_Library *py_library; | 
					
						
							|  |  |  |   PyObject *py_list; | 
					
						
							|  |  |  |   Py_ssize_t py_list_size; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool bpy_lib_exit_lapp_context_items_cb(BlendfileLinkAppendContext *lapp_context, | 
					
						
							|  |  |  |                                                BlendfileLinkAppendContextItem *item, | 
					
						
							|  |  |  |                                                void *userdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   struct LibExitLappContextItemsIterData *data = userdata; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Since `bpy_lib_exit` loops over all ID types, all items in `lapp_context` end up being looped
 | 
					
						
							|  |  |  |    * over for each ID type, so when it does not match the item can simply be skipped: it either has | 
					
						
							|  |  |  |    * already been processed, or will be processed in a later loop. */ | 
					
						
							|  |  |  |   if (BKE_blendfile_link_append_context_item_idcode_get(lapp_context, item) != data->idcode) { | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const int py_list_index = POINTER_AS_INT( | 
					
						
							|  |  |  |       BKE_blendfile_link_append_context_item_userdata_get(lapp_context, item)); | 
					
						
							|  |  |  |   ID *new_id = BKE_blendfile_link_append_context_item_newid_get(lapp_context, item); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BLI_assert(py_list_index < data->py_list_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Fully invalid items (which got set to `Py_None` already in first loop of `bpy_lib_exit`)
 | 
					
						
							|  |  |  |    * should never be accessed here, since their index should never be set to any item in | 
					
						
							|  |  |  |    * `lapp_context`. */ | 
					
						
							|  |  |  |   PyObject *item_src = PyList_GET_ITEM(data->py_list, py_list_index); | 
					
						
							|  |  |  |   BLI_assert(item_src != Py_None); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   PyObject *py_item; | 
					
						
							|  |  |  |   if (new_id != NULL) { | 
					
						
							|  |  |  |     PointerRNA newid_ptr; | 
					
						
							|  |  |  |     RNA_id_pointer_create(new_id, &newid_ptr); | 
					
						
							|  |  |  |     py_item = pyrna_struct_CreatePyObject(&newid_ptr); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     const char *item_idname = PyUnicode_AsUTF8(item_src); | 
					
						
							|  |  |  |     const char *idcode_name_plural = BKE_idtype_idcode_to_name_plural(data->idcode); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bpy_lib_exit_warn_idname(data->py_library, idcode_name_plural, item_idname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     py_item = Py_INCREF_RET(Py_None); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   PyList_SET_ITEM(data->py_list, py_list_index, py_item); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Py_DECREF(item_src); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-04 23:13:07 +11:00
										 |  |  |   Main *bmain = self->bmain; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   const bool do_append = ((self->flag & FILE_LINK) == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* here appending/linking starts */ | 
					
						
							| 
									
										
										
										
											2021-03-09 01:01:31 +11:00
										 |  |  |   const int id_tag_extra = self->bmain_is_temp ? LIB_TAG_TEMP_MAIN : 0; | 
					
						
							| 
									
										
										
										
											2020-09-08 15:32:43 +10:00
										 |  |  |   struct LibraryLink_Params liblink_params; | 
					
						
							| 
									
										
										
										
											2021-03-09 01:01:31 +11:00
										 |  |  |   BLO_library_link_params_init(&liblink_params, bmain, self->flag, id_tag_extra); | 
					
						
							| 
									
										
										
										
											2020-09-08 15:32:43 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |   BlendfileLinkAppendContext *lapp_context = BKE_blendfile_link_append_context_new( | 
					
						
							|  |  |  |       &liblink_params); | 
					
						
							|  |  |  |   BKE_blendfile_link_append_context_library_add(lapp_context, self->abspath, self->blo_handle); | 
					
						
							| 
									
										
										
										
											2021-03-09 01:01:31 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |   int idcode_step = 0; | 
					
						
							|  |  |  |   short idcode; | 
					
						
							|  |  |  |   while ((idcode = BKE_idtype_idcode_iter_step(&idcode_step))) { | 
					
						
							|  |  |  |     if (!BKE_idtype_idcode_is_linkable(idcode) || (idcode == ID_WS && !do_append)) { | 
					
						
							|  |  |  |       continue; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |     const char *name_plural = BKE_idtype_idcode_to_name_plural(idcode); | 
					
						
							|  |  |  |     PyObject *ls = PyDict_GetItemString(self->dict, name_plural); | 
					
						
							|  |  |  |     // printf("lib: %s\n", name_plural);
 | 
					
						
							|  |  |  |     if (ls == NULL || !PyList_Check(ls)) { | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |     const Py_ssize_t size = PyList_GET_SIZE(ls); | 
					
						
							|  |  |  |     if (size == 0) { | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |     /* loop */ | 
					
						
							|  |  |  |     for (Py_ssize_t i = 0; i < size; i++) { | 
					
						
							|  |  |  |       PyObject *item_src = PyList_GET_ITEM(ls, i); | 
					
						
							|  |  |  |       const char *item_idname = PyUnicode_AsUTF8(item_src); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |       // printf("  %s\n", item_idname);
 | 
					
						
							| 
									
										
										
										
											2020-08-07 12:41:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |       /* NOTE: index of item in py list is stored in userdata pointer, so that it can be found
 | 
					
						
							|  |  |  |        * later on to replace the ID name by the actual ID pointer. */ | 
					
						
							|  |  |  |       if (item_idname != NULL) { | 
					
						
							|  |  |  |         BlendfileLinkAppendContextItem *item = BKE_blendfile_link_append_context_item_add( | 
					
						
							|  |  |  |             lapp_context, item_idname, idcode, POINTER_FROM_INT(i)); | 
					
						
							|  |  |  |         BKE_blendfile_link_append_context_item_library_index_enable(lapp_context, item, 0); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         /* XXX, could complain about this */ | 
					
						
							|  |  |  |         bpy_lib_exit_warn_type(self, item_src); | 
					
						
							|  |  |  |         PyErr_Clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef USE_RNA_DATABLOCKS
 | 
					
						
							|  |  |  |         /* We can replace the item immediately with `None`. */ | 
					
						
							|  |  |  |         PyObject *py_item = Py_INCREF_RET(Py_None); | 
					
						
							|  |  |  |         PyList_SET_ITEM(ls, i, py_item); | 
					
						
							|  |  |  |         Py_DECREF(item_src); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-07 12:41:06 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |   BKE_blendfile_link(lapp_context, NULL); | 
					
						
							|  |  |  |   if (do_append) { | 
					
						
							|  |  |  |     BKE_blendfile_append(lapp_context, NULL); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |   /* If enabled, replace named items in given lists by the final matching new ID pointer. */ | 
					
						
							| 
									
										
										
										
											2016-07-14 21:00:05 +10:00
										 |  |  | #ifdef USE_RNA_DATABLOCKS
 | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  |   idcode_step = 0; | 
					
						
							|  |  |  |   while ((idcode = BKE_idtype_idcode_iter_step(&idcode_step))) { | 
					
						
							|  |  |  |     if (!BKE_idtype_idcode_is_linkable(idcode) || (idcode == ID_WS && !do_append)) { | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const char *name_plural = BKE_idtype_idcode_to_name_plural(idcode); | 
					
						
							|  |  |  |     PyObject *ls = PyDict_GetItemString(self->dict, name_plural); | 
					
						
							|  |  |  |     // printf("lib: %s\n", name_plural);
 | 
					
						
							|  |  |  |     if (ls == NULL || !PyList_Check(ls)) { | 
					
						
							|  |  |  |       continue; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const Py_ssize_t size = PyList_GET_SIZE(ls); | 
					
						
							|  |  |  |     if (size == 0) { | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Loop over linked items in `lapp_context` to find matching python one in the list, and
 | 
					
						
							|  |  |  |      * replace them with proper ID pointer. */ | 
					
						
							|  |  |  |     struct LibExitLappContextItemsIterData iter_data = { | 
					
						
							|  |  |  |         .idcode = idcode, .py_library = self, .py_list = ls, .py_list_size = size}; | 
					
						
							|  |  |  |     BKE_blendfile_link_append_context_item_foreach( | 
					
						
							|  |  |  |         lapp_context, | 
					
						
							|  |  |  |         bpy_lib_exit_lapp_context_items_cb, | 
					
						
							|  |  |  |         BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_DIRECT, | 
					
						
							|  |  |  |         &iter_data); | 
					
						
							| 
									
										
										
										
											2020-08-07 12:41:06 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-23 12:12:28 +01:00
										 |  |  | #endif  // USE_RNA_DATABLOCKS
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BLO_blendhandle_close(self->blo_handle); | 
					
						
							|  |  |  |   self->blo_handle = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BKE_blendfile_link_append_context_free(lapp_context); | 
					
						
							|  |  |  |   BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 12:41:06 +02:00
										 |  |  |   Py_RETURN_NONE; | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:00:41 +00:00
										 |  |  | static PyObject *bpy_lib_dir(BPy_Library *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   return PyDict_Keys(self->dict); | 
					
						
							| 
									
										
										
										
											2011-03-14 01:00:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 14:50:29 +10:00
										 |  |  | PyMethodDef BPY_library_load_method_def = { | 
					
						
							|  |  |  |     "load", | 
					
						
							|  |  |  |     (PyCFunction)bpy_lib_load, | 
					
						
							| 
									
										
										
										
											2021-03-04 23:13:07 +11:00
										 |  |  |     METH_VARARGS | METH_KEYWORDS, | 
					
						
							| 
									
										
										
										
											2020-05-29 14:50:29 +10:00
										 |  |  |     bpy_lib_load_doc, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int BPY_library_load_type_ready(void) | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (PyType_Ready(&bpy_lib_Type) < 0) { | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							| 
									
										
										
											
												library loading api.
this is not well suited to RNA so this is a native python api.
This uses:
  bpy.data.libraries.load(filepath, link=False, relative=False)
however the return value needs to use pythons context manager, this means the library loading is confined to a block of code and python cant leave a half loaded library state.
eg, load a single scene we know the name of:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = ["Scene"]
eg, load all scenes:
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.scenes = data_from.scenes
eg, load all objects starting with 'A'
  with bpy.data.libraries.load(filepath) as (data_from, data_to):
      data_to.objects = [name for name in data_from.objects if name.startswith("A")]
As you can see gives 2 objects like 'bpy.data', but containing lists of strings which can be moved from one into another.
											
										 
											2011-03-12 16:06:37 +00:00
										 |  |  | } |