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