To get started quickly, you first need to obtain Object objects.
In the early stages of starting a Python journey, the biggest challenges are usually about how to get started.
- How do I get an Actor in the editor?
- How do I get a specific resource in the directory?
- How can I view an object's properties in the command line?
The first step to solving these problems is getting objects in Unreal Engine through Python.
Get Objects from Predefined Menus¶
In TAPython's predefined menu, there are three menu items that allow users to quickly get objects. After selecting the corresponding object Component, Actor, or resource, click the corresponding menu. The corresponding component, Actor, or resource will be assigned to the global variable _r.
Then, in the Python command line, we can perform various desired operations:
Python(REPL) mode is most suitable for familiarizing yourself with objects and interfaces. You can save a lot of print statements.
_r dir(_r) _r.get_name()
Getting Objects from Code¶
Get Selected Actor¶
actors = unreal.EditorLevelLibrary.get_selected_level_actors()
In UE5, due to the introduction of various SubSystems, the methods in
EditorLevelLibrary have gradually been replaced by the methods in SubSystem.
actors = unreal.get_editor_subsystem(unreal.EditorActorSubsystem).get_selected_level_actors()
In various SubSystems, we need to first obtain the SubSystem instance through
get_editor_subsystem, and then execute its methods.
In many cases, we should not be troubled by the differences between various methods. Therefore, you can encapsulate the changes and directly call your own functions.
import Utilities actors = Utilities.Utils.get_selected_actors()
Get Selected Component¶
You can directly call the method provided by TAPyton's PythonBPLib
components = unreal.PythonBPLib.get_selected_components()
import Utilities components = Utilities.Utils.get_selected_components()
If you want to add a Component to the selected Actor, you can use
added_mesh_comp = unreal.PythonBPLib.add_component(unreal.StaticMeshComponent, target_actor, target_actor.root_component)
Get Selected Asset¶
Assets are similar, and you can directly call the methods in EditorUtilityLibrary
assets = unreal.EditorUtilityLibrary.get_selected_assets()
Or call your own encapsulated methods.
import Utilities assets = Utilities.Utils.get_selected_assets()
Get Material Node¶
For some complex nodes, such as nodes in materials, we can also extract them, assign them to the variable "_r", and experiment in the command line, or put them in theObject Detail viewer。
For more operations related to material nodes, please refer to this articlemanipulate Material
After obtaining the objects, you can use commands like
help, and Python's
inspect module to familiarize yourself with and view these Unreal objects in the editor.
For example, in the TAPython built-in tool Object detail viewer, the object's interfaces, properties, and corresponding results are displayed on the interface for easy viewing.