You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Blender-TexTools/op_color_select.py

73 lines
2.0 KiB
Python

import bpy
import bmesh
import operator
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_color
4 years ago
class op(bpy.types.Operator):
4 years ago
bl_idname = "uv.textools_color_select"
bl_label = "Assign Color"
bl_description = "Select faces by this color"
bl_options = {'REGISTER', 'UNDO'}
4 years ago
index: bpy.props.IntProperty(description="Color Index", default=0)
4 years ago
@classmethod
def poll(cls, context):
if not bpy.context.active_object:
return False
4 years ago
if bpy.context.active_object not in bpy.context.selected_objects:
return False
4 years ago
# Allow only 1 object selected
if len(bpy.context.selected_objects) != 1:
return False
4 years ago
if bpy.context.active_object.type != 'MESH':
return False
4 years ago
# Only in UV editor mode
4 years ago
if bpy.context.area.type != 'IMAGE_EDITOR':
return False
4 years ago
return True
4 years ago
4 years ago
def execute(self, context):
select_color(self, context, self.index)
return {'FINISHED'}
def select_color(self, context, index):
4 years ago
print("Color select "+str(index))
4 years ago
obj = bpy.context.active_object
4 years ago
4 years ago
# Check for missing slots, materials,..
if index >= len(obj.material_slots):
4 years ago
self.report({'ERROR_INVALID_INPUT'},
"No material slot for color '{}' found".format(index))
4 years ago
return
if not obj.material_slots[index].material:
4 years ago
self.report({'ERROR_INVALID_INPUT'},
"No material found for material slot '{}'".format(index))
return
4 years ago
if bpy.context.active_object.mode != 'EDIT':
bpy.ops.object.mode_set(mode='EDIT')
# Select faces
4 years ago
bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
4 years ago
bpy.ops.mesh.select_all(action='DESELECT')
for face in bm.faces:
if face.material_index == index:
face.select = True
4 years ago
bpy.utils.register_class(op)