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

object_group.c « object « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 53cabe3759e621909d2bf5627f90f40ba1e62e9d (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
 * ***** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) Blender Foundation
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/object/object_group.c
 *  \ingroup edobj
 */


#include <string.h>

#include "BLI_blenlib.h"
#include "BLI_utildefines.h"

#include "DNA_group_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"

#include "BKE_collection.h"
#include "BKE_context.h"
#include "BKE_library.h"
#include "BKE_library_remap.h"
#include "BKE_main.h"
#include "BKE_report.h"
#include "BKE_object.h"

#include "DEG_depsgraph.h"
#include "DEG_depsgraph_build.h"

#include "ED_screen.h"
#include "ED_object.h"

#include "WM_api.h"
#include "WM_types.h"

#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"

#include "object_intern.h"

/********************* 3d view operators ***********************/

/* can be called with C == NULL */
static const EnumPropertyItem *collection_object_active_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
{
	Main *bmain = CTX_data_main(C);
	Object *ob;
	EnumPropertyItem *item = NULL, item_tmp = {0};
	int totitem = 0;

	if (C == NULL) {
		return DummyRNA_NULL_items;
	}

	ob = ED_object_context(C);

	/* check that the object exists */
	if (ob) {
		Collection *collection;
		int i = 0, count = 0;

		/* if 2 or more collections, add option to add to all collections */
		collection = NULL;
		while ((collection = BKE_collection_object_find(bmain, collection, ob)))
			count++;

		if (count >= 2) {
			item_tmp.identifier = item_tmp.name = "All Collections";
			item_tmp.value = INT_MAX; /* this will give NULL on lookup */
			RNA_enum_item_add(&item, &totitem, &item_tmp);
			RNA_enum_item_add_separator(&item, &totitem);
		}

		/* add collections */
		collection = NULL;
		while ((collection = BKE_collection_object_find(bmain, collection, ob))) {
			item_tmp.identifier = item_tmp.name = collection->id.name + 2;
			/* item_tmp.icon = ICON_ARMATURE_DATA; */
			item_tmp.value = i;
			RNA_enum_item_add(&item, &totitem, &item_tmp);
			i++;
		}
	}

	RNA_enum_item_end(&item, &totitem);
	*r_free = true;

	return item;
}

/* get the collection back from the enum index, quite awkward and UI specific */
static Collection *collection_object_active_find_index(Main *bmain, Object *ob, const int collection_object_index)
{
	Collection *collection = NULL;
	int i = 0;
	while ((collection = BKE_collection_object_find(bmain, collection, ob))) {
		if (i == collection_object_index) {
			break;
		}
		i++;
	}

	return collection;
}

static int objects_add_active_exec(bContext *C, wmOperator *op)
{
	Object *ob = ED_object_context(C);
	Main *bmain = CTX_data_main(C);
	int single_collection_index = RNA_enum_get(op->ptr, "collection");
	Collection *single_collection = collection_object_active_find_index(bmain, ob, single_collection_index);
	Collection *collection;
	bool is_cycle = false;
	bool updated = false;

	if (ob == NULL)
		return OPERATOR_CANCELLED;

	/* now add all selected objects to the collection(s) */
	for (collection = bmain->collection.first; collection; collection = collection->id.next) {
		if (single_collection && collection != single_collection)
			continue;
		if (!BKE_collection_has_object(collection, ob))
			continue;

		CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
		{
			if (BKE_collection_has_object(collection, base->object))
				continue;

			if (!BKE_collection_object_cyclic_check(bmain, base->object, collection)) {
				BKE_collection_object_add(bmain, collection, base->object);
				DEG_id_tag_update(&collection->id, DEG_TAG_COPY_ON_WRITE);
				updated = true;
			}
			else {
				is_cycle = true;
			}
		}
		CTX_DATA_END;
	}

	if (is_cycle)
		BKE_report(op->reports, RPT_WARNING, "Skipped some collections because of cycle detected");

	if (!updated)
		return OPERATOR_CANCELLED;

	DEG_relations_tag_update(bmain);
	WM_event_add_notifier(C, NC_GROUP | NA_EDITED, NULL);

	return OPERATOR_FINISHED;
}

