Merging r38390 through r38418 from trunk into soc-2011-tomato
[blender-staging.git] / source / blender / blenloader / intern / readfile.c
index a26ef101c1cbbfbfe93d46f83155b1f3f584cf0a..496b8e9ab070b4b615e5d95e26250d930d37371e 100644 (file)
@@ -75,6 +75,7 @@
 #include "DNA_node_types.h"
 #include "DNA_object_fluidsim.h" // NT
 #include "DNA_packedFile_types.h"
 #include "DNA_node_types.h"
 #include "DNA_object_fluidsim.h" // NT
 #include "DNA_packedFile_types.h"
+#include "DNA_particle_types.h"
 #include "DNA_property_types.h"
 #include "DNA_text_types.h"
 #include "DNA_view3d_types.h"
 #include "DNA_property_types.h"
 #include "DNA_text_types.h"
 #include "DNA_view3d_types.h"
@@ -3203,9 +3204,37 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                        if(part->effector_weights)
                                part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
 
                        if(part->effector_weights)
                                part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
 
-                       dw = part->dupliweights.first;
-                       for(; dw; dw=dw->next)
-                               dw->ob = newlibadr(fd, part->id.lib, dw->ob);
+                       if(part->dupliweights.first) {
+                               int index_ok = 0;
+                               /* check for old files without indices (all indexes 0) */
+                               dw = part->dupliweights.first;
+                               if(part->dupliweights.first == part->dupliweights.last) {
+                                       /* special case for only one object in the group */
+                                       index_ok = 1;
+                               }
+                               else { 
+                                       for(; dw; dw=dw->next) {
+                                               if(dw->index > 0) {
+                                                       index_ok = 1;
+                                                       break;
+                                               }
+                                       }
+                               }
+
+                               if(index_ok) {
+                                       /* if we have indexes, let's use them */
+                                       dw = part->dupliweights.first;
+                                       for(; dw; dw=dw->next) {
+                                               GroupObject *go = (GroupObject *)BLI_findlink(&part->dup_group->gobject, dw->index);
+                                               dw->ob = go ? go->ob : NULL;
+                                       }
+                               }
+                               else {
+                                       /* otherwise try to get objects from own library (won't work on library linked groups) */
+                                       for(; dw; dw=dw->next)
+                                               dw->ob = newlibadr(fd, part->id.lib, dw->ob);
+                               }
+                       }
 
                        if(part->boids) {
                                BoidState *state = part->boids->states.first;
 
                        if(part->boids) {
                                BoidState *state = part->boids->states.first;