~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/addons/render_copy_settings/operator.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ##### BEGIN GPL LICENSE BLOCK #####
 
2
#
 
3
#  This program is free software; you can redistribute it and/or
 
4
#  modify it under the terms of the GNU General Public License
 
5
#  as published by the Free Software Foundation; either version 2
 
6
#  of the License, or (at your option) any later version.
 
7
#
 
8
#  This program is distributed in the hope that it will be useful,
 
9
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#  GNU General Public License for more details.
 
12
#
 
13
#  You should have received a copy of the GNU General Public License
 
14
#  along with this program; if not, write to the Free Software Foundation,
 
15
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
16
#
 
17
# ##### END GPL LICENSE BLOCK #####
 
18
 
 
19
# <pep8 compliant>
 
20
 
 
21
import bpy
 
22
from . import presets
 
23
 
 
24
# These operators are only defined because it seems impossible to directly
 
25
# edit properties from UI code…
 
26
 
 
27
 
 
28
# A sorting func for collections (working in-place).
 
29
# XXX Not optimized at all…
 
30
# XXX If some items in the collection do not have the sortkey property,
 
31
#     they are just ignored…
 
32
def collection_property_sort(collection, sortkey, start_idx=0):
 
33
    while start_idx + 1 < len(collection):
 
34
        while not hasattr(collection[start_idx], sortkey):
 
35
            start_idx += 1
 
36
            if start_idx + 1 >= len(collection):
 
37
                return collection
 
38
        min_idx = start_idx
 
39
        min_prop = collection[start_idx]
 
40
        for i, prop in enumerate(collection[start_idx + 1:]):
 
41
            if not hasattr(prop, sortkey):
 
42
                continue
 
43
            if getattr(prop, sortkey) < getattr(min_prop, sortkey):
 
44
                min_prop = prop
 
45
                min_idx = i + start_idx + 1
 
46
        collection.move(min_idx, start_idx)
 
47
        start_idx += 1
 
48
    return collection
 
49
 
 
50
 
 
51
class RenderCopySettingsPrepare(bpy.types.Operator):
 
52
    '''
 
53
    Prepare internal data for render_copy_settings (gathering all existing
 
54
    render settings, and scenes)
 
55
    '''
 
56
    bl_idname = "scene.render_copy_settings_prepare"
 
57
    bl_label = "Render: Copy Settings Prepare"
 
58
    bl_option = {'REGISTER'}
 
59
 
 
60
    @classmethod
 
61
    def poll(cls, context):
 
62
        return context.scene != None
 
63
 
 
64
    def execute(self, context):
 
65
        cp_sett = context.scene.render_copy_settings
 
66
 
 
67
        # Get all available render settings, and update accordingly
 
68
        # affected_settings…
 
69
        props = {}
 
70
        for prop in context.scene.render.bl_rna.properties:
 
71
            if prop.identifier in {'rna_type'}:
 
72
                continue
 
73
            if prop.is_readonly:
 
74
                continue
 
75
            props[prop.identifier] = prop.name
 
76
        corr = 0
 
77
        for i, sett in enumerate(cp_sett.affected_settings):
 
78
            if sett.strid not in props:
 
79
                cp_sett.affected_settings.remove(i - corr)
 
80
                corr += 1
 
81
            else:
 
82
                del props[sett.strid]
 
83
        for strid, name in props.items():
 
84
            sett = cp_sett.affected_settings.add()
 
85
            sett.name = "{} [{}]".format(name, strid)
 
86
            sett.strid = strid
 
87
        collection_property_sort(cp_sett.affected_settings, "name")
 
88
 
 
89
        # Get all available scenes, and update accordingly allowed_scenes…
 
90
        regex = None
 
91
        if cp_sett.filter_scene:
 
92
            try:
 
93
                import re
 
94
                try:
 
95
                    regex = re.compile(cp_sett.filter_scene)
 
96
                except Exception as e:
 
97
                    self.report('ERROR_INVALID_INPUT', "The filter-scene "
 
98
                                "regex did not compile:\n    (%s)." % str(e))
 
99
                    return {'CANCELLED'}
 
100
            except:
 
101
                regex = None
 
102
                self.report('WARNING', "Unable to import the re module. "
 
103
                            "Regex scene filtering will be disabled!")
 
