Fix #106366: Handle exceptions in add fur operator

Give errors in a few cases:
- The mesh has no UV map
- The faces have no area
- The applied modifier has no curve data (it may have been modified)

Use errors instead of cancelling the operator completely so the
operator can still do something useful when many meshes are
selected and only some fail.

Pull Request: https://projects.blender.org/blender/blender/pulls/106823
This commit is contained in:
Hans Goudey
2023-04-11 19:19:41 +02:00
committed by Hans Goudey
parent 03c4173d81
commit c7995f3185

View File

@@ -121,8 +121,16 @@ class QuickFur(ObjectModeOperator, Operator):
material = bpy.data.materials.new("Fur Material")
mesh_with_zero_area = False
mesh_missing_uv_map = False
modifier_apply_error = False
for mesh_object in mesh_objects:
mesh = mesh_object.data
if len(mesh.uv_layers) == 0:
mesh_missing_uv_map = True
continue
with context.temp_override(active_object=mesh_object):
bpy.ops.object.curves_empty_hair_add()
curves_object = context.active_object
@@ -132,7 +140,11 @@ class QuickFur(ObjectModeOperator, Operator):
area = 0.0
for poly in mesh.polygons:
area += poly.area
density = count / area
if area == 0.0:
mesh_with_zero_area = True
density = 10
else:
density = count / area
generate_modifier = curves_object.modifiers.new(name="Generate", type='NODES')
generate_modifier.node_group = generate_group
@@ -166,7 +178,10 @@ class QuickFur(ObjectModeOperator, Operator):
if self.apply_hair_guides:
with context.temp_override(object=curves_object):
bpy.ops.object.modifier_apply(modifier=generate_modifier.name)
try:
bpy.ops.object.modifier_apply(modifier=generate_modifier.name)
except:
modifier_apply_error = True
curves_object.modifiers.move(0, len(curves_object.modifiers) - 1)
@@ -174,6 +189,13 @@ class QuickFur(ObjectModeOperator, Operator):
for modifier in curves_object.modifiers:
modifier.node_group = modifier.node_group
if mesh_with_zero_area:
self.report({'WARNING'}, "Mesh has no face area")
if mesh_missing_uv_map:
self.report({'WARNING'}, "Mesh UV map required")
if modifier_apply_error and not mesh_with_zero_area:
self.report({'WARNING'}, "Unable to apply \"Generate\" modifier")
return {'FINISHED'}