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

texture_rename.py « materials_utils - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 585a3a7d90cdd90b11a8e85e7e0628598891f5b9 (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
# gpl: author Yadoob
# -*- coding: utf-8 -*-

import bpy
from bpy.types import (
    Operator,
    Panel,
)
from bpy.props import (
    BoolProperty,
    StringProperty,
)
from .warning_messages_utils import (
    warning_messages,
    c_data_has_images,
)


class TEXTURE_OT_patern_rename(Operator):
    bl_idname = "texture.patern_rename"
    bl_label = "Texture Renamer"
    bl_description = ("Replace the Texture names pattern with the attached Image ones\n"
                      "Works on all Textures (Including Brushes)\n"
                      "The First field - the name pattern to replace\n"
                      "The Second - search for existing names")
    bl_options = {'REGISTER', 'UNDO'}

    def_name = "Texture"    # default name
    is_not_undo = False     # prevent drawing props on undo

    named = StringProperty(
        name="Search for name",
        description="Enter the name pattern or choose the one from the dropdown list below",
        default=def_name
    )
    replace_all = BoolProperty(
        name="Replace all",
        description="Replace all the Textures in the data with the names of the images attached",
        default=False
    )

    @classmethod
    def poll(cls, context):
        return c_data_has_images()

    def draw(self, context):
        layout = self.layout
        if not self.is_not_undo:
            layout.label(text="*Only Undo is available*", icon="INFO")
            return

        layout.prop(self, "replace_all")

        box = layout.box()
        box.enabled = not self.replace_all
        box.prop(self, "named", text="Name pattern", icon="SYNTAX_ON")

        box = layout.box()
        box.enabled = not self.replace_all
        box.prop_search(self, "named", bpy.data, "textures")

    def invoke(self, context, event):
        self.is_not_undo = True
        return context.window_manager.invoke_props_dialog(self)

    def check(self, context):
        return self.is_not_undo

    def execute(self, context):
        errors = []     # collect texture names without images attached
        tex_count = len(bpy.data.textures)

        for texture in bpy.data.textures:
            try:
                is_allowed = self.named in texture.name if not self.replace_all else True
                if texture and is_allowed and texture.type in {"IMAGE"}:
                    textname = ""
                    img = (bpy.data.textures[texture.name].image if bpy.data.textures[texture.name] else None)
                    if not img:
                        errors.append(str(texture.name))
                    for word in img.name:
                        if word != ".":
                            textname = textname + word
                        else:
                            break
                    texture.name = textname
                if texture.type != "IMAGE":  # rename specific textures as clouds, environment map...
                    texture.name = texture.type.lower()
            except:
                continue

        if tex_count == 0:
            warning_messages(self, 'NO_TEX_RENAME')
        elif errors:
            warning_messages(self, 'TEX_RENAME_F', errors, 'TEX')

        # reset name to default
        self.named = self.def_name

        self.is_not_undo = False

        return {'FINISHED'}


class TEXTURE_PT_rename_panel(Panel):
    bl_label = "Texture Rename"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "texture"

    def draw(self, context):
        layout = self.layout
        layout.operator("texture.patern_rename")


def register():
    bpy.utils.register_class(TEXTURE_OT_patern_rename)
    bpy.utils.register_class(TEXTURE_PT_rename_panel)


def unregister():
    bpy.utils.unregister_class(TEXTURE_PT_rename_panel)
    bpy.utils.unregister_class(TEXTURE_OT_patern_rename)


if __name__ == "__main__":
    register()