TexTools is a UV and Texture tool set for 3dsMax created several years ago. This open repository will port in time several of the UV tools to Blender in python. http://renderhjs.net/textools/blender
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.8 KiB

import bpy
import bmesh
import operator
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_color
class op(bpy.types.Operator):
bl_idname = "uv.textools_color_assign"
bl_label = "Assign Color"
bl_description = "Assign color to selected objects or faces in edit mode."
bl_options = {'REGISTER', 'UNDO'}
index : bpy.props.IntProperty(description="Color Index", default=0)
def poll(cls, context):
if not bpy.context.active_object:
return False
if bpy.context.active_object not in bpy.context.selected_objects:
return False
if bpy.context.active_object.type != 'MESH':
return False
#Only in UV editor mode
if bpy.context.area.type != 'IMAGE_EDITOR':
return False
return True
def execute(self, context):
assign_color(self, context, self.index)
return {'FINISHED'}
def assign_color(self, context, index):
selected_obj = bpy.context.selected_objects.copy()
previous_mode = 'OBJECT'
if len(selected_obj) == 1:
previous_mode = bpy.context.active_object.mode
for obj in selected_obj:
# Select object
obj.select_set( state = True, view_layer = None)
bpy.context.view_layer.objects.active = obj
# Enter Edit mode
bm = bmesh.from_edit_mesh(obj.data);
faces = []
#Assign to all or just selected faces?
if previous_mode == 'EDIT':
faces = [face for face in bm.faces if face.select]
faces = [face for face in bm.faces]
if previous_mode == 'OBJECT':
# Verify material slots
for i in range(index+1):
if index >= len(obj.material_slots):
utilities_color.assign_slot(obj, index)
# Assign to selection
obj.active_material_index = index
#Change View mode to MATERIAL
# for area in bpy.context.screen.areas:
# if area.type == 'VIEW_3D':
# for space in area.spaces:
# if space.type == 'VIEW_3D':
# space.shading.type = 'MATERIAL'
# restore mode
for obj in selected_obj:
obj.select_set( state = True, view_layer = None)