Skip to content

Commit

Permalink
Preselecting vertex group on mesh creation, for issue #186
Browse files Browse the repository at this point in the history
  • Loading branch information
joepal1976 committed May 19, 2024
1 parent 255b2ce commit b7a2abc
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 17 deletions.
44 changes: 33 additions & 11 deletions src/mpfb/services/meshservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# - delete verts
# - recalculate_face_normals


class MeshService:
"""MeshService contains various functions for working meshes, vertex groups, weights and similar."""

Expand All @@ -38,15 +39,15 @@ def create_sample_object(name="sample_object", link=True):
# 6 7 8

vertices = [
(-1, 0, 1), # 0
( 0, 0, 1), # 1
( 1, 0, 1), # 2
(-1, 0, 0), # 3
( 0, 0, 0), # 4
( 1, 0, 0), # 5
(-1, 0, -1), # 6
( 0, 0, -1), # 7
( 1, 0, -1) # 8
(-1, 0, 1), # 0
(0, 0, 1), # 1
(1, 0, 1), # 2
(-1, 0, 0), # 3
(0, 0, 0), # 4
(1, 0, 0), # 5
(-1, 0, -1), # 6
(0, 0, -1), # 7
(1, 0, -1) # 8
]

edges = []
Expand Down Expand Up @@ -206,8 +207,8 @@ def closest_vertices(focus_obj, focus_vert_idx, target_obj, target_obj_kdtree, n
if world_coordinates:
coord = focus_obj.matrix_world @ coord

#shift_dist = focus_obj.location - target_obj.location
#if shift_dist.length > 0.0001:
# shift_dist = focus_obj.location - target_obj.location
# if shift_dist.length > 0.0001:
# coord.x = coord.x - shift_dist.x
# coord.y = coord.y - shift_dist.y
# coord.z = coord.z - shift_dist.z
Expand Down Expand Up @@ -298,3 +299,24 @@ def get_mesh_cross_references(mesh_object, after_modifiers=True, build_faces_by_
_LOG.enter()
from mpfb.entities.meshcrossref import MeshCrossRef
return MeshCrossRef(mesh_object, after_modifiers=after_modifiers, build_faces_by_group_reference=build_faces_by_group_reference)

@staticmethod
def select_all_vertices_in_vertex_group_for_active_object(vertex_group_name, deselect_other=True):
"""Select all vertices in a specific vertex group for the currently active object.
The object needs to be active."""
_LOG.enter()

mesh_object = bpy.context.active_object
_LOG.debug("Active object", mesh_object)

bpy.ops.mesh.select_mode(type="VERT")
if deselect_other:
bpy.ops.mesh.select_all(action='DESELECT')

if vertex_group_name:
for group in mesh_object.vertex_groups:
if group.name == vertex_group_name:
mesh_object.vertex_groups.active_index = group.index

bpy.ops.object.vertex_group_select()

5 changes: 4 additions & 1 deletion src/mpfb/ui/newhuman/frompresetspanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
PRESETS_HUMAN_PROPERTIES_DIR = os.path.join(_LOC, "properties")
PRESETS_HUMAN_PROPERTIES = SceneConfigSet.from_definitions_in_json_directory(PRESETS_HUMAN_PROPERTIES_DIR, prefix="FPR_")


def _populate_settings(self, context):
_LOG.enter()
_LOG.trace("Context is scene", isinstance(context, bpy.types.Scene))
return HumanService.get_list_of_human_presets(use_cache=False)


_SETTINGS_LIST_PROP = {
"type": "enum",
"name": "available_presets",
Expand All @@ -28,6 +30,7 @@ def _populate_settings(self, context):
}
PRESETS_HUMAN_PROPERTIES.add_property(_SETTINGS_LIST_PROP, _populate_settings)


class MPFB_PT_From_Presets_Panel(Abstract_Panel):
"""Create human from preset main panel."""

Expand All @@ -44,6 +47,7 @@ def _create(self, scene, layout):
"scale_factor",
"override_rig",
"override_skin_model",
"preselect_group",
"detailed_helpers",
"extra_vertex_groups",
"mask_helpers",
Expand All @@ -63,4 +67,3 @@ def draw(self, context):

ClassManager.add_class(MPFB_PT_From_Presets_Panel)


5 changes: 3 additions & 2 deletions src/mpfb/ui/newhuman/newhumanpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
NEW_HUMAN_PROPERTIES_DIR = os.path.join(_LOC, "properties")
NEW_HUMAN_PROPERTIES = SceneConfigSet.from_definitions_in_json_directory(NEW_HUMAN_PROPERTIES_DIR, prefix="NH_")


class MPFB_PT_NewHuman_Panel(bpy.types.Panel):
"""Create human from scratch main panel."""

Expand All @@ -34,7 +35,8 @@ def _create(self, scene, layout):
"scale_factor",
"detailed_helpers",
"extra_vertex_groups",
"mask_helpers"
"mask_helpers",
"preselect_group"
])
box.operator('mpfb.create_human')

