Commit: d8bdd7b261d2ac6936e16fd04845d0e491931a87
Author: Brecht Van Lommel
Date:   Thu Apr 12 18:43:43 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBd8bdd7b261d2ac6936e16fd04845d0e491931a87

Cleanup: add utility function to find a single point cache ID.

===================================================================

M       source/blender/blenkernel/BKE_pointcache.h
M       source/blender/blenkernel/intern/pointcache.c
M       source/blender/editors/physics/physics_pointcache.c
M       source/blender/makesrna/intern/rna_object_force.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_pointcache.h 
b/source/blender/blenkernel/BKE_pointcache.h
index 4a3dd72836a..89f1aa5eadd 100644
--- a/source/blender/blenkernel/BKE_pointcache.h
+++ b/source/blender/blenkernel/BKE_pointcache.h
@@ -277,6 +277,7 @@ void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct 
Object *ob, struct SmokeMo
 void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, struct Object *ob, 
struct DynamicPaintSurface *surface);
 void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, struct Object *ob, struct 
RigidBodyWorld *rbw);
 
+PTCacheID BKE_ptcache_id_find(struct Object *ob, struct Scene *scene, struct 
PointCache *cache);
 void BKE_ptcache_ids_from_object(struct ListBase *lb, struct Object *ob, 
struct Scene *scene, int duplis);
 
 /***************** Global funcs ****************************/
diff --git a/source/blender/blenkernel/intern/pointcache.c 
b/source/blender/blenkernel/intern/pointcache.c
index c8a2a8b5542..27588afea93 100644
--- a/source/blender/blenkernel/intern/pointcache.c
+++ b/source/blender/blenkernel/intern/pointcache.c
@@ -1646,6 +1646,25 @@ void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, 
Object *ob, RigidBodyWorld *r
        pid->file_type = PTCACHE_FILE_PTCACHE;
 }
 
+PTCacheID BKE_ptcache_id_find(Object *ob, Scene *scene, PointCache *cache)
+{
+       PTCacheID result = {0};
+
+       ListBase pidlist;
+       BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
+
+       for (PTCacheID *pid = pidlist.first; pid; pid = pid->next) {
+               if (pid->cache == cache) {
+                       result = *pid;
+                       break;
+               }
+       }
+
+       BLI_freelistN(&pidlist);
+
+       return result;
+}
+
 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob, Scene *scene, int 
duplis)
 {
        PTCacheID *pid;
diff --git a/source/blender/editors/physics/physics_pointcache.c 
b/source/blender/editors/physics/physics_pointcache.c
index b1d870a43ae..fdafd6c28ed 100644
--- a/source/blender/editors/physics/physics_pointcache.c
+++ b/source/blender/editors/physics/physics_pointcache.c
@@ -177,18 +177,7 @@ static PTCacheBaker *ptcache_baker_create(bContext *C, 
wmOperator *op, bool all)
                PointerRNA ptr = CTX_data_pointer_get_type(C, "point_cache", 
&RNA_PointCache);
                Object *ob = ptr.id.data;
                PointCache *cache = ptr.data;
-
-               ListBase pidlist;
-               BKE_ptcache_ids_from_object(&pidlist, ob, baker->scene, 
MAX_DUPLI_RECUR);
-
-               for (PTCacheID *pid = pidlist.first; pid; pid = pid->next) {
-                       if (pid->cache == cache) {
-                               baker->pid = *pid;
-                               break;
-                       }
-               }
-
-               BLI_freelistN(&pidlist);
+               baker->pid = BKE_ptcache_id_find(ob, baker->scene, cache);
        }
 
        return baker;
@@ -391,22 +380,14 @@ static int ptcache_add_new_exec(bContext *C, wmOperator 
*UNUSED(op))
        PointerRNA ptr= CTX_data_pointer_get_type(C, "point_cache", 
&RNA_PointCache);
        Object *ob= ptr.id.data;
        PointCache *cache= ptr.data;
-       PTCacheID *pid;
-       ListBase pidlist;
+       PTCacheID pid = BKE_ptcache_id_find(ob, scene, cache);
 
