Copy Macro from one Blueprint to another using C++

I’m trying to copy a macro from one blueprint to another using C++. (The new blueprint is made using C++, which is why it can’t be added the normal way)

I’ve tried:
// Get the function graph from the source Blueprint
UEdGraph* SourceGraph = SourceBlueprint->FunctionGraphs[Graph];

			// Duplicate the function graph
			UEdGraph* DuplicatedGraph = FEdGraphUtilities::CloneGraph(SourceGraph, TargetBlueprint);

			// Add the duplicated function graph to the target Blueprint
			TargetBlueprint->FunctionGraphs.Add(DuplicatedGraph);

But this yields an unnamed Function Graph instead of a Named Macro

Also tried:
FBlueprintEditorUtils::AddMacroGraph(TargetBlueprint, SourceGraph, true, nullptr);

But this links the macro to the original file, making the new file unsavable.

I can’t use a blueprint library, as the macro needs to change global variables.
I can’t use a function, because the input needs to changed based on what happens internally.

Example: Set position macro with an input of “MPosition” and Mesh. Internally it gets a position from the mesh, and iterates over all the points and sets the position to MPosition.
Outside the macro, is a global Variable “GPosition”. Inside the macro, it sets GPosition to the original position of point being iterated.

Basically: I’m trying to create a macro or function that works like “Set Position” of Blender’s Geometry nodes.

The function Doesn’t work because it just gets the value “GPosition” is static.
The macro works, but I can’t seem to copy it to a new blueprint correctly.

Thanks for any help.


I figured it out:

I had to use Clone Graph to so that it didn’t link the macros.

UEdGraph* DuplicatedGraph = FEdGraphUtilities::CloneGraph(SourceGraph, TargetBlueprint);
DuplicatedGraph->Rename(*MacroName.ToString());
FBlueprintEditorUtils::AddMacroGraph(TargetBlueprint, DuplicatedGraph, true, nullptr);

Also had to rename it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.