Creating blueprint assets/hierarchies with Python

Hey there,

is it possible to create a blueprint/actor in the content browser with python, like you do normally with right-click, create blueprint class, actor? Also I want to apply (mesh) components to it (with Python), so I can drag and drop a mesh-collection into the scene.
I tried something with assetTool.create_asset for creating an actor, but I am stuck with the factory argument.

Furthermore, it would be interesting to create an actorstructure while importing, as you can see here but it seams like this function is not supported yet. (just getting errors)

Thank you in advance for your answers and time!

2 Likes

Hey, I had to go back to the dev team to ask about this one. Turns out that it is possible to create a new Blueprint, like this:


import unreal

asset_name = "MyAwesomeBPActorClass"
package_path = "/Game/MyContentFolder"

factory = unreal.BlueprintFactory()
factory.set_editor_property("ParentClass", unreal.Actor)

asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
my_new_asset = asset_tools.create_asset(asset_name, package_path, None, factory)

unreal.EditorAssetLibrary.save_loaded_asset(my_new_asset)

but it doesn’t look like there’s a way to add components to the asset.

4 Likes

Thank you! This helps me a lot.

I hope the whole BluePrint APIs will be exposed to Python. It would be very useful if it was possible to code everything BluePrint related directly in Python. Coding complex algorithms with Python using BluePrints would be way easier than having to do it visually with all the spaghetti mess.

Actually that is the case already. Virtually everything that is exposed to Blueprint is also exposed to Python. The only real limitation is that Python is intended (at least for now) to be exclusively for editor scripting, not for runtime. So it’s great for things like preparing assets in the UE4 editor, setting up a pipeline that connects UE4 to other external tools, laying out a level programmatically, etc. Were you hoping to be able to use it as a runtime scripting language?

In another thread it was replied to me that it wasn’t planned to be able to create BluePrint code using Python because full BluePrint APIs weren’t exposed. If that changed then it would be really useful being able to code visual BluePrints spaghetti algorithms with Python script code. Still in editor will it be possible to even translate existing BluePrints to Python scripting for further editing thru Python? Or is Python scripting working on existing BluePrints and not just for creating new ones so that full editing and so coding could be done thru Python then? If runtime support will be added in the future too that would be great too.

It depends what you mean by “able to create BluePrint code using Python”:

If you mean, can I write a Python script that has the same effect as any given Blueprint graph, the answer is almost always yes.
If you mean, can I write a Python script that outputs a graph of Blueprint nodes, then no.

Python is an alternative to Blueprint that exposes mostly the same set of API functions in a different language. But there isn’t a way to translate code automatically back and forth from one to the other. You can’t take a Blueprint graph and say “Give me this as a Python script,” or vice-versa. That would be neat, but for the time being you need to work in one or the other.

Runtime use is tricky for a few reasons, including performance. For now, we’re mostly hearing that the need for Python is more on the data prep, production and interop side. If you’re looking for an alternative to Blueprints for runtime, you could always try to take the plunge into C++.

Sorry but then that means that it still is not possible to use Python scripts to create anything BluePrints related… or not?
You are telling me that it is not possible to code BluePrint collapsed Functions/Macros with Python, right? Only basic BluePrint graphs are supported thru Python scripts then?

Well, I hope that that will be a planned feature for future versions then. It would be really useful to not have to modify any BluePrint code visually but using Python instead and then regenerate the BluePrint visual code thru any modifications made with Python scripts. It would allow to quickly browse thru a lot of complex BP code and being able to write it at a practically infinite complexity as needed thanks to Python. Something that a human really wouldn’t be able to manage doing visually with BPs otherwise.

If only public documents for the C++ were all clearly up to date with all the APIs explained then I would use that indeed but for many things in UE4 using BluePrints became mandatory anyway more or less. The problem is with writing or modifying complex algorithm in visual spaghetti code using BluePrints that becomes a nightmare compared to text based standard script or programming languages. I can understand using BPs for accessing APIs quicker for simple things but when algorithms become very complex and you have to waste a lot of time just to follow how a graph is visually built trying to understand what the actual code does then it becomes a problem. It can be a problem with C++ and other languages too indeed when not properly commented but it is still less messed up than wandering thru many visual graphs (which is the same issue affecting things like UML and such I’d say…)

I have a (currently on hold) R&D system that can do that. I’m still in research phase…

  • but it’s not Python tho *
    ​​​​​

Great to hear, what language do you use to make bluprints scriptable in a round-trip fashion?

Wanted to bump this post. Thank you for the tip on creating blueprints via python.

I agree with the OP that it would be very helpful to A) add components to blueprints via python and B) Add and connect Blueprint nodes in Blueprint graphs via Python. In my mind, this is part of the asset prep pipeline. Is it still Epic’s plan not to support this?

So I have a blueprint that has a default scene root with a bunch of static meshes all underneath it (its a building). Each of these static meshes has a name like A, B, C, D, E, etc. I would like to rename this as “generated_A”, “generated_B”. So I want to be able to load a blue print, access its components, and then rename them. I can do this through the gui by double clicking the blueprint asset (open up a blue print editor), Looking at the scene graph on the left, and right clicking each static mesh and renaming it.

