Skip to content

Commit

Permalink
Merge pull request #4064 from Bforartists/issue#4063
Browse files Browse the repository at this point in the history
Issue#4063 - Properties Editor: Make UI consistent across various "Add ..." menus
  • Loading branch information
esnosy committed Feb 4, 2024
2 parents ab65a7c + 3f4aa69 commit f321673
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 38 deletions.
1 change: 1 addition & 0 deletions scripts/startup/bl_ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"properties_world",
"properties_collection",
"generic_ui_list",
"generic_column_menu",

# Generic Space Modules
#
Expand Down
49 changes: 49 additions & 0 deletions scripts/startup/bl_ui/generic_column_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import bpy


def fetch_op_data(class_name):
type_class = getattr(bpy.types, class_name)
type_props = type_class.bl_rna.properties["type"]

OPERATOR_DATA = {
enum_it.identifier: (enum_it.name, enum_it.icon)
for enum_it in type_props.enum_items_static
}

TRANSLATION_CONTEXT = type_props.translation_context

return (OPERATOR_DATA, TRANSLATION_CONTEXT)


class GenericColumnMenu:
bl_label = ""
bl_options = {'SEARCH_ON_KEY_PRESS'}

search_header = ""

@classmethod
def draw_operator_column(cls, layout, header, types, icon='NONE'):
text_ctxt = cls.TRANSLATION_CONTEXT
col = layout.column()

if layout.operator_context == 'INVOKE_REGION_WIN':
col.label(text=cls.search_header)
else:
col.label(text=header, icon=icon)
col.separator()

for op_type in types:
label, op_icon = cls.OPERATOR_DATA[op_type]
col.operator(cls.op_id, text=label, icon=op_icon, text_ctxt=text_ctxt).type = op_type


class InvokeMenuOperator:
bl_options = {'INTERNAL'}

@classmethod
def poll(cls, context):
space = context.space_data
return space and space.type == cls.space_type and space.context == cls.space_context

def invoke(self, context, event):
return bpy.ops.wm.call_menu(name=self.menu_id)
74 changes: 68 additions & 6 deletions scripts/startup/bl_ui/properties_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later

from bpy.types import Panel
from bpy.types import Panel, Menu, Operator
from bpy.app.translations import contexts as i18n_contexts

from bl_ui.generic_column_menu import GenericColumnMenu, fetch_op_data, InvokeMenuOperator

class ObjectConstraintPanel:
bl_context = "constraint"
Expand All @@ -30,12 +30,41 @@ class OBJECT_PT_constraints(ObjectConstraintPanel, Panel):

def draw(self, _context):
layout = self.layout

layout.operator_menu_enum("object.constraint_add", "type", text="Add Object Constraint")
layout.operator("object.add_constraints_menu", icon='ADD')

layout.template_constraints(use_bone_constraints=False)


class OBJECT_MT_constraint_add(GenericColumnMenu, Menu):
bl_description = "Add a constraint to the active object"

op_id = "object.constraint_add"
OPERATOR_DATA, TRANSLATION_CONTEXT = fetch_op_data(class_name="Constraint")
search_header = "Object Constraint"

def draw(self, _context):
layout = self.layout.row()

self.draw_operator_column(layout, header="Motion Tracking",
types=('CAMERA_SOLVER', 'FOLLOW_TRACK', 'OBJECT_SOLVER'))
self.draw_operator_column(layout, header="Transform",
types=('COPY_LOCATION', 'COPY_ROTATION', 'COPY_SCALE', 'COPY_TRANSFORMS', 'LIMIT_DISTANCE', 'LIMIT_LOCATION', 'LIMIT_ROTATION', 'LIMIT_SCALE', 'MAINTAIN_VOLUME', 'TRANSFORM', 'TRANSFORM_CACHE'))
self.draw_operator_column(layout, header="Tracking",
types=('CLAMP_TO', 'DAMPED_TRACK', 'LOCKED_TRACK', 'STRETCH_TO', 'TRACK_TO'))
self.draw_operator_column(layout, header="Relationship",
types=('ACTION', 'ARMATURE', 'CHILD_OF', 'FLOOR', 'FOLLOW_PATH', 'PIVOT', 'SHRINKWRAP'))


class OBJECT_OT_add_constraints_menu(InvokeMenuOperator, Operator):
bl_idname = "object.add_constraints_menu"
bl_label = "Add Object Constraint"
bl_description = "Add a constraint to the active object"

menu_id = "OBJECT_MT_constraint_add"
space_type = 'PROPERTIES'
space_context = 'CONSTRAINT'


class BONE_PT_constraints(BoneConstraintPanel, Panel):
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
Expand All @@ -44,12 +73,41 @@ class BONE_PT_constraints(BoneConstraintPanel, Panel):

def draw(self, _context):
layout = self.layout

layout.operator_menu_enum("pose.constraint_add", "type", text="Add Bone Constraint")
layout.operator("bone.add_constraints_menu", icon='ADD')

layout.template_constraints(use_bone_constraints=True)


class BONE_MT_constraint_add(GenericColumnMenu, Menu):
bl_description = "Add a constraint to the active bone"

op_id = "pose.constraint_add"
OPERATOR_DATA, TRANSLATION_CONTEXT = fetch_op_data(class_name="Constraint")
search_header = "Bone Constraint"

def draw(self, _context):
layout = self.layout.row()

self.draw_operator_column(layout, header="Motion Tracking",
types=('CAMERA_SOLVER', 'FOLLOW_TRACK', 'OBJECT_SOLVER'))
self.draw_operator_column(layout, header="Transform",
types=('COPY_LOCATION', 'COPY_ROTATION', 'COPY_SCALE', 'COPY_TRANSFORMS', 'LIMIT_DISTANCE', 'LIMIT_LOCATION', 'LIMIT_ROTATION', 'LIMIT_SCALE', 'MAINTAIN_VOLUME', 'TRANSFORM', 'TRANSFORM_CACHE'))
self.draw_operator_column(layout, header="Tracking",
types=('CLAMP_TO', 'DAMPED_TRACK', 'IK', 'LOCKED_TRACK', 'SPLINE_IK', 'STRETCH_TO', 'TRACK_TO'))
self.draw_operator_column(layout, header="Relationship",
types=('ACTION', 'ARMATURE', 'CHILD_OF', 'FLOOR', 'FOLLOW_PATH', 'PIVOT', 'SHRINKWRAP'))


class BONE_OT_add_constraints_menu(InvokeMenuOperator, Operator):
bl_idname = "bone.add_constraints_menu"
bl_label = "Add Bone Constraint"
bl_description = "Add a constraint to the active bone"

menu_id = "BONE_MT_constraint_add"
space_type = 'PROPERTIES'
space_context = 'BONE_CONSTRAINT'


# Parent class for constraint panels, with templates and drawing methods
# shared between the bone and object constraint panels
class ConstraintButtonsPanel:
Expand Down Expand Up @@ -1868,7 +1926,11 @@ def draw(self, context):
classes = (
# Object Panels
OBJECT_PT_constraints,
OBJECT_MT_constraint_add,
OBJECT_OT_add_constraints_menu,
BONE_PT_constraints,
BONE_MT_constraint_add,
BONE_OT_add_constraints_menu,
OBJECT_PT_bChildOfConstraint,
OBJECT_PT_bTrackToConstraint,
OBJECT_PT_bKinematicConstraint,
Expand Down

0 comments on commit f321673

Please sign in to comment.