Sun Position: Fix crash when Blender was started in background
[blender-addons.git] / storypencil / utils.py
blob9357d1c5c86084eafce76be31ce09b01893bb1f2
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 import math
7 def redraw_areas_by_type(window, area_type, region_type='WINDOW'):
8 """Redraw `window`'s areas matching the given `area_type` and optionnal `region_type`."""
9 for area in window.screen.areas:
10 if area.type == area_type:
11 for region in area.regions:
12 if region.type == region_type:
13 region.tag_redraw()
16 def redraw_all_areas_by_type(context, area_type, region_type='WINDOW'):
17 """Redraw areas in all windows matching the given `area_type` and optionnal `region_type`."""
18 for window in context.window_manager.windows:
19 redraw_areas_by_type(window, area_type, region_type)
22 def get_selected_keyframes(context):
23 """Get list of selected keyframes for any object in the scene. """
24 keys = []
26 for ob in context.scene.objects:
27 if ob.type == 'GPENCIL':
28 for gpl in ob.data.layers:
29 for gpf in gpl.frames:
30 if gpf.select:
31 keys.append(gpf.frame_number)
33 elif ob.animation_data is not None and ob.animation_data.action is not None:
34 action = ob.animation_data.action
35 for fcu in action.fcurves:
36 for kp in fcu.keyframe_points:
37 if kp.select_control_point:
38 keys.append(int(kp.co[0]))
40 keys.sort()
41 unique_keys = list(set(keys))
42 return unique_keys
45 def find_collections_recursive(root, collections=None):
46 # Initialize the result once
47 if collections is None:
48 collections = []
50 def recurse(parent, result):
51 result.append(parent)
52 # Look over children at next level
53 for child in parent.children:
54 recurse(child, result)
56 recurse(root, collections)
58 return collections
61 def get_keyframe_list(scene, frame_start, frame_end):
62 """Get list of frames for any gpencil object in the scene and meshes. """
63 keys = []
64 root = scene.view_layers[0].layer_collection
65 collections = find_collections_recursive(root)
67 for laycol in collections:
68 if laycol.exclude is True or laycol.collection.hide_render is True:
69 continue
70 for ob in laycol.collection.objects:
71 if ob.hide_render:
72 continue
73 if ob.type == 'GPENCIL':
74 for gpl in ob.data.layers:
75 if gpl.hide:
76 continue
77 for gpf in gpl.frames:
78 if frame_start <= gpf.frame_number <= frame_end:
79 keys.append(gpf.frame_number)
81 # Animation at object level
82 if ob.animation_data is not None and ob.animation_data.action is not None:
83 action = ob.animation_data.action
84 for fcu in action.fcurves:
85 for kp in fcu.keyframe_points:
86 if frame_start <= int(kp.co[0]) <= frame_end:
87 keys.append(int(kp.co[0]))
89 # Animation at datablock level
90 if ob.type != 'GPENCIL':
91 data = ob.data
92 if data and data.animation_data is not None and data.animation_data.action is not None:
93 action = data.animation_data.action
94 for fcu in action.fcurves:
95 for kp in fcu.keyframe_points:
96 if frame_start <= int(kp.co[0]) <= frame_end:
97 keys.append(int(kp.co[0]))
99 # Scene Markers
100 for m in scene.timeline_markers:
101 if frame_start <= m.frame <= frame_end and m.camera is not None:
102 keys.append(int(m.frame))
104 # If no animation or markers, must add first frame
105 if len(keys) == 0:
106 keys.append(int(frame_start))
108 unique_keys = list(set(keys))
109 unique_keys.sort()
110 return unique_keys