4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) Blender Foundation
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
32 #include "MEM_guardedalloc.h"
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
37 #include "DNA_group_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_view3d_types.h"
42 #include "BKE_depsgraph.h"
43 #include "BKE_group.h"
44 #include "BKE_global.h"
45 #include "BKE_context.h"
47 #include "BKE_report.h"
49 #include "ED_view3d.h"
50 #include "ED_space_api.h"
51 #include "ED_screen.h"
55 #include "UI_interface.h"
56 #include "UI_resources.h"
61 #include "RNA_access.h"
62 #include "RNA_define.h"
64 #include "object_intern.h"
66 static int objects_add_active_exec(bContext *C, wmOperator *op)
68 Scene *scene= CTX_data_scene(C);
69 Object *ob= OBACT, *obt;
73 if (!ob) return OPERATOR_CANCELLED;
75 /* linking to same group requires its own loop so we can avoid
76 looking up the active objects groups each time */
78 group= G.main->group.first;
80 if(object_in_group(ob, group)) {
81 /* Assign groups to selected objects */
82 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
84 add_to_group(group, obt);
85 obt->flag |= OB_FROMGROUP;
86 base->flag |= OB_FROMGROUP;
87 base->object->recalc= OB_RECALC_OB;
92 group= group->id.next;
95 if (!ok) BKE_report(op->reports, RPT_ERROR, "Active Object contains no groups");
97 DAG_scene_sort(CTX_data_scene(C));
99 WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL);
101 return OPERATOR_FINISHED;
105 void GROUP_OT_objects_add_active(wmOperatorType *ot)
109 ot->name= "Add Selected To Active Group";
110 ot->description = "Add the object to an object group that contains the active object.";
111 ot->idname= "GROUP_OT_objects_add_active";
114 ot->exec= objects_add_active_exec;
115 ot->poll= ED_operator_scene_editable;
118 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
121 static int objects_remove_active_exec(bContext *C, wmOperator *op)
123 Scene *scene= CTX_data_scene(C);
124 Object *ob= OBACT, *obt;
128 if (!ob) return OPERATOR_CANCELLED;
130 /* linking to same group requires its own loop so we can avoid
131 looking up the active objects groups each time */
133 group= G.main->group.first;
135 if(object_in_group(ob, group)) {
136 /* Assign groups to selected objects */
137 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
139 rem_from_group(group, obt);
140 obt->flag &= ~OB_FROMGROUP;
141 base->flag &= ~OB_FROMGROUP;
142 base->object->recalc= OB_RECALC_OB;
147 group= group->id.next;
150 if (!ok) BKE_report(op->reports, RPT_ERROR, "Active Object contains no groups");
152 DAG_scene_sort(CTX_data_scene(C));
154 WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL);
156 return OPERATOR_FINISHED;
160 void GROUP_OT_objects_remove_active(wmOperatorType *ot)
164 ot->name= "Remove Selected From Active Group";
165 ot->description = "Remove the object from an object group that contains the active object.";
166 ot->idname= "GROUP_OT_objects_remove_active";
169 ot->exec= objects_remove_active_exec;
170 ot->poll= ED_operator_scene_editable;
173 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
176 static int group_remove_exec(bContext *C, wmOperator *op)
180 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
182 while( (group = find_group(base->object, group)) ) {
183 rem_from_group(group, base->object);
185 base->object->flag &= ~OB_FROMGROUP;
186 base->flag &= ~OB_FROMGROUP;
187 base->object->recalc= OB_RECALC_OB;
191 DAG_scene_sort(CTX_data_scene(C));
193 WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL);
195 return OPERATOR_FINISHED;
199 void GROUP_OT_objects_remove(wmOperatorType *ot)
203 ot->name= "Remove From Groups";
204 ot->description = "Remove selected objects from all groups.";
205 ot->idname= "GROUP_OT_objects_remove";
208 ot->exec= group_remove_exec;
209 ot->poll= ED_operator_scene_editable;
212 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
215 static int group_create_exec(bContext *C, wmOperator *op)
218 char gid[32]; //group id
220 RNA_string_get(op->ptr, "GID", gid);
222 group= add_group(gid);
224 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
225 add_to_group(group, base->object);
226 base->object->flag |= OB_FROMGROUP;
227 base->flag |= OB_FROMGROUP;
228 base->object->recalc= OB_RECALC_OB;
232 DAG_scene_sort(CTX_data_scene(C));
234 WM_event_add_notifier(C, NC_GROUP|NA_EDITED, NULL);
236 return OPERATOR_FINISHED;
240 void GROUP_OT_group_create(wmOperatorType *ot)
244 ot->name= "Create New Group";
245 ot->description = "Create an object group.";
246 ot->idname= "GROUP_OT_group_create";
249 ot->exec= group_create_exec;
250 ot->poll= ED_operator_scene_editable;
253 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
255 RNA_def_string(ot->srna, "GID", "Group", 32, "Name", "Name of the new group");