Blender  V2.93
scene_edit.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "BLI_compiler_attrs.h"
25 #include "BLI_listbase.h"
26 #include "BLI_string.h"
27 
28 #include "BKE_context.h"
29 #include "BKE_global.h"
30 #include "BKE_layer.h"
31 #include "BKE_lib_id.h"
32 #include "BKE_main.h"
33 #include "BKE_node.h"
34 #include "BKE_report.h"
35 #include "BKE_scene.h"
36 
37 #include "DEG_depsgraph.h"
38 #include "DEG_depsgraph_build.h"
39 
40 #include "BLT_translation.h"
41 
42 #include "ED_object.h"
43 #include "ED_render.h"
44 #include "ED_scene.h"
45 #include "ED_screen.h"
46 #include "ED_util.h"
47 
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50 
51 #include "WM_api.h"
52 #include "WM_types.h"
53 
55 {
56  Scene *scene_new;
57 
58  if (method == SCE_COPY_NEW) {
59  scene_new = BKE_scene_add(bmain, DATA_("Scene"));
60  }
61  else { /* different kinds of copying */
62  Scene *scene_old = WM_window_get_active_scene(win);
63 
64  /* We are going to deep-copy collections, objects and various object data, we need to have
65  * up-to-date obdata for that. */
66  if (method == SCE_COPY_FULL) {
68  }
69 
70  scene_new = BKE_scene_duplicate(bmain, scene_old, method);
71  }
72 
73  WM_window_set_active_scene(bmain, C, win, scene_new);
74 
76 
77  return scene_new;
78 }
79 
85 {
86  Scene *scene_new;
87 
88  /* kill running jobs */
89  wmWindowManager *wm = bmain->wm.first;
91 
92  if (scene->id.prev) {
93  scene_new = scene->id.prev;
94  }
95  else if (scene->id.next) {
96  scene_new = scene->id.next;
97  }
98  else {
99  return false;
100  }
101 
102  LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
103  if (win->parent != NULL) { /* We only care about main windows here... */
104  continue;
105  }
106  if (win->scene == scene) {
107  WM_window_set_active_scene(bmain, C, win, scene_new);
108  }
109  }
110 
111  BKE_id_delete(bmain, scene);
112 
113  return true;
114 }
115 
116 /* Depsgraph updates after scene becomes active in a window. */
118 {
120 
123  DEG_on_visible_update(bmain, false);
124 
125  ED_render_engine_changed(bmain, false);
127 }
128 
129 static bool view_layer_remove_poll(const Scene *scene, const ViewLayer *layer)
130 {
131  const int act = BLI_findindex(&scene->view_layers, layer);
132 
133  if (act == -1) {
134  return false;
135  }
137  (scene->view_layers.first == layer)) {
138  /* ensure 1 layer is kept */
139  return false;
140  }
141 
142  return true;
143 }
144 
145 static void view_layer_remove_unset_nodetrees(const Main *bmain, Scene *scene, ViewLayer *layer)
146 {
147  int act_layer_index = BLI_findindex(&scene->view_layers, layer);
148 
149  for (Scene *sce = bmain->scenes.first; sce; sce = sce->id.next) {
150  if (sce->nodetree) {
151  BKE_nodetree_remove_layer_n(sce->nodetree, scene, act_layer_index);
152  }
153  }
154 }
155 
157 {
158  if (view_layer_remove_poll(scene, layer) == false) {
159  if (reports) {
160  BKE_reportf(reports,
161  RPT_ERROR,
162  "View layer '%s' could not be removed from scene '%s'",
163  layer->name,
164  scene->id.name + 2);
165  }
166 
167  return false;
168  }
169 
170  /* We need to unset nodetrees before removing the layer, otherwise its index will be -1. */
172 
173  BLI_remlink(&scene->view_layers, layer);
175 
176  /* Remove from windows. */
177  wmWindowManager *wm = bmain->wm.first;
178  LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
179  if (win->scene == scene && STREQ(win->view_layer_name, layer->name)) {
181  STRNCPY(win->view_layer_name, first_layer->name);
182  }
183  }
184 
186 
187  BKE_view_layer_free(layer);
188 
189  DEG_id_tag_update(&scene->id, 0);
192 
193  return true;
194 }
195 
197 {
198  Main *bmain = CTX_data_main(C);
199  wmWindow *win = CTX_wm_window(C);
200  int type = RNA_enum_get(op->ptr, "type");
201 
202  ED_scene_add(bmain, C, win, type);
203 
204  return OPERATOR_FINISHED;
205 }
206 
208 {
209  static EnumPropertyItem type_items[] = {
210  {SCE_COPY_NEW, "NEW", 0, "New", "Add a new, empty scene with default settings"},
212  "EMPTY",
213  0,
214  "Copy Settings",
215  "Add a new, empty scene, and copy settings from the current scene"},
217  "LINK_COPY",
218  0,
219  "Linked Copy",
220  "Link in the collections from the current scene (shallow copy)"},
221  {SCE_COPY_FULL, "FULL_COPY", 0, "Full Copy", "Make a full copy of the current scene"},
222  {0, NULL, 0, NULL, NULL},
223  };
224 
225  /* identifiers */
226  ot->name = "New Scene";
227  ot->description = "Add new scene by type";
228  ot->idname = "SCENE_OT_new";
229 
230  /* api callbacks */
233 
234  /* flags */
236 
237  /* properties */
238  ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
239 }
240 
242 {
243  Main *bmain = CTX_data_main(C);
245  return BKE_scene_can_be_removed(bmain, scene);
246 }
247 
249 {
251 
252  if (ED_scene_delete(C, CTX_data_main(C), scene) == false) {
253  return OPERATOR_CANCELLED;
254  }
255 
256  if (G.debug & G_DEBUG) {
257  printf("scene delete %p\n", scene);
258  }
259 
261 
262  return OPERATOR_FINISHED;
263 }
264 
266 {
267  /* identifiers */
268  ot->name = "Delete Scene";
269  ot->description = "Delete active scene";
270  ot->idname = "SCENE_OT_delete";
271 
272  /* api callbacks */
275 
276  /* flags */
278 }
279 
281 {
284 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:699
@ G_DEBUG
Definition: BKE_global.h:133
void BKE_view_layer_free(struct ViewLayer *view_layer)
Definition: layer.c:254
struct ViewLayer * BKE_view_layer_default_view(const struct Scene *scene)
void BKE_id_delete(struct Main *bmain, void *idv) ATTR_NONNULL()
void BKE_nodetree_remove_layer_n(struct bNodeTree *ntree, struct Scene *scene, const int layer_index)
Definition: node.cc:5140
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
eSceneCopyMethod
Definition: BKE_scene.h:42
@ SCE_COPY_EMPTY
Definition: BKE_scene.h:44
@ SCE_COPY_NEW
Definition: BKE_scene.h:43
@ SCE_COPY_FULL
Definition: BKE_scene.h:46
@ SCE_COPY_LINK_COLLECTION
Definition: BKE_scene.h:45
void BKE_scene_set_background(struct Main *bmain, struct Scene *sce)
Definition: scene.c:2120
bool BKE_scene_can_be_removed(const struct Main *bmain, const struct Scene *scene)
struct Scene * BKE_scene_duplicate(struct Main *bmain, struct Scene *sce, eSceneCopyMethod type)
Definition: scene.c:1908
void BKE_scene_free_view_layer_depsgraph(struct Scene *scene, struct ViewLayer *view_layer)
Definition: scene.c:3431
struct Scene * BKE_scene_add(struct Main *bmain, const char *name)
Definition: scene.c:2078
struct Depsgraph * BKE_scene_ensure_depsgraph(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer)
Definition: scene.c:3526
#define BLI_assert(a)
Definition: BLI_assert.h:58
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define STRNCPY(dst, src)
Definition: BLI_string.h:163
#define UNUSED(x)
#define STREQ(a, b)
#define DATA_(msgid)
void DEG_on_visible_update(struct Main *bmain, const bool do_time)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
void DEG_id_tag_update(struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
void DEG_graph_relations_update(struct Depsgraph *graph)
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
void ED_render_engine_changed(struct Main *bmain, const bool update_scene_data)
void ED_update_for_newframe(struct Main *bmain, struct Depsgraph *depsgraph)
Definition: screen_edit.c:1617
bool ED_editors_flush_edits(struct Main *bmain)
Definition: ed_util.c:295
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
#define C
Definition: RandGen.cpp:39
@ WM_JOB_TYPE_ANY
Definition: WM_api.h:734
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define NC_SCENE
Definition: WM_types.h:279
#define NA_REMOVED
Definition: WM_types.h:465
#define ND_LAYER
Definition: WM_types.h:350
#define ND_SCENEBROWSE
Definition: WM_types.h:332
Scene scene
const Depsgraph * depsgraph
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3771
Scene * ED_scene_add(Main *bmain, bContext *C, wmWindow *win, eSceneCopyMethod method)
Definition: scene_edit.c:54
static int scene_delete_exec(bContext *C, wmOperator *UNUSED(op))
Definition: scene_edit.c:248
static void view_layer_remove_unset_nodetrees(const Main *bmain, Scene *scene, ViewLayer *layer)
Definition: scene_edit.c:145
bool ED_scene_delete(bContext *C, Main *bmain, Scene *scene)
Definition: scene_edit.c:84
static void SCENE_OT_new(wmOperatorType *ot)
Definition: scene_edit.c:207
void ED_operatortypes_scene(void)
Definition: scene_edit.c:280
bool ED_scene_view_layer_delete(Main *bmain, Scene *scene, ViewLayer *layer, ReportList *reports)
Definition: scene_edit.c:156
static int scene_new_exec(bContext *C, wmOperator *op)
Definition: scene_edit.c:196
static bool scene_delete_poll(bContext *C)
Definition: scene_edit.c:241
static bool view_layer_remove_poll(const Scene *scene, const ViewLayer *layer)
Definition: scene_edit.c:129
static void SCENE_OT_delete(wmOperatorType *ot)
Definition: scene_edit.c:265
void ED_scene_change_update(Main *bmain, Scene *scene, ViewLayer *layer)
Definition: scene_edit.c:117
void * prev
Definition: DNA_ID.h:274
void * next
Definition: DNA_ID.h:274
char name[66]
Definition: DNA_ID.h:283
void * last
Definition: DNA_listBase.h:47
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase scenes
Definition: BKE_main.h:146
ListBase wm
Definition: BKE_main.h:175
ListBase view_layers
char name[64]
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
PropertyRNA * prop
Definition: WM_types.h:814
struct PointerRNA * ptr
#define G(x, y, z)
void WM_main_add_notifier(unsigned int type, void *reference)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_jobs_kill_type(struct wmWindowManager *wm, void *owner, int job_type)
Definition: wm_jobs.c:572
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
Definition: wm_operators.c:982
void WM_window_set_active_scene(Main *bmain, bContext *C, wmWindow *win, Scene *scene)
Definition: wm_window.c:2257
Scene * WM_window_get_active_scene(const wmWindow *win)
Definition: wm_window.c:2249