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
|
/*
* ***** 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_context.h"
#include "BKE_depsgraph.h"
#include "BKE_group.h"
#include "BKE_main.h"
#include "BKE_report.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 ***********************/
static int objects_add_active_exec(bContext *C, wmOperator *op)
{
Main *bmain= CTX_data_main(C);
Scene *scene= CTX_data_scene(C);
Object *ob= OBACT;
Group *group;
int ok = 0, cycle = 0;
if (!ob) return OPERATOR_CANCELLED;
/* linking to same group requires its own loop so we can avoid
* looking up the active objects groups each time */
for (group= bmain->group.first; group; group=group->id.next) {
if (object_in_group(ob, group)) {
/* Assign groups to selected objects */
CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
if (base->object->dup_group != group)
add_to_group(group, base->object, scene, base);
else
cycle = 1;
ok = 1;
}
CTX_DATA_END;
}
}
if (!ok) BKE_report(op->reports, RPT_ERROR, "Active Object contains no groups");
if (cycle)
BKE_report(op->reports, RPT_WARNING, "Skipped some groups because of cycle detected");
DAG_scene_sort(bmain, scene);
WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL);
return OPERATOR_FINISHED;
}
void GROUP_OT_objects_add_active(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Selected To Active Group";
ot->description = "Add the object to an object group that contains the active object";
ot->idname = "GROUP_OT_objects_add_active";
/* api callbacks */
ot->exec = objects_add_active_exec;
ot->poll = ED_operator_objectmode;
/* flags */
ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
}
static int objects_remove_active_exec(bContext *C, wmOperator *op)
{
Main *bmain= CTX_data_main(C);
Scene *scene= CTX_data_scene(C);
Object *ob= OBACT;
Group *group;
int ok = 0;
if (!ob) return OPERATOR_CANCELLED;
/* linking to same group requires its own loop so we can avoid
* looking up the active objects groups each time */
for (group= bmain->group.first; group; group=group->id.next) {
if (object_in_group(ob, group)) {
/* Assign groups to selected objects */
CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
rem_from_group(group, base->object, scene, base);
ok = 1;
}
CTX_DATA_END;
}
}
if (!ok) BKE_report(op->reports, RPT_ERROR, "Active Object contains no groups");
DAG_scene_sort(bmain, scene);
WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL);
return OPERATOR_FINISHED;
}
void GROUP_OT_objects_remove_active(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Remove Selected From Active Group";
ot->description = "Remove the object from an object group that contains the active object";
ot->idname = "GROUP_OT_objects_remove_active";
/* api callbacks */
ot->exec = objects_remove_active_exec;
ot->poll = ED_operator_objectmode;
/* flags */
ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
}
static int group_objects_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
Main *bmain= CTX_data_main(C);
Scene *scene= CTX_data_scene(C);
Group *group= NULL;
CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
group = NULL;
while ((group = find_group(base->object, group)))
rem_from_group(group, base->object, scene, base);
}
CTX_DATA_END;
DAG_scene_sort(bmain, scene);
WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL);
return OPERATOR_FINISHED;
}
void GROUP_OT_objects_remove(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Remove From Groups";
ot->description = "Remove selected objects from all groups";
ot->idname = "GROUP_OT_objects_remove";
/* api callbacks */
ot->exec = group_objects_remove_exec;
ot->poll = ED_operator_objectmode;
/* flags */
ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
}
static int group_create_exec(bContext *C, wmOperator *op)
{
Main *bmain= CTX_data_main(C);
Scene *scene= CTX_data_scene(C);
Group *group= NULL;
char name[MAX_ID_NAME-2]; /* id name */
RNA_string_get(op->ptr, "name", name);
group= add_group(name);
CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
add_to_group(group, base->object, scene, base);
}
CTX_DATA_END;
DAG_scene_sort(bmain, scene);
WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL);
return OPERATOR_FINISHED;
}
void GROUP_OT_create(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Create New Group";
ot->description = "Create an object group from selected objects";
ot->idname = "GROUP_OT_create";
/* api callbacks */
ot->exec = group_create_exec;
ot->poll = ED_operator_objectmode;
/* flags */
ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
RNA_def_string(ot->srna, "name", "Group", MAX_ID_NAME-2, "Name", "Name of the new group");
}
/****************** properties window operators *********************/
static int group_add_exec(bContext *C, wmOperator *UNUSED(op))
{
Scene *scene= CTX_data_scene(C);
Object *ob= ED_object_context(C);
Group *group;
if (ob == NULL)
return OPERATOR_CANCELLED;
group= add_group("Group");
add_to_group(group, ob, scene, NULL);
WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_group_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add to Group";
ot->idname = "OBJECT_OT_group_add";
ot->description = "Add an object to a new group";
/* api callbacks */
ot->exec = group_add_exec;
/* flags */
ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
}
static int group_link_exec(bContext *C, wmOperator *op)
{
Scene *scene= CTX_data_scene(C);
Object *ob= ED_object_context(C);
Group *group= BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
if (ELEM(NULL, ob, group))
return OPERATOR_CANCELLED;
add_to_group(group, ob, scene, NULL);
WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_group_link(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Link to Group";
ot->idname = "OBJECT_OT_group_link";
ot->description = "Add an object to an existing group";
/* api callbacks */
ot->exec = group_link_exec;
ot->invoke = WM_enum_search_invoke;
/* flags */
ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
/* properties */
prop= RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
RNA_def_enum_funcs(prop, RNA_group_local_itemf);
ot->prop = prop;
}
static int group_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
Scene *scene= CTX_data_scene(C);
Object *ob= ED_object_context(C);
Group *group= CTX_data_pointer_get_type(C, "group", &RNA_Group).data;
if (!ob || !group)
return OPERATOR_CANCELLED;
rem_from_group(group, ob, scene, NULL); /* base will be used if found */
WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_group_remove(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Remove Group";
ot->idname = "OBJECT_OT_group_remove";
/* api callbacks */
ot->exec = group_remove_exec;
/* flags */
ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
}
|