void COLLECTION_OT_objects_add_active(wmOperatorType *ot)
{
	PropertyRNA *prop;

	/* identifiers */
	ot->name = "Add Selected To Active Collection";
	ot->description = "Add the object to an object collection that contains the active object";
	ot->idname = "COLLECTION_OT_objects_add_active";

	/* api callbacks */
	ot->exec = objects_add_active_exec;
	ot->invoke = WM_menu_invoke;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	/* properties */
	prop = RNA_def_enum(ot->srna, "collection", DummyRNA_NULL_items, 0, "Collection", "The collection to add other selected objects to");
	RNA_def_enum_funcs(prop, collection_object_active_itemf);
	RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
	ot->prop = prop;
}

static int objects_remove_active_exec(bContext *C, wmOperator *op)
{
	Main *bmain = CTX_data_main(C);
	ViewLayer *view_layer = CTX_data_view_layer(C);
	Object *ob = OBACT(view_layer);
	int single_collection_index = RNA_enum_get(op->ptr, "collection");
	Collection *single_collection = collection_object_active_find_index(bmain, ob, single_collection_index);
	Collection *collection;
	bool ok = false;

	if (ob == NULL)
		return OPERATOR_CANCELLED;

	/* linking to same collection requires its own loop so we can avoid
	 * looking up the active objects collections each time */

	for (collection = bmain->collection.first; collection; collection = collection->id.next) {
		if (single_collection && collection != single_collection)
			continue;

		if (BKE_collection_has_object(collection, ob)) {
			/* Remove collections from selected objects */
			CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
			{
				BKE_collection_object_remove(bmain, collection, base->object, false);
				DEG_id_tag_update(&collection->id, DEG_TAG_COPY_ON_WRITE);
				ok = 1;
			}
			CTX_DATA_END;
		}
	}

	if (!ok)
		BKE_report(op->reports, RPT_ERROR, "Active object contains no collections");

	DEG_relations_tag_update(bmain);
	WM_event_add_notifier(C, NC_GROUP | NA_EDITED, NULL);

	return OPERATOR_FINISHED;
}

void COLLECTION_OT_objects_remove_active(wmOperatorType *ot)
{
	PropertyRNA *prop;

	/* identifiers */
	ot->name = "Remove Selected From Active Collection";
	ot->description = "Remove the object from an object collection that contains the active object";
	ot->idname = "COLLECTION_OT_objects_remove_active";

	/* api callbacks */
	ot->exec = objects_remove_active_exec;
	ot->invoke = WM_menu_invoke;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	/* properties */
	prop = RNA_def_enum(ot->srna, "collection", DummyRNA_NULL_items, 0, "Collection", "The collection to remove other selected objects from");
	RNA_def_enum_funcs(prop, collection_object_active_itemf);
	RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
	ot->prop = prop;
}

static int collection_objects_remove_all_exec(bContext *C, wmOperator *UNUSED(op))
{
	Main *bmain = CTX_data_main(C);

	CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
	{
		BKE_object_groups_clear(bmain, base->object);
	}
	CTX_DATA_END;

	DEG_relations_tag_update(bmain);
	WM_event_add_notifier(C, NC_GROUP | NA_EDITED, NULL);

	return OPERATOR_FINISHED;
}

