Skip to content

Commit

Permalink
Bring back Graft face map output
Browse files Browse the repository at this point in the history
  • Loading branch information
greisane committed Dec 19, 2023
1 parent 0e47588 commit a61c1c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions mesh/attribute_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GRET_OT_attribute_to_selection(bpy.types.Operator):
"""Set the current face selection from a boolean face attribute"""

bl_idname = 'gret.attribute_to_selection'
bl_label = "Select Faces By Values"
bl_label = "Select Faces by Values"
bl_options = {'REGISTER', 'UNDO'}

@classmethod
Expand Down Expand Up @@ -50,7 +50,7 @@ class GRET_OT_attribute_from_selection(bpy.types.Operator):
"""Update or create a boolean face attribute from the current edit mode face selection"""

bl_idname = 'gret.attribute_from_selection'
bl_label = "Face Selection To Values"
bl_label = "Face Selection to Values"
bl_context = 'mesh_edit'
bl_options = {'REGISTER', 'UNDO'}

Expand Down
26 changes: 14 additions & 12 deletions mesh/graft.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from math import pi
import bmesh
import bpy
import numpy as np

from ..math import get_dist_sq
from .helpers import edit_mesh_elements, bmesh_vertex_group_bleed
from .helpers import edit_mesh_elements, bmesh_vertex_group_bleed, get_face_map_attribute
from ..helpers import with_object, get_modifier, get_vgroup, select_only, instant_modifier
from ..operator import SaveContext

Expand Down Expand Up @@ -95,20 +96,13 @@ def do_graft(context, save, obj, dst_obj, expand=0, cuts=0, blend_distance=0.0,
try:
ret = bmesh.ops.bridge_loops(bm, edges=edges1+edges2, use_pairs=False,
use_cyclic=False, use_merge=False, merge_factor=0.5, twist_offset=0)
new_faces = ret['faces']
new_face_indices = [face.index for face in ret['faces']]
# for face in new_faces:
# face.smooth = True
except RuntimeError:
bm.free()
raise GraftError("Couldn't bridge loops.")

# If requested, fill a face map with the new faces
# Disabled for 4.0 since there is no actual replacement for face-based selection haha
# if face_map_name:
# face_map = obj.face_maps.get(face_map_name) or obj.face_maps.new(name=face_map_name)
# for face in new_faces:
# face[fm_layer] = face_map.index

if cuts > 0:
bmesh.ops.subdivide_edges(bm, edges=ret['edges'], smooth=1.0, smooth_falloff='LINEAR', cuts=cuts)

Expand All @@ -125,6 +119,14 @@ def do_graft(context, save, obj, dst_obj, expand=0, cuts=0, blend_distance=0.0,
bm.to_mesh(obj.data)
bm.free()

# If requested, fill a face map with the new faces
if face_map_name:
attr = get_face_map_attribute(obj, face_map_name)
values = np.empty(len(attr.data), dtype=bool)
attr.data.foreach_get('value', values)
values[new_face_indices] = True
attr.data.foreach_set('value', values)

# Transfer stuff
if copy_normals:
obj.data.use_auto_smooth = True
Expand Down Expand Up @@ -195,8 +197,8 @@ class GRET_OT_graft(bpy.types.Operator):
)
face_map_name: bpy.props.StringProperty(
name="Face Map Name",
description="Optional name of a face map that contains the new geometry",
default="Grafts",
description="Optional name of a boolean face attribute that contains the new geometry",
default="",
)
vertex_group_name: bpy.props.StringProperty(
name="Vertex Group Name",
Expand Down Expand Up @@ -312,7 +314,7 @@ def draw(self, context):

# layout.prop_search(self, 'vertex_group_name', obj, 'vertex_groups')
layout.prop(self, 'vertex_group_name', icon='GROUP_VERTEX')
# layout.prop(self, 'face_map_name', icon='FACE_MAPS')
layout.prop(self, 'face_map_name', icon='FACE_MAPS')

row = layout.row(align=True, heading="Copy")
row.prop(self, 'copy_normals', text="Norms.", toggle=1)
Expand Down
9 changes: 9 additions & 0 deletions mesh/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def refresh_active_color_attribute(mesh):
if mesh.color_attributes.render_color_index < 0:
mesh.color_attributes.render_color_index = 0

def get_face_map_attribute(obj, name):
"""Ensures that a boolean face attribute with the given name exists."""

assert obj.type == 'MESH'
attr = obj.data.attributes.get(name)
if not attr or attr.domain != 'FACE' or attr.data_type != 'BOOLEAN':
attr = obj.data.attributes.new(name, type='BOOLEAN', domain='FACE')
return attr

def clear_object_data(obj, vertex_groups=True, shape_keys=True, face_maps=True, constraints=True,
custom_properties=True, uv_layers=True, vertex_colors=True, attributes=True, materials=True):
# Clear object data
Expand Down

0 comments on commit a61c1c4

Please sign in to comment.