Advertisement
Guest User

Blender: Swap UV Channels up or down

a guest
Jan 14th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. class op_uv_channel_move(bpy.types.Operator):
  2.     bl_idname = "uv.textools_uv_channel_move"
  3.     bl_label = "Move UV Channel"
  4.     bl_description = "Move UV channel up or down"
  5.  
  6.     is_down = bpy.props.BoolProperty(default=False)
  7.  
  8.     @classmethod
  9.     def poll(cls, context):
  10.         if bpy.context.active_object == None:
  11.             return False
  12.         if bpy.context.active_object.type != 'MESH':
  13.             return False
  14.         if len(bpy.context.object.data.uv_layers) <= 1:
  15.             return False
  16.  
  17.         return True
  18.  
  19.     def execute(self, context):
  20.         uv_textures = bpy.context.object.data.uv_textures
  21.  
  22.         if uv_textures.active_index == 0 and not self.is_down:
  23.             return {'FINISHED'}
  24.         elif uv_textures.active_index == len(uv_textures)-1 and self.is_down:
  25.             return {'FINISHED'}
  26.  
  27.         def get_index(name):
  28.             return ([i for i in range(len(uv_textures)) if uv_textures[i].name == name])[0]
  29.  
  30.         def move_bottom(name):
  31.             # Set index
  32.             uv_textures.active_index = get_index(name)
  33.             # Copy (to bottom)
  34.             bpy.ops.mesh.uv_texture_add()
  35.             # Delete previous
  36.             uv_textures.active_index = get_index(name)
  37.             bpy.ops.mesh.uv_texture_remove()
  38.             # Rename new
  39.             uv_textures.active_index = len(uv_textures)-1
  40.             uv_textures.active.name = name
  41.  
  42.         count = len(uv_textures)
  43.  
  44.         index_A = uv_textures.active_index
  45.         index_B = index_A + (1 if self.is_down else -1)
  46.  
  47.         if not self.is_down:
  48.             # Move up
  49.             for n in [uv_textures[i].name for i in range(index_B, count) if i != index_A]:
  50.                 move_bottom(n)
  51.             bpy.context.scene.texToolsSettings.uv_channel = str(index_B)
  52.  
  53.         elif self.is_down:
  54.             # Move down
  55.             for n in [uv_textures[i].name for i in range(index_A, count) if i != index_B]:
  56.                 move_bottom(n)
  57.             bpy.context.scene.texToolsSettings.uv_channel = str(index_B)
  58.  
  59.         return {'FINISHED'}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement