Im creating a new blueprint, and I want to change an element on a component inside it (a StaticMeshComponent). Im not sure how to get access to components from a CDO/BGC reference. Its not an actor so all the get components are out, this all exists only in the editor not in the level.
Right now as a workaround im setting a variable then in the construction script setting the component to use the variable, but it would be nice to learn how to access to component directly
# Paths
path = childBP.get_path_name()
path_cdo = path + "_C"
# Objects - BlueprintGeneratedClass
bp_gc = ue.load_object(None, path_cdo)
#Objects - BGC CDO
bp_cdo = ue.get_default_object(bp_gc)
#Set variable
bp_cdo.set_editor_property("MeshToUse", a)
The question is still valid, but before anyone mentions this optimisation, iv switched the gc var setter to the much simpler:
bp_gc = ue.EditorAssetLibrary.load_blueprint_class(childBP.get_path_name())
which skips needing the path vars all together
kprimo
(kprimo)
July 18, 2023, 3:07pm
3
Hi, there. I found a way to get BP components with python api, try my functions:
import unreal
def get_component_handles(blueprint_asset_path):
subsystem = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)
blueprint_asset = unreal.load_asset(blueprint_asset_path)
subobject_data_handles = subsystem.k2_gather_subobject_data_for_blueprint(blueprint_asset)
return subobject_data_handles
def get_component_objects(blueprint_asset_path):
objects = []
handles = get_component_handles(blueprint_asset_path)
for handle in handles:
data = unreal.SubobjectDataBlueprintFunctionLibrary.get_data(handle)
object = unreal.SubobjectDataBlueprintFunctionLibrary.get_object(data)
objects.append(object)
return objects
component_objects = get_component_objects('/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.BP_ThirdPersonCharacter')
if you print component_objects
, will return:
LogPython: [<Object '/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.Default__BP_ThirdPersonCharacter_C' (0x0000050161179800) Class 'BP_ThirdPersonCharacter_C'>, <Object '/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.Default__BP_ThirdPersonCharacter_C:CollisionCylinder' (0x0000050167E84D00) Class 'CapsuleComponent'>, <Object '/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.Default__BP_ThirdPersonCharacter_C:Arrow' (0x0000050167E84600) Class 'ArrowComponent'>, <Object '/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.BP_ThirdPersonCharacter_C:CameraComponent_0__CCE3C0B4' (0
x0000050143A6CC00) Class 'CameraComponent'>, <Object '/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.Default__BP_ThirdPersonCharacter_C:CharacterMesh0' (0x0000050161385000) Class 'SkeletalMeshComponent'>, <Object '/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.BP_ThirdPersonCharacter_C:SpringArmComponent_0__A9892D03' (0x0000050147D1AC00) Class 'SpringArmComponent'>, <Object '/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.BP_ThirdPersonCharacter_C:CameraComponent_0__CCE3C0B4' (0x0000050143A6CC00) Class 'CameraComponent'>, <Object '/Game/ThirdPerson/Blueprints/BP_ThirdPersonCha
racter.Default__BP_ThirdPersonCharacter_C:CharMoveComp' (0x000005013DECF000) Class 'CharacterMovementComponent'>]
4 Likes
chyren53
(chyren53)
August 2, 2023, 9:00am
4
I did not post this question, but this work!
Thank you so much. I am looking for this almost 1 year.
Than you so much, it works well in unreal 5.1 and 5.3, but it doesn’t work in Unreal 4.27, do you konw how to get components in 4.27?
Findodo
(Findodo)
February 7, 2024, 11:45pm
6
Really thank you for this , you set me on the right path !