Post #5103: CSM v2 is now complete!

📋 Metadata

🏷️ Tags

macro hotkeys advanced solved

  • Macro Editor
  • Python Scripting
  • Command Tab
  • Custom Tab

💬 Content

CSM v2 is now complete!

It allows you to dynamically call corresponding menus based on the mode and type of the active object.

The basic specifications are the same as version 1, but there are some changes, so please check them out.

Changes

  • In v1, it could only be used in the Command tab, but in v2.1, draw_menu can now be used for the Custom Tab. Due to this change, calling via execute_script is now deprecated.

  • Changed to use context.mode instead of object.mode. This allows distinguishing between Edit Mesh mode, Edit Armature mode, and others.

  • For Empty objects, it can now identify Image, Instance, and Field. If you need to add other custom conditions, it’s easy to do so as the script is now structured.

gist.github.com

https://gist.github.com/Pluglug/ebdf6ad377922569a34cdeb71ea97ed3

autorun_context_sensitive_menu_v2.py
# Open context sensitive menu v2.3

## Usage
# Download context_sensitive_menu_v2.py and place it in the following directory, then restart Blender.
# `pie_menu_editor\scripts\autorun`
# This will make the functionality accessible through the `CSM` class.

# Example usage in the Command tab:
# ```
# CSM.from_context(context, prefix="My ", suffix=" Menu").open_menu()

This file has been truncated. show original

Usage

Download context_sensitive_menu_v2.py and place it in the following directory, then restart Blender.

pie_menu_editor\scripts\autorun

This will make the functionality accessible through the CSM class.

Example usage in the Command tab:

CSM.from_context(context, prefix="My ", suffix=" Menu").open_menu()

This command allows you to execute menus like:

  • My Mesh Menu
  • My Edit Armature Menu
  • My Paint Gpencil Menu

Example usage in the Custom tab:

col = L.column(); col.scale_x = 1.01; CSM.from_context(context, prefix="My ", suffix=" Panel").draw_menu(layout=col)

Similar to the previous example, you can display menus like:

  • My Vertex Panel
  • My Edge Panel
  • My Face Panel

Actual usage examples:

example_context_tab_menu.json (10.2 KB)

Available Keywords

Required Keywords

  • None Object: Menu called when no object is selected
  • Any Object: Menu called when no other conditions are met

If these two keywords are not used in a menu and no object is selected or an object without a corresponding menu is selected, an error will be displayed. In this case, you can either ignore the error or use the Poll method to prevent such situations from occurring.

Optional Keywords

Context mode items

Object type items

Custom Keywords

Mesh Edit Mode:

  • Vertex: Vertex selection mode
  • Edge: Edge selection mode
  • Face: Face selection mode

Empty Object:

  • Image: Display type is Image
  • Instance: Instance collection is registered
  • Field: Force Field is registered

(You can also add other types such as light types as needed)

Debug Options

The flag DBG_CSM at the beginning of the script controls debug log output.

# Output evaluation process
DBG_CSM = True

When this value is True, detailed logs will be displayed during script execution. This is useful for understanding the script’s behavior and investigating problems.

For example, in Pose mode with an Armature object selected, the log will output as follows:

ContextSensitiveMenu: Armature
  prefix: TAB:
  suffix:  (Drag)
Potential names: ['POSE', 'ARMATURE', 'Any Object']
Menu found: TAB: Armature (Drag)

Let’s look at the log contents in detail:

  1. ContextSensitiveMenu: Armature shows the name of the selected object (in this case, “Armature”) for generating the context-sensitive menu.

  2. prefix and suffix are the prefix and suffix added to the menu name. This allows flexible customization of menu names.

  3. Potential names are the menu name candidates generated based on the current context. In this example, 'POSE' corresponds to the Pose mode, 'ARMATURE' to the Armature object type, and 'Any Object' is included in case no other conditions are met.

  4. The script checks the Potential names list in order and calls the corresponding menu if it exists.

  5. In this log, the first candidate 'POSE' was ignored because a menu with the name 'TAB: POSE (Drag)' was not found. The script then moved on to the second candidate 'ARMATURE', and found the corresponding menu 'TAB: Armature (Drag)'. This menu name is a combination of the prefix 'TAB:', the object type 'Armature', and the suffix ' (Drag)'.

Also, if CSM cannot find the desired menu, it will display an error like the following:

ContextSensitiveMenu: Cube
  prefix: TAB:
  suffix:  (Drag)
Potential names: ['Vertex', 'EDIT_MESH', 'MESH', 'Any Object']
No menu found: ['TAB: Vertex (Drag)', 'TAB: Edit Mesh (Drag)', 'TAB: Mesh (Drag)', 'TAB: Any Object (Drag)']

imageimage806×429 50.8 KB

This allows you to check the required menu names for the current operation. This is especially useful when setting up menus. However, if you don’t want to display this error during actual use, set IGNORE_ERRORS at the beginning of the script to True.

# Ignore if menu not found
IGNORE_ERRORS = False

The explanation got a bit long, but that’s all. It may seem difficult, but once you actually run it, you’ll realize it’s a simple mechanism.

The downside is that menu names need to follow the naming convention. If this is unacceptable, you can conditionally branch open_menu as follows:

open_menu("Macro Set Object Mode") if C.mode in {'EDIT_MESH', 'PAINT_WEIGHT'} else open_menu("Macro Set Edit Mode")

Please let me know if you have any questions or requests.


❤️ 8 likes


🔗 View on Blender Artists