Collections: API to get collection from index

This commit is contained in:
Dalai Felinto 2018-03-29 17:28:05 -03:00
parent 1251043636
commit 156b548208
3 changed files with 34 additions and 28 deletions

View File

@ -59,6 +59,7 @@ bool BKE_collection_object_remove(struct Main *bmain, struct ID *owner_id, struc
bool BKE_collections_object_remove(struct Main *bmain, struct ID *owner_id, struct Object *object, const bool free_us);
void BKE_collection_object_move(struct ID *owner_id, struct SceneCollection *sc_dst, struct SceneCollection *sc_src, struct Object *ob);
bool BKE_collection_object_exists(struct SceneCollection *scene_collection, struct Object *ob);
struct SceneCollection *BKE_collection_from_index(struct Scene *scene, const int index);
struct Group *BKE_collection_group_create(struct Main *bmain, struct Scene *scene, struct LayerCollection *lc);

View File

@ -523,6 +523,38 @@ bool BKE_collection_object_exists(struct SceneCollection *scene_collection, stru
return false;
}
static SceneCollection *scene_collection_from_index_recursive(SceneCollection *scene_collection, const int index, int *index_current)
{
if (index == (*index_current)) {
return scene_collection;
}
(*index_current)++;
for (SceneCollection *scene_collection_iter = scene_collection->scene_collections.first;
scene_collection_iter != NULL;
scene_collection_iter = scene_collection_iter->next)
{
SceneCollection *nested = scene_collection_from_index_recursive(scene_collection_iter, index, index_current);
if (nested != NULL) {
return nested;
}
}
return NULL;
}
/**
* Return Scene Collection for a given index.
*
* The index is calculated from top to bottom counting the children before the siblings.
*/
SceneCollection *BKE_collection_from_index(Scene *scene, const int index)
{
int index_current = 0;
SceneCollection *master_collection = BKE_collection_master(&scene->id);
return scene_collection_from_index_recursive(master_collection, index, &index_current);
}
static void layer_collection_sync(LayerCollection *lc_dst, LayerCollection *lc_src)
{
lc_dst->flag = lc_src->flag;

View File

@ -2048,33 +2048,6 @@ bool ED_object_editmode_calc_active_center(Object *obedit, const bool select_onl
#define COLLECTION_INVALID_INDEX -1
static SceneCollection *scene_collection_from_index_recursive(SceneCollection *scene_collection, const int index, int *index_current)
{
if (index == (*index_current)) {
return scene_collection;
}
(*index_current)++;
for (SceneCollection *scene_collection_iter = scene_collection->scene_collections.first;
scene_collection_iter != NULL;
scene_collection_iter = scene_collection_iter->next)
{
SceneCollection *nested = scene_collection_from_index_recursive(scene_collection_iter, index, index_current);
if (nested != NULL) {
return nested;
}
}
return NULL;
}
static SceneCollection *scene_collection_from_index(Scene *scene, const int index)
{
int index_current = 0;
SceneCollection *master_collection = BKE_collection_master(&scene->id);
return scene_collection_from_index_recursive(master_collection, index, &index_current);
}
static int move_to_collection_exec(bContext *C, wmOperator *op)
{
Scene *scene = CTX_data_scene(C);
@ -2089,7 +2062,7 @@ static int move_to_collection_exec(bContext *C, wmOperator *op)
}
int collection_index = RNA_property_int_get(op->ptr, prop);
scene_collection = scene_collection_from_index(CTX_data_scene(C), collection_index);
scene_collection = BKE_collection_from_index(CTX_data_scene(C), collection_index);
if (scene_collection == NULL) {
BKE_report(op->reports, RPT_ERROR, "Unexpected error, collection not found");
return OPERATOR_CANCELLED;