-       BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
-       
-       for (pid=pidlist.first; pid; pid=pid->next) {
-               if (pid->cache == cache) {
-                       PointCache *cache_new = BKE_ptcache_add(pid->ptcaches);
-                       cache_new->step = pid->default_step;
-                       *(pid->cache_ptr) = cache_new;
-                       break;
-               }
+       if (pid.cache) {
+               PointCache *cache_new = BKE_ptcache_add(pid.ptcaches);
+               cache_new->step = pid.default_step;
+               *(pid.cache_ptr) = cache_new;
        }
 
-       BLI_freelistN(&pidlist);
-
        WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
        WM_event_add_notifier(C, NC_OBJECT|ND_POINTCACHE, ob);
 
@@ -418,26 +399,15 @@ static int ptcache_remove_exec(bContext *C, wmOperator 
*UNUSED(op))
        Scene *scene= CTX_data_scene(C);
        Object *ob= ptr.id.data;
        PointCache *cache= ptr.data;
-       PTCacheID *pid;
-       ListBase pidlist;
-
-       BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
-       
-       for (pid=pidlist.first; pid; pid=pid->next) {
-               if (pid->cache == cache) {
-                       if (pid->ptcaches->first == pid->ptcaches->last)
-                               continue; /* don't delete last cache */
-
-                       BLI_remlink(pid->ptcaches, pid->cache);
-                       BKE_ptcache_free(pid->cache);
-                       *(pid->cache_ptr) = pid->ptcaches->first;
+       PTCacheID pid = BKE_ptcache_id_find(ob, scene, cache);
 
-                       break;
-               }
+       /* don't delete last cache */
+       if (pid.cache && pid.ptcaches->first != pid.ptcaches->last) {
+               BLI_remlink(pid.ptcaches, pid.cache);
+               BKE_ptcache_free(pid.cache);
+               *(pid.cache_ptr) = pid.ptcaches->first;
        }
 
-       BLI_freelistN(&pidlist);
-       
        WM_event_add_notifier(C, NC_OBJECT|ND_POINTCACHE, ob);
 
        return OPERATOR_FINISHED;
diff --git a/source/blender/makesrna/intern/rna_object_force.c 
b/source/blender/makesrna/intern/rna_object_force.c
index c2797abbc75..7d062d41e4f 100644
--- a/source/blender/makesrna/intern/rna_object_force.c
+++ b/source/blender/makesrna/intern/rna_object_force.c
@@ -108,65 +108,45 @@ static void rna_Cache_change(Main *UNUSED(bmain), Scene 
*UNUSED(scene), PointerR
 {
        Object *ob = (Object *)ptr->id.data;
        PointCache *cache = (PointCache *)ptr->data;
-       PTCacheID *pid = NULL;
-       ListBase pidlist;
 
        if (!ob)
                return;
 
        cache->flag |= PTCACHE_OUTDATED;
 
-       BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
+       PTCacheID pid = BKE_ptcache_id_find(ob, NULL, cache);
 
        DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
 
-       for (pid = pidlist.first; pid; pid = pid->next) {
-               if (pid->cache == cache)
-                       break;
-       }
-
-       if (pid) {
+       if (pid.cache) {
                /* Just make sure this wasn't changed. */
-               if (pid->type == PTCACHE_TYPE_SMOKE_DOMAIN)
+               if (pid.type == PTCACHE_TYPE_SMOKE_DOMAIN)
                        cache->step = 1;
-               BKE_ptcache_update_info(pid);
+               BKE_ptcache_update_info(&pid);
        }
-
-       BLI_freelistN(&pidlist);
 }
 
 static void rna_Cache_toggle_disk_cache(Main *UNUSED(bmain), Scene 
*UNUSED(scene), PointerRNA *ptr)
 {
        Object *ob = (Object *)ptr->id.data;
        PointCache *cache = (PointCache *)ptr->data;
-       PTCacheID *pid = NULL;
-       ListBase pidlist;
 
        if (!ob)
                return;
 
-       BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
-
-       for (pid = pidlist.first; pid; pid = pid->next) {
-               if (pid->cache == cache)
-                       break;
-       }
+       PTCacheID pid = BKE_ptcache_id_find(ob, NULL, cache);
 
        /* smoke can only use disk cache */
-       if (pid && pid->type != PTCACHE_TYPE_SMOKE_DOMAIN)
-               BKE_ptcache_toggle_disk_cache(pid);
+       if (pid.cache && pid.type != PTCACHE_TYPE_SMOKE_DOMAIN)
+               BKE_ptcache_toggle_disk_cache(&pid);
        else
                cache->flag ^= PTCACHE_DISK_CACHE;
-
-       BLI_freelistN(&pidlist);
 }
 
 static void rna_Cache_idname_change(Main *UNUSED(bmain), Scene *UNUSED(scene), 
PointerRNA *ptr)
 {
        Object *ob = (Object *)ptr->id.data;
        PointCache *cache = (PointCache *)ptr->data;
-       PTCacheID *pid = NULL, *pid2 = NULL;
-       ListBase pidlist;
        bool use_new_name = true;
 
        if (!ob)
@@ -174,23 +154,22 @@ static void rna_Cache_idname_change(Main *UNUSED(bmain), 
Scene *UNUSED(scene), P
 
        /* TODO: check for proper characters */
 
-       BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
-
        if (cache->flag & PTCACHE_EXTERNAL) {
-               for (pid = pidlist.first; pid; pid = pid->next) {
-                       if (pid->cache == cache)
-                               break;
-               }
-
-               if (!pid)
-                       return;
+               PTCacheID pid = BKE_ptcache_id_find(ob, NULL, cache);
 
-               BKE_ptcache_load_external(pid);
+               if (pid.cache) {
+                       BKE_ptcache_load_external(&pid);
+               }
 
                DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
                WM_main_add_notifier(NC_OBJECT | ND_POINTCACHE, ob);
        }
        else {
+               PTCacheID *pid = NULL, *pid2 = NULL;
+               ListBase pidlist;
+
+               BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
+
                for (pid = pidlist.first; pid; pid = pid->next) {
                        if (pid->cache == cache)
                                pid2 = pid;
@@ -216,9 +195,9 @@ static void rna_Cache_idname_change(Main *UNUSED(bmain), 
Scene *UNUSED(scene), P
 
                        BLI_strncpy(cache->prev_name, cache->name, 
sizeof(cache->prev_name));
                }
-       }
 
-       BLI_freelistN(&pidlist);
+               BLI_freelistN(&pidlist);
+       }
 }
 
 static void rna_Cache_list_begin(CollectionPropertyIterator *iter, PointerRNA 
*ptr)
@@ -239,43 +218,27 @@ static void 
rna_Cache_active_point_cache_index_range(PointerRNA *ptr, int *min,
 {
        Object *ob = ptr->id.data;
        PointCache *cache = ptr->data;
-       PTCacheID *pid;
-       ListBase pidlist;
-
-       BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
+       PTCacheID pid = BKE_ptcache_id_find(ob, NULL, cache);
        
        *min = 0;
        *max = 0;
 
-       for (pid = pidlist.first; pid; pid = pid->next) {
-               if (pid->cache == cache) {
-                       *max = max_ii(0, BLI_listbase_count(pid->ptcaches) - 1);
-                       break;
-               }
+       if (pid.cache) {
+               *max = max_ii(0, BLI_listbase_count(pid.ptcaches) - 1);
        }
-
-       BLI_freelistN(&pidlist);
 }
 
 static int rna_Cache_active_point_cache_index_get(PointerRNA *ptr)
 {
        Object *ob = ptr->id.data;
        PointCache *cache = ptr->data;
-       PTCacheID *pid;
-       ListBase pidlist;
+       PTCacheID pid = BKE_ptcache_id_find(ob, NULL, cache);
        int num = 0;
 
-       BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
-       
-       for (pid = pidlist.first; pid; pid = pid->next) {
-               if (pid->cache == cache) {
-                       num = BLI_findindex(pid->ptcaches, cache);
-                       break;
-               }
+       if (pid.cache) {
+               num = BLI_findindex(pid.ptcaches, cache);
        }
 
-       BLI_freelistN(&pidlist);
-
        return num;
 }
 
@@ -283,19 +246,11 @@ static void rna_Cache_active_point_cache_index_set(struct 
PointerRNA *ptr, int v
 {
        Object *ob = ptr->id.data;
        PointCache *cache = ptr->data;
-       PTCacheID *pid;
-       ListBase pidlist;
-
-       BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
+       PTCacheID pid = BKE_ptcache_id_find(ob, NULL, cache);
        
-       for (pid = pidlist.first; pid; pid = pid->next) {
-               if (pid->cache == cache) {
-                       *(pid->cache_ptr) = BLI_findlink(pid->ptcaches, value);
-                       break;
-               }
+       if (pid.cache) {
+               *(pid.cache_ptr) = BLI_findlink(pid.ptcaches, value);
        }
-
-       BLI_freelistN(&pidlist);
 }
 
 static void rna_PointCache_frame_step_range(PointerRNA *ptr, int *min, int 
*max,
@@ -303,22 +258,14 @@ static void rna_PointCache_frame_step_range(PointerRNA 
*ptr, int *min, int *max,
 {
        Object *ob = ptr->id.data;
        PointCache *cache = ptr->data;
-       PTCacheID *pid;
-       ListBase pidlist;
+       PTCacheID pid = BKE_ptcache_id_find(ob, NULL, cache);
 
        *min = 1;
        *max = 20;
 
-       BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
-       
-       for (pid = pidlist.first; pid; pid = pid->next) {
-               if (pid->cache == cache) {
-                       *max = pid->max_step;
-                       break;
-               }
+       if 

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to