void COLLECTION_OT_objects_remove_all(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Remove From All Unlinked Collections";
	ot->description = "Remove selected objects from all collections not used in a scene";
	ot->idname = "COLLECTION_OT_objects_remove_all";

	/* api callbacks */
	ot->exec = collection_objects_remove_all_exec;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

static int collection_objects_remove_exec(bContext *C, wmOperator *op)
{
	Object *ob = ED_object_context(C);
	Main *bmain = CTX_data_main(C);
	int single_collection_index = RNA_enum_get(op->ptr, "collection");
	Collection *single_collection = collection_object_active_find_index(bmain, ob, single_collection_index);
	Collection *collection;
	bool updated = false;

	if (ob == NULL)
		return OPERATOR_CANCELLED;

	for (collection = bmain->collection.first; collection; collection = collection->id.next) {
		if (single_collection && collection != single_collection)
			continue;
		if (!BKE_collection_has_object(collection, ob))
			continue;

		/* now remove all selected objects from the collection */
		CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
		{
			BKE_collection_object_remove(bmain, collection, base->object, false);
			DEG_id_tag_update(&collection->id, DEG_TAG_COPY_ON_WRITE);
			updated = true;
		}
		CTX_DATA_END;
	}

	if (!updated)
		return OPERATOR_CANCELLED;

	DEG_relations_tag_update(bmain);
	WM_event_add_notifier(C, NC_GROUP | NA_EDITED, NULL);

	return OPERATOR_FINISHED;
}

void COLLECTION_OT_objects_remove(wmOperatorType *ot)
{
	PropertyRNA *prop;

	/* identifiers */
	ot->name = "Remove From Collection";
	ot->description = "Remove selected objects from a collection";
	ot->idname = "COLLECTION_OT_objects_remove";

	/* api callbacks */
	ot->exec = collection_objects_remove_exec;
	ot->invoke = WM_menu_invoke;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	/* properties */
	prop = RNA_def_enum(ot->srna, "collection", DummyRNA_NULL_items, 0, "Collection", "The collection to remove this object from");
	RNA_def_enum_funcs(prop, collection_object_active_itemf);
	RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
	ot->prop = prop;
}

static int collection_create_exec(bContext *C, wmOperator *op)
{
	Main *bmain = CTX_data_main(C);
	char name[MAX_ID_NAME - 2]; /* id name */

	RNA_string_get(op->ptr, "name", name);

	Collection *collection = BKE_collection_add(bmain, NULL, name);
	id_fake_user_set(&collection->id);

	CTX_DATA_BEGIN (C, Base *, base, selected_bases)
	{
		BKE_collection_object_add(bmain, collection, base->object);
		DEG_id_tag_update(&collection->id, DEG_TAG_COPY_ON_WRITE);
	}
	CTX_DATA_END;

	DEG_relations_tag_update(bmain);
	WM_event_add_notifier(C, NC_GROUP | NA_EDITED, NULL);

	return OPERATOR_FINISHED;
}

void COLLECTION_OT_create(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Create New Collection";
	ot->description = "Create an object collection from selected objects";
	ot->idname = "COLLECTION_OT_create";

	/* api callbacks */
	ot->exec = collection_create_exec;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	RNA_def_string(ot->srna, "name", "Collection", MAX_ID_NAME - 2, "Name", "Name of the new collection");
}

/****************** properties window operators *********************/

static int collection_add_exec(bContext *C, wmOperator *UNUSED(op))
{
	Object *ob = ED_object_context(C);
	Main *bmain = CTX_data_main(C);

	if (ob == NULL)
		return OPERATOR_CANCELLED;

	Collection *collection = BKE_collection_add(bmain, NULL, "Collection");
	id_fake_user_set(&collection->id);
	BKE_collection_object_add(bmain, collection, ob);

	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);

	return OPERATOR_FINISHED;
}

void OBJECT_OT_collection_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add to Collection";
	ot->idname = "OBJECT_OT_collection_add";
	ot->description = "Add an object to a new collection";

	/* api callbacks */
	ot->exec = collection_add_exec;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

