1
0
mirror of https://github.com/drewcassidy/TexTools-Blender synced 2024-06-11 00:26:53 +00:00
Blender-TexTools/op_color_select.py

73 lines
2.0 KiB
Python
Raw Normal View History

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