Skip to content
Snippets Groups Projects
object_color_rules.py 14.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    # ***** 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 LICENCE BLOCK *****
    
    bl_info = {
        "name": "Object Color Rules",
        "author": "Campbell Barton",
        "version": (0, 0, 2),
        "blender": (2, 80, 0),
        "location": "Properties > Object Buttons",
        "description": "Rules for assigning object color (for object & wireframe colors).",
        "category": "Object",
    }
    
    
    def test_name(rule, needle, haystack, cache):
        if rule.use_match_regex:
            if not cache:
                import re
                re_needle = re.compile(needle)
                cache[:] = [re_needle]
            else:
                re_needle = cache[0]
            return (re_needle.match(haystack) is not None)
        else:
            return (needle in haystack)
    
    
    class rule_test:
        __slots__ = ()
    
        def __new__(cls, *args, **kwargs):
            raise RuntimeError("%s should not be instantiated" % cls)
    
        @staticmethod
        def NAME(obj, rule, cache):
            match_name = rule.match_name
            return test_name(rule, match_name, obj.name, cache)
    
        def DATA(obj, rule, cache):
            match_name = rule.match_name
            obj_data = obj.data
            if obj_data is not None:
                return test_name(rule, match_name, obj_data.name, cache)
            else:
                return False
    
        @staticmethod
        def COLLECTION(obj, rule, cache):
            if not cache:
                match_name = rule.match_name
                objects = {o for g in bpy.data.collections if test_name(rule, match_name, g.name, cache) for o in g.objects}
                cache["objects"] = objects
            else:
                objects = cache["objects"]
    
            return obj in objects
    
        @staticmethod
        def MATERIAL(obj, rule, cache):
            match_name = rule.match_name
            materials = getattr(obj.data, "materials", None)
    
            return ((materials is not None) and
                    (any((test_name(rule, match_name, m.name) for m in materials if m is not None))))
    
        @staticmethod
        def TYPE(obj, rule, cache):
            return (obj.type == rule.match_object_type)
    
        @staticmethod
        def EXPR(obj, rule, cache):
            if not cache:
                match_expr = rule.match_expr
                expr = compile(match_expr, rule.name, 'eval')
    
                namespace = {}
                namespace.update(__import__("math").__dict__)
    
                cache["expr"] = expr
                cache["namespace"] = namespace
            else:
                expr = cache["expr"]
                namespace = cache["namespace"]
    
            try:
                return bool(eval(expr, {}, {"self": obj}))
            except:
                import traceback
                traceback.print_exc()
                return False
    
    
    class rule_draw:
        __slots__ = ()
    
        def __new__(cls, *args, **kwargs):
            raise RuntimeError("%s should not be instantiated" % cls)
    
        @staticmethod
        def _generic_match_name(layout, rule):
            layout.label(text="Match Name:")
            row = layout.row(align=True)
            row.prop(rule, "match_name", text="")
            row.prop(rule, "use_match_regex", text="", icon='SORTALPHA')
    
        @staticmethod
        def NAME(layout, rule):
            rule_draw._generic_match_name(layout, rule)
    
        @staticmethod
        def DATA(layout, rule):
            rule_draw._generic_match_name(layout, rule)
    
        @staticmethod
        def COLLECTION(layout, rule):
            rule_draw._generic_match_name(layout, rule)
    
        @staticmethod
        def MATERIAL(layout, rule):
            rule_draw._generic_match_name(layout, rule)
    
        @staticmethod
        def TYPE(layout, rule):
            row = layout.row()
            row.prop(rule, "match_object_type")
    
        @staticmethod
        def EXPR(layout, rule):
            col = layout.column()
            col.label(text="Scripted Expression:")
            col.prop(rule, "match_expr", text="")
    
    
    def object_colors_calc(rules, objects):
        from mathutils import Color
    
        rules_cb = [getattr(rule_test, rule.type) for rule in rules]
        rules_blend = [(1.0 - rule.factor, rule.factor) for rule in rules]
        rules_color = [Color(rule.color) for rule in rules]
        rules_cache = [{} for i in range(len(rules))]
        rules_inv = [rule.use_invert for rule in rules]
        changed_count = 0
    
        for obj in objects:
            is_set = False
            obj_color = Color(obj.color[0:3])
    
            for (rule, test_cb, color, blend, cache, use_invert) \
                 in zip(rules, rules_cb, rules_color, rules_blend, rules_cache, rules_inv):
    
                if test_cb(obj, rule, cache) is not use_invert:
                    if is_set is False:
                        obj_color = color
                    else:
                        # prevent mixing colors loosing saturation
                        obj_color_s = obj_color.s
                        obj_color = (obj_color * blend[0]) + (color * blend[1])
                        obj_color.s = (obj_color_s * blend[0]) + (color.s * blend[1])
    
                    is_set = True
    
            if is_set:
                obj.color[0:3] = obj_color
                changed_count += 1
        return changed_count
    
    
    def object_colors_select(rule, objects):
        cache = {}
    
        rule_type = rule.type
        test_cb = getattr(rule_test, rule_type)
    
        for obj in objects:
            obj.select = test_cb(obj, rule, cache)
    
    
    def object_colors_rule_validate(rule, report):
        rule_type = rule.type
    
        if rule_type in {'NAME', 'DATA', 'COLLECTION', 'MATERIAL'}:
            if rule.use_match_regex:
                import re
                try:
                    re.compile(rule.match_name)
                except Exception as e:
                    report({'ERROR'}, "Rule %r: %s" % (rule.name, str(e)))
                    return False
    
        elif rule_type == 'EXPR':
            try:
                compile(rule.match_expr, rule.name, 'eval')
            except Exception as e:
                report({'ERROR'}, "Rule %r: %s" % (rule.name, str(e)))
                return False
    
        return True
    
    
    
    import bpy
    from bpy.types import (
        Operator,
        Panel,
        UIList,
    )
    from bpy.props import (
        StringProperty,
        BoolProperty,
        IntProperty,
        FloatProperty,
        EnumProperty,
        CollectionProperty,
        BoolVectorProperty,
        FloatVectorProperty,
    )
    
    
    class OBJECT_PT_color_rules(Panel):
        bl_label = "Color Rules"
        bl_space_type = 'PROPERTIES'
        bl_region_type = 'WINDOW'
        bl_context = "object"
    
        def draw(self, context):
            layout = self.layout
    
            scene = context.scene
    
            # Rig type list
            row = layout.row()
            row.template_list(
                "OBJECT_UL_color_rule", "color_rules",
                scene, "color_rules",
                scene, "color_rules_active_index",
            )
    
            col = row.column()
            colsub = col.column(align=True)
            colsub.operator("object.color_rules_add", icon='ADD', text="")
            colsub.operator("object.color_rules_remove", icon='REMOVE', text="")
    
            colsub = col.column(align=True)
            colsub.operator("object.color_rules_move", text="", icon='TRIA_UP').direction = -1
            colsub.operator("object.color_rules_move", text="", icon='TRIA_DOWN').direction = 1
    
            colsub = col.column(align=True)
            colsub.operator("object.color_rules_select", text="", icon='RESTRICT_SELECT_OFF')
    
            if scene.color_rules:
                index = scene.color_rules_active_index
                rule = scene.color_rules[index]
    
                box = layout.box()
                row = box.row(align=True)
                row.prop(rule, "name", text="")
                row.prop(rule, "type", text="")
                row.prop(rule, "use_invert", text="", icon='ARROW_LEFTRIGHT')
    
                draw_cb = getattr(rule_draw, rule.type)
                draw_cb(box, rule)
    
                row = layout.split(factor=0.75, align=True)
                props = row.operator("object.color_rules_assign", text="Assign Selected")
                props.use_selection = True
                props = row.operator("object.color_rules_assign", text="All")
                props.use_selection = False
    
    
    class OBJECT_UL_color_rule(UIList):
        def draw_item(self, context, layout, data, rule, icon, active_data, active_propname, index):
            # assert(isinstance(rule, bpy.types.ShapeKey))
            # scene = active_data
            split = layout.split(factor=0.5)
            row = split.split(align=False)
            row.label(text="%s (%s)" % (rule.name, rule.type.lower()))
            split = split.split(factor=0.7)
            split.prop(rule, "factor", text="", emboss=False)
            split.prop(rule, "color", text="")
    
    
    class OBJECT_OT_color_rules_assign(Operator):
        """Assign colors to objects based on user rules"""
        bl_idname = "object.color_rules_assign"
        bl_label = "Assign Colors"
        bl_options = {'UNDO'}
    
        use_selection: BoolProperty(
            name="Selected",
            description="Apply to selected (otherwise all objects in the scene)",
            default=True,
        )
    
        def execute(self, context):
            scene = context.scene
    
            if self.use_selection:
                objects = context.selected_editable_objects
            else:
                objects = scene.objects
    
            rules = scene.color_rules[:]
            for rule in rules:
                if not object_colors_rule_validate(rule, self.report):
                    return {'CANCELLED'}
    
            changed_count = object_colors_calc(rules, objects)
            self.report({'INFO'}, "Set colors for {} of {} objects".format(changed_count, len(objects)))
            return {'FINISHED'}
    
    
    class OBJECT_OT_color_rules_select(Operator):
        """Select objects matching the current rule"""
        bl_idname = "object.color_rules_select"
        bl_label = "Select Rule"
        bl_options = {'UNDO'}
    
        def execute(self, context):
            scene = context.scene
            rule = scene.color_rules[scene.color_rules_active_index]
    
            if not object_colors_rule_validate(rule, self.report):
                return {'CANCELLED'}
    
            objects = context.visible_objects
            object_colors_select(rule, objects)
            return {'FINISHED'}
    
    
    class OBJECT_OT_color_rules_add(Operator):
        bl_idname = "object.color_rules_add"
        bl_label = "Add Color Layer"
        bl_options = {'UNDO'}
    
        def execute(self, context):
            scene = context.scene
            rules = scene.color_rules
            rule = rules.add()
            rule.name = "Rule.%.3d" % len(rules)
            scene.color_rules_active_index = len(rules) - 1
            return {'FINISHED'}
    
    
    class OBJECT_OT_color_rules_remove(Operator):
        bl_idname = "object.color_rules_remove"
        bl_label = "Remove Color Layer"
        bl_options = {'UNDO'}
    
        def execute(self, context):
            scene = context.scene
            rules = scene.color_rules
            rules.remove(scene.color_rules_active_index)
            if scene.color_rules_active_index > len(rules) - 1:
                scene.color_rules_active_index = len(rules) - 1
            return {'FINISHED'}
    
    
    class OBJECT_OT_color_rules_move(Operator):
        bl_idname = "object.color_rules_move"
        bl_label = "Remove Color Layer"
        bl_options = {'UNDO'}
        direction: IntProperty()
    
        def execute(self, context):
            scene = context.scene
            rules = scene.color_rules
            index = scene.color_rules_active_index
            index_new = index + self.direction
            if index_new < len(rules) and index_new >= 0:
                rules.move(index, index_new)
                scene.color_rules_active_index = index_new
                return {'FINISHED'}
            else:
                return {'CANCELLED'}
    
    
    class ColorRule(bpy.types.PropertyGroup):
        name: StringProperty(
            name="Rule Name",
        )
        color: FloatVectorProperty(
            name="Color",
            description="Color to assign",
            subtype='COLOR', size=3, min=0, max=1, precision=3, step=0.1,
            default=(0.5, 0.5, 0.5),
        )
        factor: FloatProperty(
            name="Opacity",
            description="Color to assign",
            min=0, max=1, precision=1, step=0.1,
            default=1.0,
        )
        type: EnumProperty(
            name="Rule Type",
            items=(
                ('NAME', "Name", "Object name contains this text (or matches regex)"),
                ('DATA', "Data Name", "Object data name contains this text (or matches regex)"),
                ('COLLECTION', "Collection Name", "Object in collection that contains this text (or matches regex)"),
                ('MATERIAL', "Material Name", "Object uses a material name that contains this text (or matches regex)"),
                ('TYPE', "Type", "Object type"),
                ('EXPR', "Expression", (
                    "Scripted expression (using 'self' for the object) eg:\n"
                    "  self.type == 'MESH' and len(self.data.vertices) > 20"
                )
                ),
            ),
        )
    
        use_invert: BoolProperty(
            name="Invert",
            description="Match when the rule isn't met",
        )
    
        # ------------------
        # Matching Variables
    
        # shared by all name matching
        match_name: StringProperty(
            name="Match Name",
        )
        use_match_regex: BoolProperty(
            name="Regex",
            description="Use regular expressions for pattern matching",
        )
        # type == 'TYPE'
        match_object_type: EnumProperty(
            name="Object Type",
            items=([(i.identifier, i.name, "")
                    for i in bpy.types.Object.bl_rna.properties['type'].enum_items]
                   )
        )
        # type == 'EXPR'
        match_expr: StringProperty(
            name="Expression",
            description="Python expression, where 'self' is the object variable"
        )
    
    
    classes = (
        OBJECT_PT_color_rules,
        OBJECT_OT_color_rules_add,
        OBJECT_OT_color_rules_remove,
        OBJECT_OT_color_rules_move,
        OBJECT_OT_color_rules_assign,
        OBJECT_OT_color_rules_select,
        OBJECT_UL_color_rule,
        ColorRule,
    )
    
    
    def register():
        for cls in classes:
            bpy.utils.register_class(cls)
    
        bpy.types.Scene.color_rules = CollectionProperty(type=ColorRule)
        bpy.types.Scene.color_rules_active_index = IntProperty()
    
    
    def unregister():
        for cls in classes:
            bpy.utils.unregister_class(cls)
    
        del bpy.types.Scene.color_rules