Hello Vitor,
No problem about the delay, thank you for the comprehensive answer.
Can you access the logic of a built-in button (which logic is set in CPP) through the Python API or a Blueprint?
That is what I thought but I needed to be sure, unfortunately we are bound to use Blueprint for specific reasons but it is good to know how we could do such a thing in C++ if we get the possibility to switch at some point.
Is there a way for these menus to be contextual to the parent class (here, PLA) instead of generic Blueprint Class?
The first part of your answer here is how we do it in general for calling blueprint scripts but it leads to a few more manipulations that we wanted to avoid since it seems we can add new buttons to existing menus (like what was shown in the pictures of my first message).
Ideally, I would want to add said button to one of the existing categories e.g. Blueprint Class Actions, Common, etc. or even my own sub category that I would add to the menu.
On top of that, I would want said button to be available only when I am right clicking on a Packed Level Actor but it seems that with the current implementation and design in Unreal, this menu is contextual to blueprint objects in general and does not distinguish between parent classes (this menu would appear for a Static Mesh Actor, a Skeletal Mesh Actor, etc. any object of “Blueprint Class”).
For now I decided to add the button to the LevelEditor.ActorContextMenu.LevelSubMenu (i.e. the sub menu where you go to create a Packed Level Actor from an object selected inside an open level from the viewport, you can see that on the first picture below) but it is not conditioned to appear only when selecting Packed Level Actor.
Here is what the python code looks like:
`import unreal
@unreal.uclass()
class PLAStationMenu(unreal.ToolMenuEntryScript):
“”"
Class for the button used to create a PLA Station
(i.e. reparent the selected PLA to the BP PLAStation)
It implements all the logic to create a button inside an
existing menu, add a custom section and call on a function
from a specific Blueprint.
“”"
state = unreal.uproperty(bool, getter=‘get_state’, setter=‘set_state’)
def init(self):
“”"
Function to initiate the object PLAStationMenu:
Args:
self (PLAStationMenu): the object itself.
Returns:
No returns.
“”"
super().init()
self.init_menu()
@unreal.ufunction(override=True)
def execute(self, context):
“”"
Function to load the specific BP’s function we want the button.
to call. It is an override of the existing function for ToolMenuEntryScript.
Args:
self (PLAStationMenu): the object itself.
context
“”"
print(f’[PLA] Executing button’)
TODO set the asset path correctly
PLA_factory = unreal.EditorAssetLibrary.load_asset(“/Game/Developers/thibaultlacharme/PLA/EU_PLA”)
bp_class = unreal.load_object(None, PLA_factory.generated_class().get_path_name())
bp_cdo = unreal.get_default_object(bp_class)
bp_cdo.call_method(“CreatePLAStation”)
def init_menu(self):
“”"
Function to initiate the button in the menu and adding the custom
category PLA Preset.
Args:
self (PLAStationMenu): the object itself.
“”"
owning_menu_name = ‘LevelEditor.ActorContextMenu.LevelSubMenu’
category_name = ‘PLA Preset’
self.data.menu = owning_menu_name
self.data.advanced.entry_type = unreal.MultiBlockType.MENU_ENTRY
self.data.advanced.user_interface_action_type = unreal.UserInterfaceActionType.BUTTON
self.data.icon = unreal.ScriptSlateIcon(“EditorStyle”, “MergeActors.MeshMergingTool”)
self.init_entry(owner_name=‘editorUtilities’,
menu=‘’, section=category_name,
name=‘Create PLA Station’,
label=‘Create PLA Station’,
tool_tip=‘Convert PLA to PLA Station’)
tools_menus:unreal.ToolMenus = unreal.ToolMenus.get()
menu = tools_menus.extend_menu(owning_menu_name)
Add section “PLA Station” to the menu
menu.add_section(section_name=category_name, label=‘PLA Preset’)
Add our button to the menu
menu.add_menu_entry_object(self)
tools_menus.refresh_all_widgets()
if name == ‘main’:
PLAStationMenu()`Could I condition the creation of said button depending on the object I selected to open the menu on? It seems I could override the function Construct Menu Entry in Blueprint, a function which is called every time I open the sub menu.
While it seems I might not be able to do so in Blueprint (the function override gets run but I cannot use Add Menu Entry Object on the fly in Blueprint, only in the Run event read once at project launch), could I maybe do a check right before the self.init_menu() to get the current object’s parent class and, if it is a Packed Level Actor, call on the init function?
If such a check is possible, I might be able to do an operation similar to the picture with the Execute Python Script (i.e. using get_class to recover the class and, if it is a Packed Level Actor class, go through the init logic).
Sorry for the delay of my answer, thanks again for your first input!
Best regards,
Thibault
[Image Removed][Image Removed]