diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2016-01-06 21:34:42 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2016-01-06 21:48:10 +0300 |
commit | 4acf0f05a1ec0b96c4a2e9c3628190f52a3590e2 (patch) | |
tree | 6c58b90f9133ce30d3e7b125181c5c267885b698 /source/blender/blenkernel/intern/library_query.c | |
parent | ea7a2766f6054bf991dee746408f45c535c324b5 (diff) |
'users of ID' py API.
This mainly adds bpy.data.user_map() method, which goes over the whole Main database
to build a mapping (dict) {ID: {users_of_that_ID}}.
Very handy to check and debug ID usages, but could also be really valuable for py addons
creating temporary scenes, or some exporters, etc.
Note: current code in master's libquery misses some IDs (and reports some it should not,
like nodetrees), this is fixed in id-remap but still needs serious review before going to master.
This basically means that current bpy.data.user_map() **will not** report a complete and exhaustive
state of dependencies between IDs. Should work OK in most cases though.
Original work/idea comes from id-remap branch, was heavily reworked by @campbellbarton
and myself for master.
Reviewers: campbellbarton, sergey
Differential Revision: https://developer.blender.org/D1678
Diffstat (limited to 'source/blender/blenkernel/intern/library_query.c')
-rw-r--r-- | source/blender/blenkernel/intern/library_query.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/source/blender/blenkernel/intern/library_query.c b/source/blender/blenkernel/intern/library_query.c index d54f382f4c0..7936aab6700 100644 --- a/source/blender/blenkernel/intern/library_query.c +++ b/source/blender/blenkernel/intern/library_query.c @@ -665,3 +665,49 @@ void BKE_library_update_ID_link_user(ID *id_dst, ID *id_src, const int cd_flag) id_us_ensure_real(id_dst); } } + +/* ***** ID users iterator. ***** */ +typedef struct IDUsersIter { + ID *id; + + ListBase *lb_array[MAX_LIBARRAY]; + int lb_idx; + + ID *curr_id; + int count; /* Set by callback. */ +} IDUsersIter; + +static bool foreach_libblock_id_users_callback(void *user_data, ID **id_p, int UNUSED(cb_flag)) +{ + IDUsersIter *iter = user_data; + + if (*id_p && (*id_p == iter->id)) { + iter->count++; + } + + return true; +} + +/** + * Return the number of times given \a id_user uses/references \a id_used. + * + * \note This only checks for pointer references of an ID, shallow usages (like e.g. by RNA paths, as done + * for FCurves) are not detected at all. + * + * \param id_user the ID which is supposed to use (reference) \a id_used. + * \param id_used the ID which is supposed to be used (referenced) by \a id_user. + * \return the number of direct usages/references of \a id_used by \a id_user. + */ +int BKE_library_ID_use_ID(ID *id_user, ID *id_used) +{ + IDUsersIter iter; + + /* We do not care about iter.lb_array/lb_idx here... */ + iter.id = id_used; + iter.curr_id = id_user; + iter.count = 0; + + BKE_library_foreach_ID_link(iter.curr_id, foreach_libblock_id_users_callback, (void *)&iter, IDWALK_NOP); + + return iter.count; +} |