Expand Down Expand Up @@ -73,4 +75,3 @@ def draw(self, context):

ClassManager.add_class(MPFB_PT_NewHuman_Panel)


10 changes: 10 additions & 0 deletions src/mpfb/ui/newhuman/operators/createhuman.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from mpfb.services.targetservice import TargetService
from mpfb.services.humanservice import HumanService
from mpfb.services.systemservice import SystemService
from mpfb.services.meshservice import MeshService
from mpfb.ui.mpfboperator import MpfbOperator
from mpfb import ClassManager

_LOG = LogService.get_logger("newhuman.createhuman")


class MPFB_OT_CreateHumanOperator(MpfbOperator):
"""Create a new human"""
bl_idname = "mpfb.create_human"
Expand Down Expand Up @@ -163,10 +165,18 @@ def hardened_execute(self, context):
# Otherwise all targets will be set to 100% when entering edit mode
basemesh.use_shape_key_edit_mode = True

preselect_group = NEW_HUMAN_PROPERTIES.get_value("preselect_group", entity_reference=context.scene)
if not preselect_group:
preselect_group = None

bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = basemesh
basemesh.select_set(True)

bpy.ops.object.mode_set(mode='EDIT', toggle=False)
MeshService.select_all_vertices_in_vertex_group_for_active_object(preselect_group, deselect_other=True)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

return {'FINISHED'}


Expand Down
24 changes: 21 additions & 3 deletions src/mpfb/ui/newhuman/operators/humanfrompresets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from mpfb.services.locationservice import LocationService
from mpfb.services.humanservice import HumanService
from mpfb.services.objectservice import ObjectService
from mpfb.services.meshservice import MeshService
from mpfb.services.systemservice import SystemService
from mpfb.ui.mpfboperator import MpfbOperator
from mpfb import ClassManager

_LOG = LogService.get_logger("newhuman.humanfrompresets")


class MPFB_OT_HumanFromPresetsOperator(MpfbOperator):
"""Create a new human from presets"""
bl_idname = "mpfb.human_from_presets"
Expand Down Expand Up @@ -62,15 +64,31 @@ def hardened_execute(self, context):

_LOG.debug("Basemesh", basemesh)

preselect_group = PRESETS_HUMAN_PROPERTIES.get_value("preselect_group", entity_reference=context.scene)
if not preselect_group:
preselect_group = None

proxy = ObjectService.find_object_of_type_amongst_nearest_relatives(basemesh, "Proxymeshes")
if proxy:
bpy.context.view_layer.objects.active = proxy
proxy.select_set(True)
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
MeshService.select_all_vertices_in_vertex_group_for_active_object(preselect_group, deselect_other=True)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = basemesh
basemesh.select_set(True)

bpy.ops.object.mode_set(mode='EDIT', toggle=False)
MeshService.select_all_vertices_in_vertex_group_for_active_object(preselect_group, deselect_other=True)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

rig = ObjectService.find_object_of_type_amongst_nearest_relatives(basemesh, mpfb_type_name="Skeleton")
if rig:
bpy.context.view_layer.objects.active = rig
basemesh.select_set(False)
rig.select_set(True)
bpy.context.view_layer.objects.active = rig
basemesh.select_set(False)
rig.select_set(True)

_LOG.time("Human created in")

Expand Down
7 changes: 7 additions & 0 deletions src/mpfb/ui/newhuman/properties/preselect_group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "string",
"name": "preselect_group",
"description": "The name of a a vertex group that should be selected per default in edit mode. This will come into effect for both base mesh and proxy",
"label": "Preselect group",
"default": ""
}

0 comments on commit b7a2abc

Please sign in to comment.