Welcome to mirror list, hosted at ThFree Co, Russian Federation.

freestyle.py « bl_operators « startup « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: edda92284d234542485d1ff00c6a5f97b0cc9389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import bpy

from bpy.props import (
        BoolProperty,
        EnumProperty,
        StringProperty,
        )


class SCENE_OT_freestyle_fill_range_by_selection(bpy.types.Operator):
    """Fill the Range Min/Max entries by the min/max distance between selected mesh objects and the source object """
    """(either a user-specified object or the active camera)"""
    bl_idname = "scene.freestyle_fill_range_by_selection"
    bl_label = "Fill Range by Selection"
    bl_options = {'INTERNAL'}

    type = EnumProperty(
            name="Type", description="Type of the modifier to work on",
            items=(("COLOR", "Color", "Color modifier type"),
                   ("ALPHA", "Alpha", "Alpha modifier type"),
                   ("THICKNESS", "Thickness", "Thickness modifier type")),
            )
    name = StringProperty(
            name="Name",
            description="Name of the modifier to work on",
            )

    @classmethod
    def poll(cls, context):
        rl = context.scene.render.layers.active
        return rl and rl.freestyle_settings.linesets.active

    def execute(self, context):
        import sys

        scene = context.scene
        rl = scene.render.layers.active
        lineset = rl.freestyle_settings.linesets.active
        linestyle = lineset.linestyle
        # Find the modifier to work on
        if self.type == 'COLOR':
            m = linestyle.color_modifiers[self.name]
        elif self.type == 'ALPHA':
            m = linestyle.alpha_modifiers[self.name]
        else:
            m = linestyle.thickness_modifiers[self.name]
        # Find the source object
        if m.type == 'DISTANCE_FROM_CAMERA':
            source = scene.camera
        elif m.type == 'DISTANCE_FROM_OBJECT':
            if m.target is None:
                self.report({'ERROR'}, "Target object not specified")
                return {'CANCELLED'}
            source = m.target
        else:
            self.report({'ERROR'}, "Unexpected modifier type: " + m.type)
            return {'CANCELLED'}
        # Find selected mesh objects
        selection = [ob for ob in scene.objects if ob.select and ob.type == 'MESH' and ob.name != source.name]
        if selection:
            # Compute the min/max distance between selected mesh objects and the source
            min_dist = sys.float_info.max
            max_dist = -min_dist
            for ob in selection:
                for vert in ob.data.vertices:
                    dist = (ob.matrix_world * vert.co - source.location).length
                    min_dist = min(dist, min_dist)
                    max_dist = max(dist, max_dist)
            # Fill the Range Min/Max entries with the computed distances
            m.range_min = min_dist
            m.range_max = max_dist
        return {'FINISHED'}


class SCENE_OT_freestyle_add_edge_marks_to_keying_set(bpy.types.Operator):
    '''Add the data paths to the Freestyle Edge Mark property of selected edges to the active keying set'''
    bl_idname = "scene.freestyle_add_edge_marks_to_keying_set"
    bl_label = "Add Edge Marks to Keying Set"
    bl_options = {'UNDO'}

    @classmethod
    def poll(cls, context):
        ob = context.active_object
        return (ob and ob.type == 'MESH')

    def execute(self, context):
        # active keying set
        scene = context.scene
        ks = scene.keying_sets.active
        if ks is None:
            ks = scene.keying_sets.new(idname="FreestyleEdgeMarkKeyingSet", name="Freestyle Edge Mark Keying Set")
            ks.bl_description = ""
        # add data paths to the keying set
        ob = context.active_object
        ob_mode = ob.mode
        mesh = ob.data
        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
        for i, edge in enumerate(mesh.edges):
            if not edge.hide and edge.select:
                path = 'edges[%d].use_freestyle_mark' % i
                ks.paths.add(mesh, path, index=0)
        bpy.ops.object.mode_set(mode=ob_mode, toggle=False)
        return {'FINISHED'}


class SCENE_OT_freestyle_add_face_marks_to_keying_set(bpy.types.Operator):
    '''Add the data paths to the Freestyle Face Mark property of selected polygons to the active keying set'''
    bl_idname = "scene.freestyle_add_face_marks_to_keying_set"
    bl_label = "Add Face Marks to Keying Set"
    bl_options = {'UNDO'}

    @classmethod
    def poll(cls, context):
        ob = context.active_object
        return (ob and ob.type == 'MESH')

    def execute(self, context):
        # active keying set
        scene = context.scene
        ks = scene.keying_sets.active
        if ks is None:
            ks = scene.keying_sets.new(idname="FreestyleFaceMarkKeyingSet", name="Freestyle Face Mark Keying Set")
            ks.bl_description = ""
        # add data paths to the keying set
        ob = context.active_object
        ob_mode = ob.mode
        mesh = ob.data
        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
        for i, polygon in enumerate(mesh.polygons):
            if not polygon.hide and polygon.select:
                path = 'polygons[%d].use_freestyle_mark' % i
                ks.paths.add(mesh, path, index=0)
        bpy.ops.object.mode_set(mode=ob_mode, toggle=False)
        return {'FINISHED'}


class SCENE_OT_freestyle_module_open(bpy.types.Operator):
    """Open a style module file"""
    bl_idname = "scene.freestyle_module_open"
    bl_label = "Open Style Module File"
    bl_options = {'INTERNAL'}

    filepath = StringProperty(subtype='FILE_PATH')

    make_internal = BoolProperty(
        name="Make internal",
        description="Make module file internal after loading",
        default=True)

    @classmethod
    def poll(cls, context):
        rl = context.scene.render.layers.active
        return rl and rl.freestyle_settings.mode == 'SCRIPT'

    def invoke(self, context, event):
        self.freestyle_module = context.freestyle_module
        wm = context.window_manager
        wm.fileselect_add(self)
        return {'RUNNING_MODAL'}

    def execute(self, context):
        text = bpy.data.texts.load(self.filepath, self.make_internal)
        self.freestyle_module.script = text
        return {'FINISHED'}