How to modify a static mesh component in python ?

Hello,

I’m trying to duplicate a BP and change a static mesh inside.

The ideal plan would be as follow:

NewAsset = editor_assets.duplicate_asset(TemplatePath, TargetAssetPath)
MeshComponent = NewAsset.get_editor_property(“sm00”)
MeshComponent.set_editor_property(“StaticMesh”, newMesh)

But of course it would be too simple so it doesn’t work.

Long story short, i spent a lot of time trying all the combos i could think of or find on the forum and the api documentation.

The best i could come up with is this one (from another post on this forum):

NewAsset = editor_assets.duplicate_asset(TemplatePath, TargetAssetPath)
bp_gc = NewAsset.generated_class()
bp_cdo = ue.get_default_object(bp_gc)
MeshComponent = bp_cdo.get_editor_property(“sm00”)
MeshComponent.set_editor_property(“StaticMesh”, newMesh)

This sort of works: i have to get the generated class, then the CDO.
And i can modify the static mesh on the cdo IF it was created in a C++ constructor.

But apparently a manually added static mesh component is not in the CDO.
So how can i get to it ?
Either i find a way to add it to the CDO, or i can get directly to the BP and not to the CDO of its generated class.

Any help ?

Thanks
Cedric

I tried to create a child BP from my template BP, hoping that the component would appear in the CDO of the child BP (inheriting from a BP already having this component), but no luck.

I also tried to use the AddStaticMeshComponent in the BP constructor, but it doesn’t work either.

Anyone has any idea of how i can add a component to the CDO without modifying the C++ constructor ?

How would i modify the CDO of a BP in a BP-only project (no C++) ?

Thanks
Cedric

I found a strange workaround to my problem but with a twist.

Continuing my tests, i found that, although i couldn’t act on components, i could actually act on variables.
For some reason, get_editor_property() and set_editor_property() method work on variables but not on component, although the doc says it should act on any property visible in the editor.
Anyone knows if this is normal or if this could be a bug to report ?

Anyway i defined a StaticMesh variable, and i can modify it in Python with this simple code:

bp_cdo.set_editor_property(“mesh”, actorMesh)

Now of course, since i’m only setting a variable, i have to assign it to the component.
Being atm unable to do it in python, i added this to the BP constructor:

ConstructorAddSM

This seems to do the job: i can set the mesh in Python without modifying my C++ class. I just have to assign it through BP scripting, which is acceptable for me.

So, problem almost solved, but there’s still a strange twist.
The mesh is assigned correctly and shows in the viewport, but it doesn’t appear as assigned in the component’s details panel (i tried many times to recompile, restart the editor, etc.).

As a result, i don’t get any thumbnail picture on the final BP, which makes it difficult to work with a lot of BPs in the editor.

Am i facing a strange bug from UE5 ? Or am i doing something wrong ?
Any ideas ?

Thanks
Cedric