static int collection_link_exec(bContext *C, wmOperator *op)
{
	Main *bmain = CTX_data_main(C);
	Object *ob = ED_object_context(C);
	Collection *collection = BLI_findlink(&bmain->collection, RNA_enum_get(op->ptr, "collection"));

	if (ELEM(NULL, ob, collection))
		return OPERATOR_CANCELLED;

	/* Early return check, if the object is already in collection
	 * we could skip all the dependency check and just consider
	 * operator is finished.
	 */
	if (BKE_collection_has_object(collection, ob)) {
		return OPERATOR_FINISHED;
	}

	/* Adding object to collection which is used as duplicollection for self is bad idea.
	 *
	 * It is also  bad idea to add object to collection which is in collection which
	 * contains our current object.
	 */
	if (BKE_collection_object_cyclic_check(bmain, ob, collection)) {
		BKE_report(op->reports, RPT_ERROR, "Could not add the collection because of dependency cycle detected");
		return OPERATOR_CANCELLED;
	}

	BKE_collection_object_add(bmain, collection, ob);

	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);

	return OPERATOR_FINISHED;
}

void OBJECT_OT_collection_link(wmOperatorType *ot)
{
	PropertyRNA *prop;

	/* identifiers */
	ot->name = "Link to Collection";
	ot->idname = "OBJECT_OT_collection_link";
	ot->description = "Add an object to an existing collection";

	/* api callbacks */
	ot->exec = collection_link_exec;
	ot->invoke = WM_enum_search_invoke;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	/* properties */
	prop = RNA_def_enum(ot->srna, "collection", DummyRNA_NULL_items, 0, "Collection", "");
	RNA_def_enum_funcs(prop, RNA_collection_local_itemf);
	RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
	ot->prop = prop;
}

static int collection_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
	Main *bmain = CTX_data_main(C);
	Object *ob = ED_object_context(C);
	Collection *collection = CTX_data_pointer_get_type(C, "collection", &RNA_Collection).data;

	if (!ob || !collection)
		return OPERATOR_CANCELLED;

	BKE_collection_object_remove(bmain, collection, ob, false);

	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);

	return OPERATOR_FINISHED;
}

void OBJECT_OT_collection_remove(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Remove Collection";
	ot->idname = "OBJECT_OT_collection_remove";
	ot->description = "Remove the active object from this collection";

	/* api callbacks */
	ot->exec = collection_remove_exec;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}


static int collection_unlink_exec(bContext *C, wmOperator *UNUSED(op))
{
	Main *bmain = CTX_data_main(C);
	Collection *collection = CTX_data_pointer_get_type(C, "collection", &RNA_Collection).data;

	if (!collection)
		return OPERATOR_CANCELLED;

	BKE_libblock_delete(bmain, collection);

	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);

	return OPERATOR_FINISHED;
}

void OBJECT_OT_collection_unlink(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Unlink Collection";
	ot->idname = "OBJECT_OT_collection_unlink";
	ot->description = "Unlink the collection from all objects";

	/* api callbacks */
	ot->exec = collection_unlink_exec;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

static int select_grouped_exec(bContext *C, wmOperator *UNUSED(op))  /* Select objects in the same collection as the active */
{
	Collection *collection = CTX_data_pointer_get_type(C, "collection", &RNA_Collection).data;

	if (!collection)
		return OPERATOR_CANCELLED;

	CTX_DATA_BEGIN (C, Base *, base, visible_bases)
	{
		if (((base->flag & BASE_SELECTED) == 0) && ((base->flag & BASE_SELECTABLE) != 0)) {
			if (BKE_collection_has_object_recursive(collection, base->object)) {
				ED_object_base_select(base, BA_SELECT);
			}
		}
	}
	CTX_DATA_END;

	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);

	return OPERATOR_FINISHED;
}

void OBJECT_OT_collection_objects_select(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Select Objects in Collection";
	ot->idname = "OBJECT_OT_collection_objects_select";
	ot->description = "Select all objects in collection";

	/* api callbacks */
	ot->exec = select_grouped_exec;
	ot->poll = ED_operator_objectmode;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}