Hey everyone! Welcome back to my blog! This one will be a little bit different! I wanted to put my Python skills to use after realizing that I did a whole Python course over the summer but didn't find the time to implement it into anything. As a little refresher, I wanted to create some basic scripts as a practice.
I started off with something really basic because I wanted to get familiar with using the Unreal Engine Python API as a resource to write these scripts. My first idea was to create a script that would tell me how many times a Static Mesh Actor is being instanced inside the level! Even though this can easily be checked through the Statistics window, I felt like this would be a good way to test that this tool is working. Further, I knew that if I made this tool work as intended then it would give me the confidence boost to make something a little bit more complex. The idea is to work my way up as to not over-complicate things and overwhelm myself, as I did mention before, I hadn't put these skills to use since the summer break!
This process was actually a lot easier and way more fun than I anticipated. All thanks to this handy little Unreal Python API Documentation. The beauty of this documentation is that you can easily navigate due to it's simple IU and Layout. All you have to do is type in some key words inside the search bar and voila!
After you find what you need, you then gain access to the amount of things you can do with that command!
Using Ctrl + F I searched for instance as that is what I wanted to find out, and would you look at that?! There is a beautiful little function that does exactly what I need!
With that In mind, I began writing my code inside PyCharm. A very handy text editor that has a crazy amount of customizability and one I was familiar with from Colt Steele's Python Course!
Here is what that code looks like!
import unreal
def count_mesh_instances():
selected_actors = unreal.EditorLevelLibrary.get_selected_level_actors()
if not selected_actors:
unreal.log_warning("No mesh selected. Please select a mesh in the editor.")
return
selected_actor = selected_actors[0]
static_mesh_component = selected_actor.get_component_by_class(unreal.StaticMeshComponent)
if not static_mesh_component:
unreal.log_warning("Selected actor does not have a static mesh component.")
return
static_mesh = static_mesh_component.static_mesh
instance_count = 0
for actor in unreal.EditorLevelLibrary.get_all_level_actors():
if actor.get_class() == unreal.StaticMeshActor.static_class():
mesh_comp = actor.get_component_by_class(unreal.StaticMeshComponent)
if mesh_comp and mesh_comp.static_mesh == static_mesh:
instance_count += 1
unreal.log("Instance count for {}: {}".format(static_mesh.get_name(), instance_count))
count_mesh_instances()
Now to make sure that the code works one last time to make sure that we don't need to do any troubleshooting! Lets run it one last time.
Here is me comparing it to the statistics window in order to make sure that I'm getting the correct results.How cool is that?! I made my first little UE5 script! I will move on to creating something a little more complicated as I believe this is still quite primitive and hopefully build my way up from that. I also want to learn how to make a User Interface for the next script so it's a little more user friendly! Luckily I found a video that does exactly that and I can't wait to put it to use once I finalize the next script I make!


