Cleanup: strip trailing space
[blender-addons.git] / storypencil / utils.py
blobe12f021953ede3b2452b6d34059faf512ca45e71
1 # SPDX-FileCopyrightText: 2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
6 import math
9 def redraw_areas_by_type(window, area_type, region_type='WINDOW'):
10 """Redraw `window`'s areas matching the given `area_type` and optionnal `region_type`."""
11 for area in window.screen.areas:
12 if area.type == area_type:
13 for region in area.regions:
14 if region.type == region_type:
15 region.tag_redraw()
18 def redraw_all_areas_by_type(context, area_type, region_type='WINDOW'):
19 """Redraw areas in all windows matching the given `area_type` and optionnal `region_type`."""
20 for window in context.window_manager.windows:
21 redraw_areas_by_type(window, area_type, region_type)
24 def get_selected_keyframes(context):
25 """Get list of selected keyframes for any object in the scene. """
26 keys = []
28 for ob in context.scene.objects:
29 if ob.type == 'GPENCIL':
30 for gpl in ob.data.layers:
31 for gpf in gpl.frames:
32 if gpf.select:
33 keys.append(gpf.frame_number)
35 elif ob.animation_data is not None and ob.animation_data.action is not None:
36 action = ob.animation_data.action
37 for fcu in action.fcurves:
38 for kp in fcu.keyframe_points:
39 if kp.select_control_point:
40 keys.append(int(kp.co[0]))
42 keys.sort()
43 unique_keys = list(set(keys))
44 return unique_keys
47 def find_collections_recursive(root, collections=None):
48 # Initialize the result once
49 if collections is None:
50 collections = []
52 def recurse(parent, result):
53 result.append(parent)
54 # Look over children at next level
55 for child in parent.children:
56 recurse(child, result)
58 recurse(root, collections)
60 return collections
63 def get_keyframe_list(scene, frame_start, frame_end):
64 """Get list of frames for any gpencil object in the scene and meshes. """
65 keys = []
66 root = scene.view_layers[0].layer_collection
67 collections = find_collections_recursive(root)
69 for laycol in collections:
70 if laycol.exclude is True or laycol.collection.hide_render is True:
71 continue
72 for ob in laycol.collection.objects:
73 if ob.hide_render:
74 continue
75 if ob.type == 'GPENCIL':
76 for gpl in ob.data.layers:
77 if gpl.hide:
78 continue
79 for gpf in gpl.frames:
80 if frame_start <= gpf.frame_number <= frame_end:
81 keys.append(gpf.frame_number)
83 # Animation at object level
84 if ob.animation_data is not None and ob.animation_data.action is not None:
85 action = ob.animation_data.action
86 for fcu in action.fcurves:
87 for kp in fcu.keyframe_points:
88 if frame_start <= int(kp.co[0]) <= frame_end:
89 keys.append(int(kp.co[0]))
91 # Animation at datablock level
92 if ob.type != 'GPENCIL':
93 data = ob.data
94 if data and data.animation_data is not None and data.animation_data.action is not None:
95 action = data.animation_data.action
96 for fcu in action.fcurves:
97 for kp in fcu.keyframe_points:
98 if frame_start <= int(kp.co[0]) <= frame_end:
99 keys.append(int(kp.co[0]))
101 # Scene Markers
102 for m in scene.timeline_markers:
103 if frame_start <= m.frame <= frame_end and m.camera is not None:
104 keys.append(int(m.frame))
106 # If no animation or markers, must add first frame
107 if len(keys) == 0:
108 keys.append(int(frame_start))
110 unique_keys = list(set(keys))
111 unique_keys.sort()
112 return unique_keys