Rename any instance of scene layer or render layer in code with view layer
[blender.git] / source / blender / editors / scene / scene_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/scene/scene_edit.c
22  *  \ingroup edscene
23  */
24
25 #include <stdio.h>
26
27 #include "BLI_compiler_attrs.h"
28 #include "BLI_listbase.h"
29
30 #include "BKE_context.h"
31 #include "BKE_global.h"
32 #include "BKE_layer.h"
33 #include "BKE_library_remap.h"
34 #include "BKE_main.h"
35 #include "BKE_node.h"
36 #include "BKE_report.h"
37 #include "BKE_scene.h"
38 #include "BKE_workspace.h"
39
40 #include "DEG_depsgraph.h"
41 #include "DEG_depsgraph_build.h"
42
43 #include "BLT_translation.h"
44
45 #include "DNA_workspace_types.h"
46
47 #include "ED_object.h"
48 #include "ED_render.h"
49 #include "ED_scene.h"
50 #include "ED_screen.h"
51 #include "ED_util.h"
52
53 #include "RNA_access.h"
54 #include "RNA_define.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59
60 Scene *ED_scene_add(Main *bmain, bContext *C, wmWindow *win, eSceneCopyMethod method)
61 {
62         Scene *scene_new;
63
64         if (method == SCE_COPY_NEW) {
65                 scene_new = BKE_scene_add(bmain, DATA_("Scene"));
66         }
67         else { /* different kinds of copying */
68                 Scene *scene_old = WM_window_get_active_scene(win);
69
70                 scene_new = BKE_scene_copy(bmain, scene_old, method);
71
72                 /* these can't be handled in blenkernel currently, so do them here */
73                 if (method == SCE_COPY_LINK_DATA) {
74                         ED_object_single_users(bmain, scene_new, false, true);
75                 }
76                 else if (method == SCE_COPY_FULL) {
77                         ED_editors_flush_edits(C, false);
78                         ED_object_single_users(bmain, scene_new, true, true);
79                 }
80         }
81
82         WM_window_change_active_scene(bmain, C, win, scene_new);
83
84         WM_event_add_notifier(C, NC_SCENE | ND_SCENEBROWSE, scene_new);
85
86         return scene_new;
87 }
88
89 /**
90  * \note Only call outside of area/region loops
91  * \return true if successful
92  */
93 bool ED_scene_delete(bContext *C, Main *bmain, wmWindow *win, Scene *scene)
94 {
95         Scene *scene_new;
96
97         if (scene->id.prev)
98                 scene_new = scene->id.prev;
99         else if (scene->id.next)
100                 scene_new = scene->id.next;
101         else
102                 return false;
103
104         WM_window_change_active_scene(bmain, C, win, scene_new);
105
106         BKE_libblock_remap(bmain, scene, scene_new, ID_REMAP_SKIP_INDIRECT_USAGE | ID_REMAP_SKIP_NEVER_NULL_USAGE);
107
108         id_us_clear_real(&scene->id);
109         if (scene->id.us == 0) {
110                 BKE_libblock_free(bmain, scene);
111         }
112
113         return true;
114 }
115
116 void ED_scene_exit(bContext *C)
117 {
118         ED_object_editmode_exit(C, EM_FREEDATA | EM_DO_UNDO);
119 }
120
121 void ED_scene_changed_update(Main *bmain, bContext *C, Scene *scene_new, const bScreen *active_screen)
122 {
123         /* XXX Just using active scene render-layer for workspace when switching,
124          * but workspace should remember the last one set. Could store render-layer
125          * per window-workspace combination (using WorkSpaceDataRelation) */
126         ViewLayer *layer_new = BLI_findlink(&scene_new->view_layers, scene_new->active_view_layer);
127         Depsgraph *depsgraph = BKE_scene_get_depsgraph(scene_new, layer_new, true);
128
129         CTX_data_scene_set(C, scene_new);
130         BKE_workspace_view_layer_set(CTX_wm_workspace(C), layer_new);
131         BKE_scene_set_background(bmain, scene_new);
132         DEG_graph_relations_update(depsgraph, bmain, scene_new, layer_new);
133         DEG_on_visible_update(bmain, false);
134
135         ED_screen_update_after_scene_change(active_screen, scene_new, layer_new);
136         ED_render_engine_changed(bmain);
137         ED_update_for_newframe(bmain, scene_new, layer_new, depsgraph);
138
139         /* complete redraw */
140         WM_event_add_notifier(C, NC_WINDOW, NULL);
141 }
142
143 static bool view_layer_remove_poll(
144         const Scene *scene, const ViewLayer *layer)
145 {
146         const int act = BLI_findindex(&scene->view_layers, layer);
147
148         if (act == -1) {
149                 return false;
150         }
151         else if ((scene->view_layers.first == scene->view_layers.last) &&
152                  (scene->view_layers.first == layer))
153         {
154                 /* ensure 1 layer is kept */
155                 return false;
156         }
157
158         return true;
159 }
160
161 static void view_layer_remove_unset_nodetrees(const Main *bmain, Scene *scene, ViewLayer *layer)
162 {
163         int act_layer_index = BLI_findindex(&scene->view_layers, layer);
164
165         for (Scene *sce = bmain->scene.first; sce; sce = sce->id.next) {
166                 if (sce->nodetree) {
167                         BKE_nodetree_remove_layer_n(sce->nodetree, scene, act_layer_index);
168                 }
169         }
170 }
171
172 bool ED_scene_view_layer_delete(
173         Main *bmain, Scene *scene, ViewLayer *layer,
174         ReportList *reports)
175 {
176         if (view_layer_remove_poll(scene, layer) == false) {
177                 if (reports) {
178                         BKE_reportf(reports, RPT_ERROR, "View layer '%s' could not be removed from scene '%s'",
179                                     layer->name, scene->id.name + 2);
180                 }
181
182                 return false;
183         }
184
185         BLI_remlink(&scene->view_layers, layer);
186         BLI_assert(BLI_listbase_is_empty(&scene->view_layers) == false);
187         scene->active_view_layer = 0;
188
189         ED_workspace_view_layer_unset(bmain, layer, scene->view_layers.first);
190         view_layer_remove_unset_nodetrees(bmain, scene, layer);
191
192         BKE_view_layer_free(layer);
193
194         DEG_id_tag_update(&scene->id, 0);
195         DEG_relations_tag_update(bmain);
196         WM_main_add_notifier(NC_SCENE | ND_LAYER | NA_REMOVED, scene);
197
198         return true;
199 }
200
201 static int scene_new_exec(bContext *C, wmOperator *op)
202 {
203         Main *bmain = CTX_data_main(C);
204         wmWindow *win = CTX_wm_window(C);
205         int type = RNA_enum_get(op->ptr, "type");
206
207         ED_scene_add(bmain, C, win, type);
208
209         return OPERATOR_FINISHED;
210 }
211
212 static void SCENE_OT_new(wmOperatorType *ot)
213 {
214         static EnumPropertyItem type_items[] = {
215                 {SCE_COPY_NEW, "NEW", 0, "New", "Add new scene"},
216                 {SCE_COPY_EMPTY, "EMPTY", 0, "Copy Settings", "Make a copy without any objects"},
217                 {SCE_COPY_LINK_OB, "LINK_OBJECTS", 0, "Link Objects", "Link to the objects from the current scene"},
218                 {SCE_COPY_LINK_DATA, "LINK_OBJECT_DATA", 0, "Link Object Data", "Copy objects linked to data from the current scene"},
219                 {SCE_COPY_FULL, "FULL_COPY", 0, "Full Copy", "Make a full copy of the current scene"},
220                 {0, NULL, 0, NULL, NULL}
221         };
222
223         /* identifiers */
224         ot->name = "New Scene";
225         ot->description = "Add new scene by type";
226         ot->idname = "SCENE_OT_new";
227
228         /* api callbacks */
229         ot->exec = scene_new_exec;
230         ot->invoke = WM_menu_invoke;
231
232         /* flags */
233         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
234
235         /* properties */
236         ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
237 }
238
239 static int scene_delete_exec(bContext *C, wmOperator *UNUSED(op))
240 {
241         Scene *scene = CTX_data_scene(C);
242
243         if (ED_scene_delete(C, CTX_data_main(C), CTX_wm_window(C), scene) == false) {
244                 return OPERATOR_CANCELLED;
245         }
246
247         if (G.debug & G_DEBUG)
248                 printf("scene delete %p\n", scene);
249
250         WM_event_add_notifier(C, NC_SCENE | NA_REMOVED, scene);
251
252         return OPERATOR_FINISHED;
253 }
254
255 static void SCENE_OT_delete(wmOperatorType *ot)
256 {
257         /* identifiers */
258         ot->name = "Delete Scene";
259         ot->description = "Delete active scene";
260         ot->idname = "SCENE_OT_delete";
261
262         /* api callbacks */
263         ot->exec = scene_delete_exec;
264
265         /* flags */
266         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
267 }
268
269 void ED_operatortypes_scene(void)
270 {
271         WM_operatortype_append(SCENE_OT_new);
272         WM_operatortype_append(SCENE_OT_delete);
273 }