Skinify: fix shape generation
[blender-addons.git] / storypencil / scene_tools.py
blobd3bf037d13ca0d896439a5ca3b2197da25eec58a
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")
23 # ------------------------------
24 # Poll
25 # ------------------------------
26 @classmethod
27 def poll(cls, context):
28 scene = context.scene
29 scene_base = scene.storypencil_base_scene
30 if scene_base is not None and scene_base.name in bpy.data.scenes:
31 return True
33 return False
35 def invoke(self, context, event):
36 return context.window_manager.invoke_props_dialog(self)
38 def draw(self, context):
39 layout = self.layout
40 col = layout.column()
41 col.prop(self, "scene_name", text="Scene Name")
43 def format_to3(self, value):
44 return f"{value:03}"
46 # ------------------------------
47 # Execute button action
48 # ------------------------------
49 def execute(self, context):
50 scene_prv = context.scene
51 cfra_prv = scene_prv.frame_current
52 scene_base = scene_prv.storypencil_base_scene
54 # Set context to base scene and duplicate
55 context.window.scene = scene_base
56 bpy.ops.scene.new(type='FULL_COPY')
57 scene_new = context.window.scene
58 new_name = scene_prv.storypencil_name_prefix + \
59 self.scene_name + scene_prv.storypencil_name_suffix
60 id = 0
61 while new_name in bpy.data.scenes:
62 id += 1
63 new_name = scene_prv.storypencil_name_prefix + self.scene_name + \
64 scene_prv.storypencil_name_suffix + '.' + self.format_to3(id)
66 scene_new.name = new_name
67 # Set duration of new scene
68 scene_new.frame_end = scene_new.frame_start + \
69 scene_prv.storypencil_scene_duration - 1
71 # Back to original scene
72 context.window.scene = scene_prv
73 scene_prv.frame_current = cfra_prv
74 bpy.ops.sequencer.scene_strip_add(
75 frame_start=cfra_prv, scene=scene_new.name)
77 scene_new.update_tag()
78 scene_prv.update_tag()
80 return {"FINISHED"}
83 def draw_new_scene(self, context):
84 """Add menu options."""
86 self.layout.operator_context = 'INVOKE_REGION_WIN'
87 row = self.layout.row(align=True)
88 row.operator(STORYPENCIL_OT_NewScene.bl_idname, text="New Template Scene")
91 def setup_storyboard(self, context):
92 """Add Setup menu option."""
93 # For security, check if this is the default template.
94 is_gpencil = context.active_object and context.active_object.name == 'Stroke'
95 if is_gpencil and context.workspace.name in ('2D Animation', '2D Full Canvas') and context.scene.name == 'Scene':
96 if "Video Editing" not in bpy.data.workspaces:
97 row = self.layout.row(align=True)
98 row.separator()
99 row = self.layout.row(align=True)
100 row.operator(STORYPENCIL_OT_Setup.bl_idname,
101 text="Setup Storyboard Session")
104 # -------------------------------------------------------------
105 # Setup all environment
107 # -------------------------------------------------------------
108 class STORYPENCIL_OT_Setup(Operator):
109 bl_idname = "storypencil.setup"
110 bl_label = "Setup"
111 bl_description = "Configure all settings for a storyboard session"
112 bl_options = {'REGISTER', 'UNDO'}
114 # ------------------------------
115 # Poll
116 # ------------------------------
117 @classmethod
118 def poll(cls, context):
119 return True
121 def get_workspace(self, type):
122 for wrk in bpy.data.workspaces:
123 if wrk.name == type:
124 return wrk
126 return None
128 # ------------------------------
129 # Execute button action
130 # ------------------------------
131 def execute(self, context):
132 scene_base = context.scene
133 # Create Workspace
134 templatepath = None
135 if "Video Editing" not in bpy.data.workspaces:
136 template_path = None
137 for path in bpy.utils.app_template_paths():
138 template_path = path
140 filepath = os.path.join(
141 template_path, "Video_Editing", "startup.blend")
142 bpy.ops.workspace.append_activate(
143 idname="Video Editing", filepath=filepath)
144 # Create New scene
145 bpy.ops.scene.new()
146 scene_edit = context.scene
147 scene_edit.name = 'Edit'
148 # Rename original base scene
149 scene_base.name = 'Base'
150 # Setup Edit scene settings
151 scene_edit.storypencil_main_workspace = self.get_workspace(
152 "Video Editing")
153 scene_edit.storypencil_main_scene = scene_edit
154 scene_edit.storypencil_base_scene = scene_base
155 scene_edit.storypencil_edit_workspace = self.get_workspace(
156 "2D Animation")
158 # Add a new strip (need set the area context)
159 context.window.scene = scene_edit
160 area_prv = context.area.ui_type
161 context.area.ui_type = 'SEQUENCE_EDITOR'
162 prv_frame = scene_edit.frame_current
164 scene_edit.frame_current = scene_edit.frame_start
165 bpy.ops.storypencil.new_scene()
167 context.area.ui_type = area_prv
168 scene_edit.frame_current = prv_frame
170 scene_edit.update_tag()
171 bpy.ops.sequencer.reload()
173 return {"FINISHED"}