constraints unique name length was set too long (could overrun the buffer), use sizeo...
[blender.git] / source / blender / blenkernel / intern / group.c
1 /* 
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_action_types.h"
36 #include "DNA_effect_types.h"
37 #include "DNA_group_types.h"
38 #include "DNA_ID.h"
39 #include "DNA_ipo_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_nla_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_particle_types.h"
45
46 #include "BLI_blenlib.h"
47
48 #include "BKE_global.h"
49 #include "BKE_group.h"
50 #include "BKE_ipo.h"
51 #include "BKE_library.h"
52 #include "BKE_main.h"
53 #include "BKE_object.h"
54 #include "BKE_scene.h" /* object_in_scene */
55
56 #ifdef HAVE_CONFIG_H
57 #include <config.h>
58 #endif
59
60 static void free_group_object(GroupObject *go)
61 {
62         MEM_freeN(go);
63 }
64
65
66 void free_group_objects(Group *group)
67 {
68         /* don't free group itself */
69         GroupObject *go;
70         
71         while(group->gobject.first) {
72                 go= group->gobject.first;
73                 BLI_remlink(&group->gobject, go);
74                 free_group_object(go);
75         }
76 }
77
78 void unlink_group(Group *group)
79 {
80         Material *ma;
81         Object *ob;
82         Scene *sce;
83         SceneRenderLayer *srl;
84         ParticleSystem *psys;
85         
86         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
87                 if(ma->group==group)
88                         ma->group= NULL;
89         }
90         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
91                 if(ma->group==group)
92                         ma->group= NULL;
93         }
94         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
95                 Base *base= sce->base.first;
96                 
97                 /* ensure objects are not in this group */
98                 for(; base; base= base->next) {
99                         if(rem_from_group(group, base->object, sce, base) && find_group(base->object, NULL)==NULL) {
100                                 base->object->flag &= ~OB_FROMGROUP;
101                                 base->flag &= ~OB_FROMGROUP;
102                         }
103                 }                       
104                 
105                 for(srl= sce->r.layers.first; srl; srl= srl->next) {
106                         if (srl->light_override==group)
107                                 srl->light_override= NULL;
108                 }
109         }
110         
111         for(ob= G.main->object.first; ob; ob= ob->id.next) {
112                 bActionStrip *strip;
113                 
114                 if(ob->dup_group==group) {
115                         ob->dup_group= NULL;
116                 
117                         /* duplicator strips use a group object, we remove it */
118                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
119                                 if(strip->object)
120                                         strip->object= NULL;
121                         }
122                 }
123                 
124                 for(psys=ob->particlesystem.first; psys; psys=psys->next){
125                         if(psys->part->dup_group==group)
126                                 psys->part->dup_group= NULL;
127                         if(psys->part->eff_group==group)
128                                 psys->part->eff_group= NULL;
129                 }
130         }
131         
132         /* group stays in library, but no members */
133         free_group_objects(group);
134         group->id.us= 0;
135 }
136
137 Group *add_group(char *name)
138 {
139         Group *group;
140         
141         group = alloc_libblock(&G.main->group, ID_GR, name);
142         group->layer= (1<<20)-1;
143         return group;
144 }
145
146 Group *copy_group(Group *group)
147 {
148         Group *groupn;
149
150         groupn= MEM_dupallocN(group);
151         BLI_duplicatelist(&groupn->gobject, &group->gobject);
152
153         return groupn;
154 }
155
156 /* external */
157 static int add_to_group_internal(Group *group, Object *ob)
158 {
159         GroupObject *go;
160         
161         if(group==NULL || ob==NULL) return 0;
162         
163         /* check if the object has been added already */
164         for(go= group->gobject.first; go; go= go->next) {
165                 if(go->ob==ob) return 0;
166         }
167         
168         go= MEM_callocN(sizeof(GroupObject), "groupobject");
169         BLI_addtail( &group->gobject, go);
170         
171         go->ob= ob;
172         
173         return 1;
174 }
175
176 int add_to_group(Group *group, Object *object, Scene *scene, Base *base)
177 {
178         if(add_to_group_internal(group, object)) {
179                 if((object->flag & OB_FROMGROUP)==0) {
180
181                         if(scene && base==NULL)
182                                 base= object_in_scene(object, scene);
183
184                         object->flag |= OB_FROMGROUP;
185
186                         if(base)
187                                 base->flag |= OB_FROMGROUP;
188                 }
189                 return 1;
190         }
191         else {
192                 return 0;
193         }
194 }
195
196 /* also used for ob==NULL */
197 static int rem_from_group_internal(Group *group, Object *ob)
198 {
199         GroupObject *go, *gon;
200         int removed = 0;
201         if(group==NULL) return 0;
202         
203         go= group->gobject.first;
204         while(go) {
205                 gon= go->next;
206                 if(go->ob==ob) {
207                         BLI_remlink(&group->gobject, go);
208                         free_group_object(go);
209                         removed = 1;
210                         /* should break here since an object being in a group twice cant happen? */
211                 }
212                 go= gon;
213         }
214         return removed;
215 }
216
217 int rem_from_group(Group *group, Object *object, Scene *scene, Base *base)
218 {
219         if(rem_from_group_internal(group, object)) {
220                 /* object can be NULL */
221                 if(object && find_group(object, NULL) == NULL) {
222                         if(scene && base==NULL)
223                                 base= object_in_scene(object, scene);
224
225                         object->flag &= ~OB_FROMGROUP;
226
227                         if(base)
228                                 base->flag &= ~OB_FROMGROUP;
229                 }
230                 return 1;
231         }
232         else {
233                 return 0;
234         }
235 }
236
237 int object_in_group(Object *ob, Group *group)
238 {
239         GroupObject *go;
240         
241         if(group==NULL || ob==NULL) return 0;
242         
243         for(go= group->gobject.first; go; go= go->next) {
244                 if(go->ob==ob) 
245                         return 1;
246         }
247         return 0;
248 }
249
250 Group *find_group(Object *ob, Group *group)
251 {
252         if (group)
253                 group= group->id.next;
254         else
255                 group= G.main->group.first;
256         
257         while(group) {
258                 if(object_in_group(ob, group))
259                         return group;
260                 group= group->id.next;
261         }
262         return NULL;
263 }
264
265 void group_tag_recalc(Group *group)
266 {
267         GroupObject *go;
268         
269         if(group==NULL) return;
270         
271         for(go= group->gobject.first; go; go= go->next) {
272                 if(go->ob) 
273                         go->ob->recalc= go->recalc;
274         }
275 }
276
277 int group_is_animated(Object *parent, Group *group)
278 {
279         GroupObject *go;
280
281         if(give_timeoffset(parent) != 0.0f || parent->nlastrips.first)
282                 return 1;
283
284         for(go= group->gobject.first; go; go= go->next)
285                 if(go->ob && go->ob->proxy)
286                         return 1;
287
288         return 0;
289 }
290
291 /* only replaces object strips or action when parent nla instructs it */
292 /* keep checking nla.c though, in case internal structure of strip changes */
293 static void group_replaces_nla(Object *parent, Object *target, char mode)
294 {
295         static ListBase nlastrips={NULL, NULL};
296         static bAction *action= NULL;
297         static int done= 0;
298         bActionStrip *strip, *nstrip;
299         
300         if(mode=='s') {
301                 
302                 for(strip= parent->nlastrips.first; strip; strip= strip->next) {
303                         if(strip->object==target) {
304                                 if(done==0) {
305                                         /* clear nla & action from object */
306                                         nlastrips= target->nlastrips;
307                                         target->nlastrips.first= target->nlastrips.last= NULL;
308                                         action= target->action;
309                                         target->action= NULL;
310                                         target->nlaflag |= OB_NLA_OVERRIDE;
311                                         done= 1;
312                                 }
313                                 nstrip= MEM_dupallocN(strip);
314                                 BLI_addtail(&target->nlastrips, nstrip);
315                         }
316                 }
317         }
318         else if(mode=='e') {
319                 if(done) {
320                         BLI_freelistN(&target->nlastrips);
321                         target->nlastrips= nlastrips;
322                         target->action= action;
323                         
324                         nlastrips.first= nlastrips.last= NULL;  /* not needed, but yah... :) */
325                         action= NULL;
326                         done= 0;
327                 }
328         }
329 }
330
331 /* puts all group members in local timing system, after this call
332 you can draw everything, leaves tags in objects to signal it needs further updating */
333
334 /* note: does not work for derivedmesh and render... it recreates all again in convertblender.c */
335 void group_handle_recalc_and_update(Scene *scene, Object *parent, Group *group)
336 {
337         GroupObject *go;
338         
339         /* if animated group... */
340         if(give_timeoffset(parent) != 0.0f || parent->nlastrips.first) {
341                 int cfrao;
342                 
343                 /* switch to local time */
344                 cfrao= scene->r.cfra;
345                 scene->r.cfra -= (int)give_timeoffset(parent);
346                 
347                 /* we need a DAG per group... */
348                 for(go= group->gobject.first; go; go= go->next) {
349                         if(go->ob && go->recalc) {
350                                 go->ob->recalc= go->recalc;
351                                 
352                                 group_replaces_nla(parent, go->ob, 's');
353                                 object_handle_update(scene, go->ob);
354                                 group_replaces_nla(parent, go->ob, 'e');
355                                 
356                                 /* leave recalc tags in case group members are in normal scene */
357                                 go->ob->recalc= go->recalc;
358                         }
359                 }
360                 
361                 /* restore */
362                 scene->r.cfra= cfrao;
363         }
364         else {
365                 /* only do existing tags, as set by regular depsgraph */
366                 for(go= group->gobject.first; go; go= go->next) {
367                         if(go->ob) {
368                                 if(go->ob->recalc) {
369                                         object_handle_update(scene, go->ob);
370                                 }
371                         }
372                 }
373         }
374 }
375
376 Object *group_get_member_with_action(Group *group, bAction *act)
377 {
378         GroupObject *go;
379         
380         if(group==NULL || act==NULL) return NULL;
381         
382         for(go= group->gobject.first; go; go= go->next) {
383                 if(go->ob) {
384                         if(go->ob->action==act)
385                                 return go->ob;
386                         if(go->ob->nlastrips.first) {
387                                 bActionStrip *strip;
388                                 
389                                 for(strip= go->ob->nlastrips.first; strip; strip= strip->next) {
390                                         if(strip->act==act)
391                                                 return go->ob;
392                                 }
393                         }
394                 }
395         }
396         return NULL;
397 }
398
399 /* if group has NLA, we try to map the used objects in NLA to group members */
400 /* this assuming that object has received a new group link */
401 void group_relink_nla_objects(Object *ob)
402 {
403         Group *group;
404         GroupObject *go;
405         bActionStrip *strip;
406         
407         if(ob==NULL || ob->dup_group==NULL) return;
408         group= ob->dup_group;
409         
410         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
411                 if(strip->object) {
412                         for(go= group->gobject.first; go; go= go->next) {
413                                 if(go->ob) {
414                                         if(strcmp(go->ob->id.name, strip->object->id.name)==0)
415                                                 break;
416                                 }
417                         }
418                         if(go)
419                                 strip->object= go->ob;
420                         else
421                                 strip->object= NULL;
422                 }
423                         
424         }
425 }
426