Any way to automate this.?

Here is some example code that only renames the entire blue print asset, not the indivual static compoennts

from unreal import EditorAssetLibrary as ual, EditorLevelLibrary as ull, Vector, Rotator
obj1 = ual.load_asset(“/Game/UrbanCity/Models/ModularBuildings/Prefabs/Building1_Example”)
obj1.rename(“Generated_Buildings1_Example”)

what i want

for component in obj1.get_components()
component.rename(“generated_” + component.get_name())

It doesn’t look like we can get into the Blueprint class and rename its components.
You should be able to rename the components in the Actor once you drop an instance of that Blueprint class into your Level, but I don’t see a way to do it on the Asset itself.

Here’s a script that renames the components on a selected BP Actor in the Level:


import unreal

actors = unreal.EditorLevelLibrary.get_selected_level_actors()

for actor in actors:
    sm_comps = actor.get_components_by_class(unreal.StaticMeshComponent)
    for sm_comp in sm_comps:
        comp_name = sm_comp.get_name()
        sm_comp.rename("Generated " + comp_name)



I’d like to see more snippets. A reference for small tasks like “create a blueprint” was great.

Is this a full-blown editor scripting API? Or just some surface-level tidbits to do the odd thing, like create a folder. If the functionality is limited, can I at least call code in my plug-in as a workaround?

Can I do stuff like this? UnrealEnginePython/YourFirstAutomatedPipeline.md at master · 20tab/UnrealEnginePython · GitHub

Not sure when they added it, but you can add components to BP’s with python now :slight_smile:

Some useful links:

If I may bump this thread, I’m looking for pretty much the same functionality. Is it possible with the official Python Plugin in Unreal? As far as I understand, 20tab was the third party developer that used to be the standard, but was succeeded by the official version.

1 Like

Old topic. but can`t find the answer. Is is possible to create a blueprint from custom class? I have my own class, and I need to convert already placed actors into bp with this class. Idealy adding some simple logic automatically inside of it.

Do you want to edit a blueprint asset from python or spawn instance of this custom blueprint to replace existing actors?

If it is the second you can create your custom blueprint with its code and components (here I just made a bp from actor and add a static mesh component).
image

Then you can do some code like this to replace all static mesh actor in level with your custom bp:

import unreal

custom_bp_class = unreal.EditorAssetLibrary.load_blueprint_class("/game/TestSpawnBP/BP_MyCustomActor")

actors = unreal.EditorLevelLibrary.get_all_level_actors()
smas = unreal.EditorFilterLibrary.by_class(actors,unreal.StaticMeshActor)
for a in smas:
    new_custom_actor = unreal.EditorLevelLibrary.spawn_actor_from_class(custom_bp_class,a.get_actor_location(),a.get_actor_rotation())
    new_custom_actor.set_actor_label("Custom_"+a.get_actor_label())
    smcs = new_custom_actor.get_components_by_class(unreal.StaticMeshComponent)
    if(len(smcs) == 1):
        smcs[0].set_static_mesh(a.static_mesh_component.static_mesh)
    

Here I spawn the custom blueprint in same position and rotation of the static mesh actors, I copy the label and apply the same static mesh. You can do more like attaching the new actor in the same place in the hierarchy, delete the original static mesh actor.

Hi!

I’m able to do the same thing with this code (ue5.1.0), it creates the hierarki that I want but I can’t figure out how to edit each component. Any help would be highly appreciated!

#---create_bluprint_actor---#

factory = unreal.BlueprintFactory()
factory.set_editor_property("ParentClass", unreal.Actor)

# make the blueprint
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
bp_actor = asset_tools.create_asset(blueprint_name, asset_path, None, factory)

unreal.EditorAssetLibrary.save_loaded_asset( bp_actor )

# get the root data handle
subsystem = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)
root_data_handle = subsystem.k2_gather_subobject_data_for_blueprint(bp_actor)[0]

skeletalmesh = unreal.SkeletalMeshComponent 

# add new component
sub_handle, fail_reson = subsystem.add_new_subobject(
            params=unreal.AddNewSubobjectParams(
                parent_handle=root_data_handle,
                new_class=skeletalmesh,
                blueprint_context=bp_actor))

subsystem.rename_subobject(handle=sub_handle, new_name=unreal.Text("skeleton"))

root_data_handle = subsystem.k2_gather_subobject_data_for_blueprint(bp_actor)[2]

for x in range(0,30,1):
	
	staticmesh = unreal.StaticMeshComponent

	#add new component
	sub_handle, fail_reson = subsystem.add_new_subobject(
	            params=unreal.AddNewSubobjectParams(
	                parent_handle=root_data_handle,
	                new_class=staticmesh,
	                blueprint_context=bp_actor))

	subsystem.rename_subobject(handle=sub_handle, new_name=unreal.Text( "piece" + str(x) ))

Here I’ve set the properties manually:

1 Like


From here: Using Python to Create Unreal Engine Objects | unrealcode.net