Blender  V3.3
scene_edit.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <stdio.h>
8 #include <string.h>
9 
10 #include "BLI_compiler_attrs.h"
11 #include "BLI_listbase.h"
12 #include "BLI_string.h"
13 
14 #include "DNA_sequence_types.h"
15 
16 #include "BKE_context.h"
17 #include "BKE_global.h"
18 #include "BKE_layer.h"
19 #include "BKE_lib_id.h"
20 #include "BKE_main.h"
21 #include "BKE_node.h"
22 #include "BKE_report.h"
23 #include "BKE_scene.h"
24 
25 #include "DEG_depsgraph.h"
26 #include "DEG_depsgraph_build.h"
27 
28 #include "BLT_translation.h"
29 
30 #include "ED_object.h"
31 #include "ED_render.h"
32 #include "ED_scene.h"
33 #include "ED_screen.h"
34 #include "ED_util.h"
35 
36 #include "SEQ_relations.h"
37 #include "SEQ_select.h"
38 
39 #include "RNA_access.h"
40 #include "RNA_define.h"
41 #include "RNA_enum_types.h"
42 
43 #include "WM_api.h"
44 #include "WM_types.h"
45 
46 /* -------------------------------------------------------------------- */
50 static Scene *scene_add(Main *bmain, Scene *scene_old, eSceneCopyMethod method)
51 {
52  Scene *scene_new = NULL;
53  if (method == SCE_COPY_NEW) {
54  scene_new = BKE_scene_add(bmain, DATA_("Scene"));
55  }
56  else { /* different kinds of copying */
57  /* We are going to deep-copy collections, objects and various object data, we need to have
58  * up-to-date obdata for that. */
59  if (method == SCE_COPY_FULL) {
61  }
62 
63  scene_new = BKE_scene_duplicate(bmain, scene_old, method);
64  }
65 
66  return scene_new;
67 }
68 
71  bContext *C,
72  eSceneCopyMethod method,
73  const bool assign_strip)
74 {
75  Sequence *seq = NULL;
76  Scene *scene_active = CTX_data_scene(C);
77  Scene *scene_strip = NULL;
78  /* Sequencer need to use as base the scene defined in the strip, not the main scene. */
79  Editing *ed = scene_active->ed;
80  if (ed) {
81  seq = ed->act_seq;
82  if (seq && seq->scene) {
83  scene_strip = seq->scene;
84  }
85  }
86 
87  /* If no scene assigned to the strip, only NEW scene mode is logic. */
88  if (scene_strip == NULL) {
89  method = SCE_COPY_NEW;
90  }
91 
92  Scene *scene_new = scene_add(bmain, scene_strip, method);
93 
94  /* If don't need assign the scene to the strip, nothing else to do. */
95  if (!assign_strip) {
96  return scene_new;
97  }
98 
99  /* As the scene is created in sequencer, do not set the new scene as active.
100  * This is useful for story-boarding where we want to keep actual scene active.
101  * The new scene is linked to the active strip and the viewport updated. */
102  if (scene_new && seq) {
103  seq->scene = scene_new;
104  /* Do a refresh of the sequencer data. */
105  SEQ_relations_invalidate_cache_raw(scene_active, seq);
108  }
109 
110  WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene_active);
112 
113  return scene_new;
114 }
115 
117 {
118  Scene *scene_old = WM_window_get_active_scene(win);
119  Scene *scene_new = scene_add(bmain, scene_old, method);
120 
121  WM_window_set_active_scene(bmain, C, win, scene_new);
122 
124 
125  return scene_new;
126 }
127 
129 {
130  Scene *scene_new;
131 
132  /* kill running jobs */
133  wmWindowManager *wm = bmain->wm.first;
135 
136  if (scene->id.prev) {
137  scene_new = scene->id.prev;
138  }
139  else if (scene->id.next) {
140  scene_new = scene->id.next;
141  }
142  else {
143  return false;
144  }
145 
146  LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
147  if (win->parent != NULL) { /* We only care about main windows here... */
148  continue;
149  }
150  if (win->scene == scene) {
151  WM_window_set_active_scene(bmain, C, win, scene_new);
152  }
153  }
154 
155  BKE_id_delete(bmain, scene);
156 
157  return true;
158 }
159 
161 {
163 
166  DEG_tag_on_visible_update(bmain, false);
167 
168  ED_render_engine_changed(bmain, false);
170 }
171 
172 static bool view_layer_remove_poll(const Scene *scene, const ViewLayer *layer)
173 {
174  const int act = BLI_findindex(&scene->view_layers, layer);
175 
176  if (act == -1) {
177  return false;
178  }
180  (scene->view_layers.first == layer)) {
181  /* ensure 1 layer is kept */
182  return false;
183  }
184 
185  return true;
186 }
187 
188 static void view_layer_remove_unset_nodetrees(const Main *bmain, Scene *scene, ViewLayer *layer)
189 {
190  int act_layer_index = BLI_findindex(&scene->view_layers, layer);
191 
192  for (Scene *sce = bmain->scenes.first; sce; sce = sce->id.next) {
193  if (sce->nodetree) {
194  BKE_nodetree_remove_layer_n(sce->nodetree, scene, act_layer_index);
195  }
196  }
197 }
198 
200 {
201  if (view_layer_remove_poll(scene, layer) == false) {
202  if (reports) {
203  BKE_reportf(reports,
204  RPT_ERROR,
205  "View layer '%s' could not be removed from scene '%s'",
206  layer->name,
207  scene->id.name + 2);
208  }
209 
210  return false;
211  }
212 
213  /* We need to unset nodetrees before removing the layer, otherwise its index will be -1. */
215 
216  BLI_remlink(&scene->view_layers, layer);
218 
219  /* Remove from windows. */
220  wmWindowManager *wm = bmain->wm.first;
221  LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
222  if (win->scene == scene && STREQ(win->view_layer_name, layer->name)) {
224  STRNCPY(win->view_layer_name, first_layer->name);
225  }
226  }
227 
229 
230  BKE_view_layer_free(layer);
231 
232  DEG_id_tag_update(&scene->id, 0);
235 
236  return true;
237 }
238 
241 /* -------------------------------------------------------------------- */
246 {
247  Main *bmain = CTX_data_main(C);
248  wmWindow *win = CTX_wm_window(C);
249  int type = RNA_enum_get(op->ptr, "type");
250 
251  ED_scene_add(bmain, C, win, type);
252 
253  return OPERATOR_FINISHED;
254 }
255 
257  {SCE_COPY_NEW, "NEW", 0, "New", "Add a new, empty scene with default settings"},
259  "EMPTY",
260  0,
261  "Copy Settings",
262  "Add a new, empty scene, and copy settings from the current scene"},
264  "LINK_COPY",
265  0,
266  "Linked Copy",
267  "Link in the collections from the current scene (shallow copy)"},
268  {SCE_COPY_FULL, "FULL_COPY", 0, "Full Copy", "Make a full copy of the current scene"},
269  {0, NULL, 0, NULL, NULL},
270 };
271 
273 {
274 
275  /* identifiers */
276  ot->name = "New Scene";
277  ot->description = "Add new scene by type";
278  ot->idname = "SCENE_OT_new";
279 
280  /* api callbacks */
283 
284  /* flags */
286 
287  /* properties */
288  ot->prop = RNA_def_enum(ot->srna, "type", scene_new_items, SCE_COPY_NEW, "Type", "");
289 }
290 
293 /* -------------------------------------------------------------------- */
298 {
299  Main *bmain = CTX_data_main(C);
300  int type = RNA_enum_get(op->ptr, "type");
301 
302  if (ED_scene_sequencer_add(bmain, C, type, true) == NULL) {
303  return OPERATOR_CANCELLED;
304  }
305 
306  return OPERATOR_FINISHED;
307 }
308 
310 {
312  const Sequence *seq = SEQ_select_active_get(scene);
313  return (seq && (seq->type == SEQ_TYPE_SCENE));
314 }
315 
318  PropertyRNA *UNUSED(prop),
319  bool *r_free)
320 {
321  EnumPropertyItem *item = NULL;
322  int totitem = 0;
323  uint item_index;
324 
326  RNA_enum_item_add(&item, &totitem, &scene_new_items[item_index]);
327 
328  bool has_scene_or_no_context = false;
329  if (C == NULL) {
330  /* For documentation generation. */
331  has_scene_or_no_context = true;
332  }
333  else {
336  if ((seq && (seq->type == SEQ_TYPE_SCENE) && (seq->scene != NULL))) {
337  has_scene_or_no_context = true;
338  }
339  }
340 
341  if (has_scene_or_no_context) {
343  for (int i = 0; i < ARRAY_SIZE(values); i++) {
344  item_index = RNA_enum_from_value(scene_new_items, values[i]);
345  RNA_enum_item_add(&item, &totitem, &scene_new_items[item_index]);
346  }
347  }
348 
349  RNA_enum_item_end(&item, &totitem);
350  *r_free = true;
351  return item;
352 }
353 
355 {
356 
357  /* identifiers */
358  ot->name = "New Scene";
359  ot->description = "Add new scene by type in the sequence editor and assign to active strip";
360  ot->idname = "SCENE_OT_new_sequencer";
361 
362  /* api callbacks */
366 
367  /* flags */
369 
370  /* properties */
371  ot->prop = RNA_def_enum(ot->srna, "type", scene_new_items, SCE_COPY_NEW, "Type", "");
374 }
375 
378 /* -------------------------------------------------------------------- */
383 {
384  Main *bmain = CTX_data_main(C);
386  return BKE_scene_can_be_removed(bmain, scene);
387 }
388 
390 {
392 
393  if (ED_scene_delete(C, CTX_data_main(C), scene) == false) {
394  return OPERATOR_CANCELLED;
395  }
396 
397  if (G.debug & G_DEBUG) {
398  printf("scene delete %p\n", scene);
399  }
400 
402 
403  return OPERATOR_FINISHED;
404 }
405 
407 {
408  /* identifiers */
409  ot->name = "Delete Scene";
410  ot->description = "Delete active scene";
411  ot->idname = "SCENE_OT_delete";
412 
413  /* api callbacks */
416 
417  /* flags */
419 }
420 
423 /* -------------------------------------------------------------------- */
428 {
432 }
433 
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:723
@ G_DEBUG
Definition: BKE_global.h:174
void BKE_view_layer_free(struct ViewLayer *view_layer)
Definition: layer.c:239
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, int layer_index)
Definition: node.cc:5067
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
eSceneCopyMethod
Definition: BKE_scene.h:28
@ SCE_COPY_EMPTY
Definition: BKE_scene.h:30
@ SCE_COPY_NEW
Definition: BKE_scene.h:29
@ SCE_COPY_FULL
Definition: BKE_scene.h:32
@ SCE_COPY_LINK_COLLECTION
Definition: BKE_scene.h:31
void BKE_scene_set_background(struct Main *bmain, struct Scene *sce)
Definition: scene.cc:2075
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.cc:1864
void BKE_scene_free_view_layer_depsgraph(struct Scene *scene, struct ViewLayer *view_layer)
Definition: scene.cc:3361
struct Scene * BKE_scene_add(struct Main *bmain, const char *name)
Definition: scene.cc:2044
struct Depsgraph * BKE_scene_ensure_depsgraph(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer)
Definition: scene.cc:3456
#define BLI_assert(a)
Definition: BLI_assert.h:46
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:269
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
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:483
unsigned int uint
Definition: BLI_sys_types.h:67
#define ARRAY_SIZE(arr)
#define UNUSED(x)
#define STREQ(a, b)
#define DATA_(msgid)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
void DEG_tag_on_visible_update(struct Main *bmain, bool do_time)
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)
@ ID_RECALC_AUDIO
Definition: DNA_ID.h:848
@ ID_RECALC_SEQUENCER_STRIPS
Definition: DNA_ID.h:838
@ SEQ_TYPE_SCENE
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
void ED_render_engine_changed(struct Main *bmain, bool update_scene_data)
void ED_update_for_newframe(struct Main *bmain, struct Depsgraph *depsgraph)
Definition: screen_edit.c:1694
bool ED_editors_flush_edits(struct Main *bmain)
Definition: ed_util.c:325
_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
@ PROP_ENUM_NO_TRANSLATE
Definition: RNA_types.h:294
#define C
Definition: RandGen.cpp:25
@ WM_JOB_TYPE_ANY
Definition: WM_api.h:1347
#define ND_SEQUENCER
Definition: WM_types.h:385
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
#define NC_SCENE
Definition: WM_types.h:328
#define NA_REMOVED
Definition: WM_types.h:526
#define ND_LAYER
Definition: WM_types.h:398
#define ND_SCENEBROWSE
Definition: WM_types.h:380
Scene scene
const Depsgraph * depsgraph
#define G(x, y, z)
int RNA_enum_from_value(const EnumPropertyItem *item, const int value)
Definition: rna_access.c:1736
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
Definition: rna_define.c:4487
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
Definition: rna_define.c:4436
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1490
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
Definition: rna_define.c:3830
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:3783
Scene * ED_scene_add(Main *bmain, bContext *C, wmWindow *win, eSceneCopyMethod method)
Definition: scene_edit.c:116
static int scene_delete_exec(bContext *C, wmOperator *UNUSED(op))
Definition: scene_edit.c:389
static void view_layer_remove_unset_nodetrees(const Main *bmain, Scene *scene, ViewLayer *layer)
Definition: scene_edit.c:188
bool ED_scene_delete(bContext *C, Main *bmain, Scene *scene)
Definition: scene_edit.c:128
static void SCENE_OT_new(wmOperatorType *ot)
Definition: scene_edit.c:272
static void SCENE_OT_new_sequencer(wmOperatorType *ot)
Definition: scene_edit.c:354
static int scene_new_sequencer_exec(bContext *C, wmOperator *op)
Definition: scene_edit.c:297
void ED_operatortypes_scene(void)
Definition: scene_edit.c:427
bool ED_scene_view_layer_delete(Main *bmain, Scene *scene, ViewLayer *layer, ReportList *reports)
Definition: scene_edit.c:199
static int scene_new_exec(bContext *C, wmOperator *op)
Definition: scene_edit.c:245
static bool scene_delete_poll(bContext *C)
Definition: scene_edit.c:382
static bool scene_new_sequencer_poll(bContext *C)
Definition: scene_edit.c:309
Scene * ED_scene_sequencer_add(Main *bmain, bContext *C, eSceneCopyMethod method, const bool assign_strip)
Definition: scene_edit.c:70
static Scene * scene_add(Main *bmain, Scene *scene_old, eSceneCopyMethod method)
Definition: scene_edit.c:50
static bool view_layer_remove_poll(const Scene *scene, const ViewLayer *layer)
Definition: scene_edit.c:172
static void SCENE_OT_delete(wmOperatorType *ot)
Definition: scene_edit.c:406
static const EnumPropertyItem * scene_new_sequencer_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
Definition: scene_edit.c:316
void ED_scene_change_update(Main *bmain, Scene *scene, ViewLayer *layer)
Definition: scene_edit.c:160
static EnumPropertyItem scene_new_items[]
Definition: scene_edit.c:256
void SEQ_relations_invalidate_cache_raw(Scene *scene, Sequence *seq)
Sequence * SEQ_select_active_get(Scene *scene)
Definition: strip_select.c:18
Sequence * act_seq
void * prev
Definition: DNA_ID.h:369
void * next
Definition: DNA_ID.h:369
char name[66]
Definition: DNA_ID.h:378
void * last
Definition: DNA_listBase.h:31
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
ListBase scenes
Definition: BKE_main.h:168
ListBase wm
Definition: BKE_main.h:197
struct Editing * ed
ListBase view_layers
struct Scene * scene
char name[64]
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:919
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
PropertyRNA * prop
Definition: WM_types.h:981
struct PointerRNA * ptr
void WM_main_add_notifier(unsigned int type, void *reference)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
PointerRNA * ptr
Definition: wm_files.c:3480
wmOperatorType * ot
Definition: wm_files.c:3479
void WM_jobs_kill_type(struct wmWindowManager *wm, const 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))
void WM_window_set_active_scene(Main *bmain, bContext *C, wmWindow *win, Scene *scene)
Definition: wm_window.c:2188
Scene * WM_window_get_active_scene(const wmWindow *win)
Definition: wm_window.c:2183