Welcome to mirror list, hosted at ThFree Co, Russian Federation.

group.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fffbd794ef75b5dc4824e9a9c3c8bd1275133e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* 
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include <stdio.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "DNA_action_types.h"
#include "DNA_effect_types.h"
#include "DNA_group_types.h"
#include "DNA_ID.h"
#include "DNA_ipo_types.h"
#include "DNA_material_types.h"
#include "DNA_object_types.h"
#include "DNA_nla_types.h"
#include "DNA_scene_types.h"
#include "DNA_particle_types.h"

#include "BLI_blenlib.h"

#include "BKE_global.h"
#include "BKE_group.h"
#include "BKE_ipo.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_object.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

void free_group_object(GroupObject *go)
{
	MEM_freeN(go);
}


void free_group(Group *group)
{
	/* don't free group itself */
	GroupObject *go;
	
	while(group->gobject.first) {
		go= group->gobject.first;
		BLI_remlink(&group->gobject, go);
		free_group_object(go);
	}
}

void unlink_group(Group *group)
{
	Material *ma;
	Object *ob;
	Scene *sce;
	SceneRenderLayer *srl;
	ParticleSystem *psys;
	
	for(ma= G.main->mat.first; ma; ma= ma->id.next) {
		if(ma->group==group)
			ma->group= NULL;
	}
	for(ma= G.main->mat.first; ma; ma= ma->id.next) {
		if(ma->group==group)
			ma->group= NULL;
	}
	for (sce= G.main->scene.first; sce; sce= sce->id.next) {
		Base *base= sce->base.first;
		
		/* ensure objects are not in this group */
		for(; base; base= base->next) {
			if(rem_from_group(group, base->object) && find_group(base->object, NULL)==NULL) {
				base->object->flag &= ~OB_FROMGROUP;
				base->flag &= ~OB_FROMGROUP;
			}
		}			
		
		for(srl= sce->r.layers.first; srl; srl= srl->next) {
			if (srl->light_override==group)
				srl->light_override= NULL;
		}
	}
	
	for(ob= G.main->object.first; ob; ob= ob->id.next) {
		bActionStrip *strip;
		
		if(ob->dup_group==group) {
			ob->dup_group= NULL;
		
			/* duplicator strips use a group object, we remove it */
			for(strip= ob->nlastrips.first; strip; strip= strip->next) {
				if(strip->object)
					strip->object= NULL;
			}
		}
		
		for(psys=ob->particlesystem.first; psys; psys=psys->next){
			if(psys->part->dup_group==group)
				psys->part->dup_group= NULL;
			if(psys->part->eff_group==group)
				psys->part->eff_group= NULL;
		}
	}
	
	/* group stays in library, but no members */
	free_group(group);
	group->id.us= 0;
}

Group *add_group(char *name)
{
	Group *group;
	
	group = alloc_libblock(&G.main->group, ID_GR, name);
	group->layer= (1<<20)-1;
	return group;
}

/* external */
void add_to_group(Group *group, Object *ob)
{
	GroupObject *go;
	
	if(group==NULL || ob==NULL) return;
	
	/* check if the object has been added already */
	for(go= group->gobject.first; go; go= go->next) {
		if(go->ob==ob) return;
	}
	
	go= MEM_callocN(sizeof(GroupObject), "groupobject");
	BLI_addtail( &group->gobject, go);
	
	go->ob= ob;
	
}

/* also used for ob==NULL */
int rem_from_group(Group *group, Object *ob)
{
	GroupObject *go, *gon;
	int removed = 0;
	if(group==NULL) return 0;
	
	go= group->gobject.first;
	while(go) {
		gon= go->next;
		if(go->ob==ob) {
			BLI_remlink(&group->gobject, go);
			free_group_object(go);
			removed = 1;
			/* should break here since an object being in a group twice cant happen? */
		}
		go= gon;
	}
	return removed;
}

int object_in_group(Object *ob, Group *group)
{
	GroupObject *go;
	
	if(group==NULL || ob==NULL) return 0;
	
	for(go= group->gobject.first; go; go= go->next) {
		if(go->ob==ob) 
			return 1;
	}
	return 0;
}

Group *find_group(Object *ob, Group *group)
{
	if (group)
		group= group->id.next;
	else
		group= G.main->group.first;
	
	while(group) {
		if(object_in_group(ob, group))
			return group;
		group= group->id.next;
	}
	return NULL;
}

