resolve some compiler warnings with intel c/c++ compiler
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
55 #ifdef HAVE_CONFIG_H
56 #include <config.h>
57 #endif
58
59 void free_group_object(GroupObject *go)
60 {
61         MEM_freeN(go);
62 }
63
64
65 void free_group(Group *group)
66 {
67         /* don't free group itself */
68         GroupObject *go;
69         
70         while(group->gobject.first) {
71                 go= group->gobject.first;
72                 BLI_remlink(&group->gobject, go);
73                 free_group_object(go);
74         }
75 }
76
77 void unlink_group(Group *group)
78 {
79         Material *ma;
80         Object *ob;
81         Scene *sce;
82         SceneRenderLayer *srl;
83         ParticleSystem *psys;
84         
85         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
86                 if(ma->group==group)
87                         ma->group= NULL;
88         }
89         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
90                 if(ma->group==group)
91                         ma->group= NULL;
92         }
93         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
94                 Base *base= sce->base.first;
95                 
96                 /* ensure objects are not in this group */
97                 for(; base; base= base->next) {
98                         if(rem_from_group(group, base->object) && find_group(base->object, NULL)==NULL) {
99                                 base->object->flag &= ~OB_FROMGROUP;
100                                 base->flag &= ~OB_FROMGROUP;
101                         }
102                 }                       
103                 
104                 for(srl= sce->r.layers.first; srl; srl= srl->next) {
105                         if (srl->light_override==group)
106                                 srl->light_override= NULL;
107                 }
108         }
109         
110         for(ob= G.main->object.first; ob; ob= ob->id.next) {
111                 bActionStrip *strip;
112                 
113                 if(ob->dup_group==group) {
114                         ob->dup_group= NULL;
115                 
116                         /* duplicator strips use a group object, we remove it */
117                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
118                                 if(strip->object)
119                                         strip->object= NULL;
120                         }
121                 }
122                 
123                 for(psys=ob->particlesystem.first; psys; psys=psys->next){
124                         if(psys->part->dup_group==group)
125                                 psys->part->dup_group= NULL;
126                         if(psys->part->eff_group==group)
127                                 psys->part->eff_group= NULL;
128                 }
129         }
130         
131         /* group stays in library, but no members */
132         free_group(group);
133         group->id.us= 0;
134 }
135
136 Group *add_group(char *name)
137 {
138         Group *group;
139         
140         group = alloc_libblock(&G.main->group, ID_GR, name);
141         group->layer= (1<<20)-1;
142         return group;
143 }
144
145 /* external */
146 void add_to_group(Group *group, Object *ob)
147 {
148         GroupObject *go;
149         
150         if(group==NULL || ob==NULL) return;
151         
152         /* check if the object has been added already */
153         for(go= group->gobject.first; go; go= go->next) {
154                 if(go->ob==ob) return;
155         }
156         
157         go= MEM_callocN(sizeof(GroupObject), "groupobject");
158         BLI_addtail( &group->gobject, go);
159         
160         go->ob= ob;
161         
162 }
163
164 /* also used for ob==NULL */
165 int rem_from_group(Group *group, Object *ob)
166 {
167         GroupObject *go, *gon;
168         int removed = 0;
169         if(group==NULL) return 0;
170         
171         go= group->gobject.first;
172         while(go) {
173                 gon= go->next;
174                 if(go->ob==ob) {
175                         BLI_remlink(&group->gobject, go);
176                         free_group_object(go);
177                         removed = 1;
178                         /* should break here since an object being in a group twice cant happen? */
179                 }
180                 go= gon;
181         }
182         return removed;
183 }
184
185 int object_in_group(Object *ob, Group *group)
186 {
187         GroupObject *go;
188         
189         if(group==NULL || ob==NULL) return 0;
190         
191         for(go= group->gobject.first; go; go= go->next) {
192                 if(go->ob==ob) 
193                         return 1;
194         }
195         return 0;
196 }
197
198 Group *find_group(Object *ob, Group *group)
199 {
200         if (group)
201                 group= group->id.next;
202         else
203                 group= G.main->group.first;
204         
205         while(group) {
206                 if(object_in_group(ob, group))
207                         return group;
208                 group= group->id.next;
209         }
210         return NULL;
211 }
212
213 void group_tag_recalc(Group *group)
214 {
215         GroupObject *go;
216         
217         if(group==NULL) return;
218         
219         for(go= group->gobject.first; go; go= go->next) {
220                 if(go->ob) 
221                         go->ob->recalc= go->recalc;
222         }
223 }
224
225 int group_is_animated(Object *parent, Group *group)
226 {
227         GroupObject *go;
228
229         if(give_timeoffset(parent) != 0.0f || parent->nlastrips.first)
230                 return 1;
231
232         for(go= group->gobject.first; go; go= go->next)
233                 if(go->ob && go->ob->proxy)
234                         return 1;
235
236         return 0;
237 }
238
239 /* only replaces object strips or action when parent nla instructs it */
240 /* keep checking nla.c though, in case internal structure of strip changes */
241 static void group_replaces_nla(Object *parent, Object *target, char mode)
242 {
243         static ListBase nlastrips={NULL, NULL};
244         static bAction *action= NULL;
245         static int done= 0;
246         bActionStrip *strip, *nstrip;
247         
248         if(mode=='s') {
249                 
250                 for(strip= parent->nlastrips.first; strip; strip= strip->next) {
251                         if(strip->object==target) {
252                                 if(done==0) {
253                                         /* clear nla & action from object */
254                                         nlastrips= target->nlastrips;
255                                         target->nlastrips.first= target->nlastrips.last= NULL;
256                                         action= target->action;
257                                         target->action= NULL;
258                                         target->nlaflag |= OB_NLA_OVERRIDE;
259                                         done= 1;
260                                 }
261                                 nstrip= MEM_dupallocN(strip);
262                                 BLI_addtail(&target->nlastrips, nstrip);
263                         }
264                 }
265         }
266         else if(mode=='e') {
267                 if(done) {
268                         BLI_freelistN(&target->nlastrips);
269                         target->nlastrips= nlastrips;
270                         target->action= action;
271                         
272                         nlastrips.first= nlastrips.last= NULL;  /* not needed, but yah... :) */
273                         action= NULL;
274                         done= 0;
275                 }
276         }
277 }
278
279 /* puts all group members in local timing system, after this call
280 you can draw everything, leaves tags in objects to signal it needs further updating */
281
282 /* note: does not work for derivedmesh and render... it recreates all again in convertblender.c */
283 void group_handle_recalc_and_update(Object *parent, Group *group)
284 {
285         GroupObject *go;
286         
287         /* if animated group... */
288         if(give_timeoffset(parent) != 0.0f || parent->nlastrips.first) {
289                 int cfrao;
290                 
291                 /* switch to local time */
292                 cfrao= G.scene->r.cfra;
293                 G.scene->r.cfra -= (int)give_timeoffset(parent);
294                 
295                 /* we need a DAG per group... */
296                 for(go= group->gobject.first; go; go= go->next) {
297                         if(go->ob && go->recalc) {
298                                 go->ob->recalc= go->recalc;
299                                 
300                                 group_replaces_nla(parent, go->ob, 's');
301                                 object_handle_update(go->ob);
302                                 group_replaces_nla(parent, go->ob, 'e');
303                                 
304                                 /* leave recalc tags in case group members are in normal scene */
305                                 go->ob->recalc= go->recalc;
306                         }
307                 }
308                 
309                 /* restore */
310                 G.scene->r.cfra= cfrao;
311         }
312         else {
313                 /* only do existing tags, as set by regular depsgraph */
314                 for(go= group->gobject.first; go; go= go->next) {
315                         if(go->ob) {
316                                 if(go->ob->recalc) {
317                                         object_handle_update(go->ob);
318                                 }
319                         }
320                 }
321         }
322 }
323
324 Object *group_get_member_with_action(Group *group, bAction *act)
325 {
326         GroupObject *go;
327         
328         if(group==NULL || act==NULL) return NULL;
329         
330         for(go= group->gobject.first; go; go= go->next) {
331                 if(go->ob) {
332                         if(go->ob->action==act)
333                                 return go->ob;
334                         if(go->ob->nlastrips.first) {
335                                 bActionStrip *strip;
336                                 
337                                 for(strip= go->ob->nlastrips.first; strip; strip= strip->next) {
338                                         if(strip->act==act)
339                                                 return go->ob;
340                                 }
341                         }
342                 }
343         }
344         return NULL;
345 }
346
347 /* if group has NLA, we try to map the used objects in NLA to group members */
348 /* this assuming that object has received a new group link */
349 void group_relink_nla_objects(Object *ob)
350 {
351         Group *group;
352         GroupObject *go;
353         bActionStrip *strip;
354         
355         if(ob==NULL || ob->dup_group==NULL) return;
356         group= ob->dup_group;
357         
358         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
359                 if(strip->object) {
360                         for(go= group->gobject.first; go; go= go->next) {
361                                 if(go->ob) {
362                                         if(strcmp(go->ob->id.name, strip->object->id.name)==0)
363                                                 break;
364                                 }
365                         }
366                         if(go)
367                                 strip->object= go->ob;
368                         else
369                                 strip->object= NULL;
370                 }
371                         
372         }
373 }
374