Update scripts to account for removal of the context override to bpy.ops
[blender-addons.git] / storypencil / scene_tools.py
blobbd621cedb4721f3e2cb306e9ed2c992012a89c49
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 import os
6 from bpy.types import (
7 Operator,
11 # -------------------------------------------------------------
12 # Add a new scene and set to new strip
14 # -------------------------------------------------------------
15 class STORYPENCIL_OT_NewScene(Operator):
16 bl_idname = "storypencil.new_scene"
17 bl_label = "New Scene"
18 bl_description = "Create a new scene base on template scene"
19 bl_options = {'REGISTER', 'UNDO'}
21 scene_name: bpy.props.StringProperty(default="Scene")
22 num_strips: bpy.props.IntProperty(default=1, min=1, max=128, description="Number of scenes to add")
24 # ------------------------------
25 # Poll
26 # ------------------------------
27 @classmethod
28 def poll(cls, context):
29 scene = context.scene
30 scene_base = scene.storypencil_base_scene
31 if scene_base is not None and scene_base.name in bpy.data.scenes:
32 return True
34 return False
36 def invoke(self, context, event):
37 return context.window_manager.invoke_props_dialog(self)
39 def draw(self, context):
40 layout = self.layout
41 col = layout.column()
42 col.prop(self, "scene_name", text="Scene Name")
43 col.prop(self, "num_strips", text="Repeat")
45 def format_to3(self, value):
46 return f"{value:03}"
48 # ------------------------------
49 # Execute button action
50 # ------------------------------
51 def execute(self, context):
52 scene_prv = context.scene
53 cfra_prv = scene_prv.frame_current
54 scene_base = scene_prv.storypencil_base_scene
55 repeat = self.num_strips
57 offset = 0
58 for i in range(repeat):
59 # Set context to base scene and duplicate
60 context.window.scene = scene_base
61 bpy.ops.scene.new(type='FULL_COPY')
62 scene_new = context.window.scene
63 new_name = scene_prv.storypencil_name_prefix + \
64 self.scene_name + scene_prv.storypencil_name_suffix
65 id = 0
66 while new_name in bpy.data.scenes:
67 id += 1
68 new_name = scene_prv.storypencil_name_prefix + self.scene_name + \
69 scene_prv.storypencil_name_suffix + '.' + self.format_to3(id)
71 scene_new.name = new_name
72 # Set duration of new scene
73 scene_new.frame_end = scene_new.frame_start + \
74 scene_prv.storypencil_scene_duration - 1
76 # Back to original scene
77 context.window.scene = scene_prv
78 scene_prv.frame_current = cfra_prv
79 bpy.ops.sequencer.scene_strip_add(
80 frame_start=cfra_prv + offset, scene=scene_new.name)
82 # Add offset for repeat
83 offset += scene_new.frame_end - scene_new.frame_start + 1
85 scene_new.update_tag()
86 scene_prv.update_tag()
88 return {"FINISHED"}
91 def draw_new_scene(self, context):
92 """Add menu options."""
94 self.layout.operator_context = 'INVOKE_REGION_WIN'
95 row = self.layout.row(align=True)
96 row.operator(STORYPENCIL_OT_NewScene.bl_idname, text="New Template Scene")
99 def setup_storyboard(self, context):
100 """Add Setup menu option."""
101 # For security, check if this is the default template.
102 is_gpencil = context.active_object and context.active_object.name == 'Stroke'
103 if is_gpencil and context.workspace.name in ('2D Animation', '2D Full Canvas') and context.scene.name == 'Scene':
104 if "Video Editing" not in bpy.data.workspaces:
105 row = self.layout.row(align=True)
106 row.separator()
107 row = self.layout.row(align=True)
108 row.operator(STORYPENCIL_OT_Setup.bl_idname,
109 text="Setup Storyboard Session")
112 # -------------------------------------------------------------
113 # Setup all environment
115 # -------------------------------------------------------------
116 class STORYPENCIL_OT_Setup(Operator):
117 bl_idname = "storypencil.setup"
118 bl_label = "Setup"
119 bl_description = "Configure all settings for a storyboard session"
120 bl_options = {'REGISTER', 'UNDO'}
122 # ------------------------------
123 # Poll
124 # ------------------------------
125 @classmethod
126 def poll(cls, context):
127 return True
129 def get_workspace(self, type):
130 for wrk in bpy.data.workspaces:
131 if wrk.name == type:
132 return wrk
134 return None
136 # ------------------------------
137 # Execute button action
138 # ------------------------------
139 def execute(self, context):
140 scene_base = context.scene
141 # Create Workspace
142 templatepath = None
143 if "Video Editing" not in bpy.data.workspaces:
144 template_path = None
145 for path in bpy.utils.app_template_paths():
146 template_path = path
148 filepath = os.path.join(
149 template_path, "Video_Editing", "startup.blend")
150 bpy.ops.workspace.append_activate(
151 idname="Video Editing", filepath=filepath)
152 # Create New scene
153 bpy.ops.scene.new()
154 scene_edit = context.scene
155 scene_edit.name = 'Edit'
156 # Rename original base scene
157 scene_base.name = 'Base'
158 # Setup Edit scene settings
159 scene_edit.storypencil_main_workspace = self.get_workspace(
160 "Video Editing")
161 scene_edit.storypencil_main_scene = scene_edit
162 scene_edit.storypencil_base_scene = scene_base
163 scene_edit.storypencil_edit_workspace = self.get_workspace(
164 "2D Animation")
166 # Add a new strip (need set the area context)
167 context.window.scene = scene_edit
168 area_prv = context.area.ui_type
169 context.area.ui_type = 'SEQUENCE_EDITOR'
170 prv_frame = scene_edit.frame_current
172 scene_edit.frame_current = scene_edit.frame_start
173 bpy.ops.storypencil.new_scene()
175 context.area.ui_type = area_prv
176 scene_edit.frame_current = prv_frame
178 scene_edit.update_tag()
179 bpy.ops.sequencer.reload()
181 return {"FINISHED"}