void group_tag_recalc(Group *group)
{
	GroupObject *go;
	
	if(group==NULL) return;
	
	for(go= group->gobject.first; go; go= go->next) {
		if(go->ob) 
			go->ob->recalc= go->recalc;
	}
}

int group_is_animated(Object *parent, Group *group)
{
	GroupObject *go;

	if(give_timeoffset(parent) != 0.0f || parent->nlastrips.first)
		return 1;

	for(go= group->gobject.first; go; go= go->next)
		if(go->ob && go->ob->proxy)
			return 1;

	return 0;
}

/* only replaces object strips or action when parent nla instructs it */
/* keep checking nla.c though, in case internal structure of strip changes */
static void group_replaces_nla(Object *parent, Object *target, char mode)
{
	static ListBase nlastrips={NULL, NULL};
	static bAction *action= NULL;
	static int done= 0;
	bActionStrip *strip, *nstrip;
	
	if(mode=='s') {
		
		for(strip= parent->nlastrips.first; strip; strip= strip->next) {
			if(strip->object==target) {
				if(done==0) {
					/* clear nla & action from object */
					nlastrips= target->nlastrips;
					target->nlastrips.first= target->nlastrips.last= NULL;
					action= target->action;
					target->action= NULL;
					target->nlaflag |= OB_NLA_OVERRIDE;
					done= 1;
				}
				nstrip= MEM_dupallocN(strip);
				BLI_addtail(&target->nlastrips, nstrip);
			}
		}
	}
	else if(mode=='e') {
		if(done) {
			BLI_freelistN(&target->nlastrips);
			target->nlastrips= nlastrips;
			target->action= action;
			
			nlastrips.first= nlastrips.last= NULL;	/* not needed, but yah... :) */
			action= NULL;
			done= 0;
		}
	}
}

/* puts all group members in local timing system, after this call
you can draw everything, leaves tags in objects to signal it needs further updating */

/* note: does not work for derivedmesh and render... it recreates all again in convertblender.c */
void group_handle_recalc_and_update(Scene *scene, Object *parent, Group *group)
{
	GroupObject *go;
	
	/* if animated group... */
	if(give_timeoffset(parent) != 0.0f || parent->nlastrips.first) {
		int cfrao;
		
		/* switch to local time */
		cfrao= scene->r.cfra;
		scene->r.cfra -= (int)give_timeoffset(parent);
		
		/* we need a DAG per group... */
		for(go= group->gobject.first; go; go= go->next) {
			if(go->ob && go->recalc) {
				go->ob->recalc= go->recalc;
				
				group_replaces_nla(parent, go->ob, 's');
				object_handle_update(scene, go->ob);
				group_replaces_nla(parent, go->ob, 'e');
				
				/* leave recalc tags in case group members are in normal scene */
				go->ob->recalc= go->recalc;
			}
		}
		
		/* restore */
		scene->r.cfra= cfrao;
	}
	else {
		/* only do existing tags, as set by regular depsgraph */
		for(go= group->gobject.first; go; go= go->next) {
			if(go->ob) {
				if(go->ob->recalc) {
					object_handle_update(scene, go->ob);
				}
			}
		}
	}
}

Object *group_get_member_with_action(Group *group, bAction *act)
{
	GroupObject *go;
	
	if(group==NULL || act==NULL) return NULL;
	
	for(go= group->gobject.first; go; go= go->next) {
		if(go->ob) {
			if(go->ob->action==act)
				return go->ob;
			if(go->ob->nlastrips.first) {
				bActionStrip *strip;
				
				for(strip= go->ob->nlastrips.first; strip; strip= strip->next) {
					if(strip->act==act)
						return go->ob;
				}
			}
		}
	}
	return NULL;
}

/* if group has NLA, we try to map the used objects in NLA to group members */
/* this assuming that object has received a new group link */
void group_relink_nla_objects(Object *ob)
{
	Group *group;
	GroupObject *go;
	bActionStrip *strip;
	
	if(ob==NULL || ob->dup_group==NULL) return;
	group= ob->dup_group;
	
	for(strip= ob->nlastrips.first; strip; strip= strip->next) {
		if(strip->object) {
			for(go= group->gobject.first; go; go= go->next) {
				if(go->ob) {
					if(strcmp(go->ob->id.name, strip->object->id.name)==0)
						break;
				}
			}
			if(go)
				strip->object= go->ob;
			else
				strip->object= NULL;
		}
			
	}
}