Skip to content

Commit

Permalink
feat: allow ID Materials to be actioned in both object and edit mode …
Browse files Browse the repository at this point in the history
…(face select)
  • Loading branch information
tristan-hm committed Sep 18, 2022
1 parent bf9d5c1 commit 021efe2
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packaging/create_id_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# ---

import bpy
import bmesh
import numpy
from random import choice

Expand Down Expand Up @@ -63,7 +64,7 @@ class ND_OT_create_id_material(bpy.types.Operator):

@classmethod
def poll(cls, context):
if context.mode == 'OBJECT':
if context.mode in ['OBJECT', 'EDIT_MESH']:
return len(context.selected_objects) >= 1 and all(obj.type == 'MESH' for obj in context.selected_objects)


Expand All @@ -86,8 +87,25 @@ def execute(self, context):
else:
material = bpy.data.materials[self.material_name]

for object in context.selected_objects:
object.active_material = material
if context.mode == 'OBJECT':
for object in context.selected_objects:
object.data.materials.clear()
object.active_material = material

if context.mode == 'EDIT_MESH':
for object in context.selected_objects:
previous_material_names = [m.name for m in object.material_slots]
if self.material_name not in previous_material_names:
object.data.materials.append(material)

active_materials = [slot.name for slot in object.material_slots]

bm = bmesh.from_edit_mesh(object.data)
for face in bm.faces:
if face.select:
face.material_index = active_materials.index(self.material_name)

bmesh.update_edit_mesh(object.data)

return {'FINISHED'}

Expand Down

0 comments on commit 021efe2

Please sign in to comment.