Use URL icon, add Tip: prefix, increase lower margin
[blender-addons-contrib.git] / uv_utility.py
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; version 2
6 #  of the License.
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
20 bl_info = {
21     "name": "UV Utility",
22     "author": "Paul Geraskin",
23     "version": (0, 1),
24     "blender": (2, 70, 0),
25     "location": "View3D > ToolBar",
26     "description": "Change Index Of UVMap.",
27     "wiki_url": "",
28     "tracker_url": "",
29     "category": "UV"}
30
31
32 import bpy
33 from bpy.types import (Operator,
34                        Panel,
35                        PropertyGroup,
36                        )
37 from bpy.props import (IntProperty,
38                        StringProperty,
39                        BoolProperty,
40                        )
41
42
43 class UV_IC_Panel():
44     bl_space_type = 'VIEW_3D'
45     bl_region_type = 'TOOLS'
46     bl_category = 'Tools'
47     bl_label = "UV Utility"
48     bl_context = "objectmode"
49     bl_options = {'DEFAULT_CLOSED'}
50
51
52 class UV_IC_TexIndex(PropertyGroup):
53     bpy.types.Scene.UVTexIndex = IntProperty(
54         name="UVIndexToGet",
55         description="get UVIndex of selected objects",
56         min=1,
57         max=8,
58         default=1)
59
60     bpy.types.Scene.UVTexGetName = StringProperty(
61         name="UVNameToGet",
62              description="get new UVName of selected objects",
63              default="UVMap")
64
65     bpy.types.Scene.UVTexRenderActive = BoolProperty(
66         name="Set Render Active",
67         description="Set Render Active...",
68         default=False
69     )
70
71
72 class UV_IC_Base(UV_IC_Panel, Panel):
73     bl_context = "objectmode"
74     bl_label = "UV Utility"
75     bl_options = {"DEFAULT_CLOSED"}
76
77     def draw(self, context):
78
79         layout = self.layout
80
81         scene = context.scene
82         ob = context.object
83
84         col = layout.column(align=True)
85
86         row = layout.row(align=True)
87         row.operator("uvutil.change_index", text="Drop Active UV Back")
88
89         row = layout.row(align=True)
90         col = layout.column()
91         col.prop(scene, "UVTexRenderActive")
92
93         row = layout.row(align=True)
94         col = layout.column()
95         col.operator("uvutil.select_index", text="Select UVTexCoord")
96         col.prop(scene, "UVTexIndex", text="UVTexCoord")
97         row = layout.row(align=True)
98         row = layout.row(align=True)
99         col = layout.column()
100         col.operator("uvutil.select_name", text="Select UV Name")
101         col.prop(scene, "UVTexGetName", text="")
102         row = layout.row(align=True)
103         col = layout.column()
104         col.operator("uvutil.remove_active", text="Remove Active UV")
105
106
107 class UV_IC_ChangeIndex(Operator):
108     bl_idname = "uvutil.change_index"
109     bl_label = "Change Index"
110
111     def execute(self, context):
112         scene = context.scene
113
114         for theObj in context.selected_objects:
115             meshData = theObj.data
116
117             if theObj.type == 'MESH':
118                 if len(meshData.uv_textures) > meshData.uv_textures.active_index and meshData.uv_textures:
119                     # meshData.uv_textures.active_index = 0
120                     tmpuvmap = meshData.uv_textures.active
121                     tmpuvmap_name = tmpuvmap.name
122
123                     newuvmap = meshData.uv_textures.new()
124                     meshData.uv_textures.remove(tmpuvmap)
125
126                     droppedUV = meshData.uv_textures[
127                         len(meshData.uv_textures) - 1]
128                     droppedUV.name = tmpuvmap_name
129                     # droppedUV.active = True
130                     # if scene.UVTexRenderActive == True:
131                       # droppedUV.active_render = True
132
133         return{'FINISHED'}
134
135
136 class UV_IC_SelectIndex(Operator):
137     bl_idname = "uvutil.select_index"
138     bl_label = "Select Index"
139
140     def execute(self, context):
141         scene = context.scene
142
143         for theObj in context.selected_objects:
144             meshData = theObj.data
145             indexNew = scene.UVTexIndex - 1
146
147             if theObj.type == 'MESH':
148                 if len(meshData.uv_textures) > indexNew and meshData.uv_textures:
149                     meshData.uv_textures.active_index = indexNew
150
151                     if scene.UVTexRenderActive:
152                         meshData.uv_textures[indexNew].active_render = True
153
154         return{'FINISHED'}
155
156
157 class UV_IC_SelectName(Operator):
158     bl_idname = "uvutil.select_name"
159     bl_label = "Select Name"
160
161     def execute(self, context):
162         scene = context.scene
163
164         for theObj in context.selected_objects:
165             meshData = theObj.data
166             uvName = scene.UVTexGetName
167
168             if theObj.type == 'MESH':
169                 if meshData.uv_textures:
170                     uvToGet = meshData.uv_textures.get(uvName)
171
172                     if uvToGet is not None:
173                         uvToGet.active = True
174
175                         if scene.UVTexRenderActive:
176                             uvToGet.active_render = True
177
178         return{'FINISHED'}
179
180
181 class UV_IC_RemoveActiveUV(Operator):
182     bl_idname = "uvutil.remove_active"
183     bl_label = "Remove Active UV"
184
185     def execute(self, context):
186         scene = context.scene
187
188         for theObj in context.selected_objects:
189             meshData = theObj.data
190
191             if theObj.type == 'MESH':
192                 if meshData.uv_textures:
193                     activeIndex = meshData.uv_textures.active_index
194
195                     if len(meshData.uv_textures) > activeIndex:
196                         meshData.uv_textures.remove(
197                             meshData.uv_textures[activeIndex])
198
199         return{'FINISHED'}
200
201
202 def register():
203     bpy.utils.register_module(__name__)
204
205
206 def unregister():
207     bpy.utils.unregister_module(__name__)
208
209
210 if __name__ == "__main__":
211     register()