Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created October 21, 2024 05:04
Show Gist options
  • Save CGArtPython/836337be031ae66342ee458e553644cc to your computer and use it in GitHub Desktop.
Save CGArtPython/836337be031ae66342ee458e553644cc to your computer and use it in GitHub Desktop.
A draft of an simple add-on that colorize faces of a mesh based on this tutorial https://www.youtube.com/watch?v=WnZX--idSQI&lc=UgzNlDw63xzAORn-3Q14AaABAg
# give Python access to Blender's functionality
import bpy
# give Python access to Blender's mesh editing functionality
import bmesh
# extend Python functionality to generate random numbers
import random
# import the Operator class from the bpy.types module
from bpy.types import Operator
def get_random_color():
"""generate a random color"""
red = random.random() # creates a value from 0.0 to 1.0
green = random.random()
blue = random.random()
alpha = 1.0
color = (red, green, blue, alpha)
return color
def generate_random_color_materials(obj, count):
"""create and assign materials to the object"""
for i in range(count):
mat = bpy.data.materials.new(name=f"material_{i}")
mat.diffuse_color = get_random_color()
obj.data.materials.append(mat)
def add_ico_sphere():
"""add an ico sphere"""
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=3)
return bpy.context.active_object
def assign_materials_to_faces(obj):
"""iterates over all the faces of the object and
assigns a random material to the face"""
# turn ON Edit Mode
bpy.ops.object.editmode_toggle()
# deselect all faces
bpy.ops.mesh.select_all()
# get geometry data from mesh object
bmesh_obj = bmesh.from_edit_mesh(obj.data)
# get the number of materials assigned to the object
material_count = len(obj.data.materials)
# iterate through each face of the mesh
for face in bmesh_obj.faces:
# randomly select a material
obj.active_material_index = random.randint(0, material_count)
# select the face and assign the active material
face.select = True
bpy.ops.object.material_slot_assign()
face.select = False
# turn OFF Edit Mode
bpy.ops.object.editmode_toggle()
def apply_materials_to_faces(obj, material_count):
"""iterates over all the faces of the object and
assigns a random material to"""
generate_random_color_materials(obj, material_count)
assign_materials_to_faces(obj)
class MESH_OT_colorize_faces(Operator):
"""Create a new Mesh Object"""
bl_idname = "mesh.colorize_faces"
bl_label = "Add Colorized Mesh Faces"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
return context.active_object is not None and context.active_object.type == 'MESH'
def execute(self, context):
if not context.active_object:
return {"CANCELLED"}
apply_materials_to_faces(context.active_object, material_count=10)
return {"FINISHED"}
class VIEW3D_PT_my_custom_panel(bpy.types.Panel): # class naming convention ‘CATEGORY_PT_name’
# where to add the panel in the UI
bl_space_type = "VIEW_3D" # 3D Viewport area (find list of values here https://docs.blender.org/api/current/bpy_types_enum_items/space_type_items.html#rna-enum-space-type-items)
bl_region_type = "UI" # Sidebar region (find list of values here https://docs.blender.org/api/current/bpy_types_enum_items/region_type_items.html#rna-enum-region-type-items)
bl_category = "My Custom Panel category" # found in the Sidebar
bl_label = "My Custom Panel label" # found at the top of the Panel
def draw(self, context):
"""define the layout of the panel"""
row = self.layout.row()
row.operator("mesh.colorize_faces", text="Colorize Faces")
def register():
bpy.utils.register_class(MESH_OT_colorize_faces)
bpy.utils.register_class(VIEW3D_PT_my_custom_panel)
def unregister():
bpy.utils.unregister_class(VIEW3D_PT_my_custom_panel)
bpy.utils.unregister_class(MESH_OT_colorize_faces)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment