Cleanup: quiet float argument to in type warning
[blender-addons.git] / render_copy_settings / operator.py
blob9540e3ad1330fdb97e9762769949eece18ffcf5a
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 from . import presets
6 # A sorting func for collections (working in-place).
7 # XXX Not optimized at all…
8 # XXX If some items in the collection do not have the sortkey property, they are just ignored…
9 def collection_property_sort(collection, sortkey, start_idx=0):
10 while start_idx + 1 < len(collection):
11 while not hasattr(collection[start_idx], sortkey):
12 start_idx += 1
13 if start_idx + 1 >= len(collection):
14 return collection
15 min_idx = start_idx
16 min_prop = collection[start_idx]
17 for i, prop in enumerate(collection[start_idx + 1:]):
18 if not hasattr(prop, sortkey):
19 continue
20 if getattr(prop, sortkey) < getattr(min_prop, sortkey):
21 min_prop = prop
22 min_idx = i + start_idx + 1
23 collection.move(min_idx, start_idx)
24 start_idx += 1
25 return collection
28 def scene_render_copy_settings_update():
29 """Prepare internal data for render_copy_settings (gathering all existing render settings, and scenes)."""
30 current_scene = getattr(bpy.context, "scene", None)
31 if current_scene is None:
32 return
33 cp_sett = current_scene.render_copy_settings
35 # Get all available render settings, and update accordingly affected_settings…
36 props = {}
37 for prop in current_scene.render.bl_rna.properties:
38 if prop.identifier in {'rna_type'}:
39 continue
40 if prop.is_readonly:
41 continue
42 props[prop.identifier] = prop.name
43 corr = 0
44 for i, sett in enumerate(cp_sett.affected_settings):
45 if sett.strid not in props:
46 cp_sett.affected_settings.remove(i - corr)
47 corr += 1
48 else:
49 del props[sett.strid]
50 for strid, name in props.items():
51 sett = cp_sett.affected_settings.add()
52 sett.name = "{} [{}]".format(name, strid)
53 sett.strid = strid
54 collection_property_sort(cp_sett.affected_settings, "name")
56 # Get all available scenes, and update accordingly allowed_scenes…
57 regex = None
58 if cp_sett.filter_scene:
59 try:
60 import re
61 try:
62 regex = re.compile(cp_sett.filter_scene)
63 except Exception as e:
64 print("The filter-scene regex did not compile:\n (%s)." % str(e))
65 return
66 except:
67 regex = None
68 print("Unable to import the re module, regex scene filtering will be disabled!")
69 scenes = set()
70 for scene in bpy.data.scenes:
71 if scene == current_scene: # Exclude current scene!
72 continue
73 # If a valid filtering regex, only keep scenes matching it.
74 if regex:
75 if regex.match(scene.name):
76 scenes.add(scene.name)
77 else:
78 scenes.add(scene.name)
79 for i, scene in enumerate(cp_sett.allowed_scenes):
80 if scene.name not in scenes:
81 cp_sett.allowed_scenes.remove(i)
82 else:
83 scenes.remove(scene.name)
84 for scene in scenes:
85 sett = cp_sett.allowed_scenes.add()
86 sett.name = scene
87 collection_property_sort(cp_sett.allowed_scenes, "name")
90 from bpy.props import EnumProperty
93 class RenderCopySettingsOPPreset(bpy.types.Operator):
94 """Apply some presets of render settings to copy to other scenes"""
95 bl_idname = "scene.render_copy_settings_preset"
96 bl_label = "Render: Copy Settings Preset"
97 bl_description = "Apply or clear this preset of render settings"
98 # Enable undo…
99 bl_option = {'REGISTER', 'UNDO'}
101 presets: EnumProperty(items=(p.rna_enum for p in presets.presets),
102 default=set(),
103 options={'ENUM_FLAG'})
105 @staticmethod
106 def process_elements(settings, elts):
107 setts = []
108 val = True
109 for sett in settings:
110 if sett.strid in elts:
111 setts.append(sett)
112 val = val and sett.copy
113 for e in setts:
114 e.copy = not val
116 @classmethod
117 def poll(cls, context):
118 return context.scene is not None
120 def execute(self, context):
121 cp_sett = context.scene.render_copy_settings
122 for p in presets.presets:
123 if p.rna_enum[0] in self.presets:
124 self.process_elements(cp_sett.affected_settings, p.elements)
125 return {'FINISHED'}
128 # Real interesting stuff…
130 def do_copy(context, affected_settings, allowed_scenes):
131 # Stores render settings from current scene.
132 p = {sett: getattr(context.scene.render, sett)
133 for sett in affected_settings}
134 # put it in all other (valid) scenes’ render settings!
135 for scene in bpy.data.scenes:
136 # If scene not in allowed scenes, skip.
137 if scene.name not in allowed_scenes:
138 continue
139 # Propagate all affected settings.
140 for sett, val in p.items():
141 setattr(scene.render, sett, val)
144 class RenderCopySettingsOPCopy(bpy.types.Operator):
145 """Copy render settings from current scene to others"""
146 bl_idname = "scene.render_copy_settings"
147 bl_label = "Render: Copy Settings"
148 # Enable undo…
149 bl_option = {'REGISTER', 'UNDO'}
151 @classmethod
152 def poll(cls, context):
153 return context.scene is not None
155 def execute(self, context):
156 regex = None
157 cp_sett = context.scene.render_copy_settings
158 affected_settings = {sett.strid for sett in cp_sett.affected_settings if sett.copy}
159 allowed_scenes = {sce.name for sce in cp_sett.allowed_scenes if sce.allowed}
160 do_copy(context, affected_settings=affected_settings, allowed_scenes=allowed_scenes)
161 return {'FINISHED'}
164 classes = (
165 RenderCopySettingsOPPreset,
166 RenderCopySettingsOPCopy,