104
        scenes = set()
 
105
        for scene in bpy.data.scenes:
 
106
            if scene == bpy.context.scene:  # Exclude current scene!
 
107
                continue
 
108
            # If a valid filtering regex, only keep scenes matching it.
 
109
            if regex:
 
110
                if regex.match(scene.name):
 
111
                    scenes.add(scene.name)
 
112
            else:
 
113
                scenes.add(scene.name)
 
114
        for i, scene in enumerate(cp_sett.allowed_scenes):
 
115
            if scene.name not in scenes:
 
116
                cp_sett.allowed_scenes.remove(i)
 
117
            else:
 
118
                scenes.remove(scene.name)
 
119
        for scene in scenes:
 
120
            sett = cp_sett.allowed_scenes.add()
 
121
            sett.name = scene
 
122
        collection_property_sort(cp_sett.allowed_scenes, "name")
 
123
 
 
124
        return {'FINISHED'}
 
125
 
 
126
 
 
127
from bpy.props import EnumProperty
 
128
 
 
129
 
 
130
class RenderCopySettingsPreset(bpy.types.Operator):
 
131
    '''Apply some presets of render settings to copy to other scenes'''
 
132
    bl_idname = "scene.render_copy_settings_preset"
 
133
    bl_label = "Render: Copy Settings Preset"
 
134
    bl_description = "Apply or clear this preset of render settings"
 
135
    # Enable undo…
 
136
    bl_option = {'REGISTER', 'UNDO'}
 
137
 
 
138
    presets = EnumProperty(items=(p.rna_enum for p in presets.presets),
 
139
                           default=set(),
 
140
                           options={"ENUM_FLAG"})
 
141
 
 
142
    @staticmethod
 
143
    def process_elements(settings, elts):
 
144
        setts = []
 
145
        val = True
 
146
        for sett in settings:
 
147
            if sett.strid in elts:
 
148
                setts.append(sett)
 
149
                val = val and sett.copy
 
150
        for e in setts:
 
151
            e.copy = not val
 
152
 
 
153
    @classmethod
 
154
    def poll(cls, context):
 
155
        return context.scene != None
 
156
 
 
157
    def execute(self, context):
 
158
        cp_sett = context.scene.render_copy_settings
 
159
        for p in presets.presets:
 
160
            if p.rna_enum[0] in self.presets:
 
161
                self.process_elements(cp_sett.affected_settings, p.elements)
 
162
        return {'FINISHED'}
 
163
 
 
164
 
 
165
# Real interesting stuff…
 
166
 
 
167
def do_copy(context, affected_settings, allowed_scenes):
 
168
    # Stores render settings from current scene.
 
169
    p = {sett: getattr(context.scene.render, sett)
 
170
         for sett in affected_settings}
 
171
    # put it in all other (valid) scenes’ render settings!
 
172
    for scene in bpy.data.scenes:
 
173
        # If scene not in allowed scenes, skip.
 
174
        if scene.name not in allowed_scenes:
 
175
            continue
 
176
        # Propagate all affected settings.
 
177
        for sett, val in p.items():
 
178
            setattr(scene.render, sett, val)
 
179
 
 
180
 
 
181
class RenderCopySettings(bpy.types.Operator):
 
182
    '''Copy render settings from current scene to others'''
 
183
    bl_idname = "scene.render_copy_settings"
 
184
    bl_label = "Render: Copy Settings"
 
185
    # Enable undo…
 
186
    bl_option = {'REGISTER', 'UNDO'}
 
187
 
 
188
    @classmethod
 
189
    def poll(cls, context):
 
190
        return context.scene != None
 
191
 
 
192
    def execute(self, context):
 
193
        regex = None
 
194
        cp_sett = context.scene.render_copy_settings
 
195
        affected_settings = {sett.strid for sett in cp_sett.affected_settings
 
196
                                                 if sett.copy}
 
197
        allowed_scenes = {sce.name for sce in cp_sett.allowed_scenes
 
198
                                           if sce.allowed}
 
199
        do_copy(context, affected_settings=affected_settings,
 
200
                allowed_scenes=allowed_scenes)
 
201
        return {'FINISHED'}
 
202
 
 
203
 
 
204
if __name__ == "__main__":
 
205
    bpy.ops.scene.render_copy_settings()