How to create custom Control Rig class in c++?

How do I create a new custom c++ class which inherits from ControlRig ? Such that when I use the editor to create a new Control Rig and it asks for a parent rig my custom control rig shows up:

I’m trying to do this so I can avoid cluttering my workspace by not having to create complex rig graphs like this and simply write it all in c++ (if it’s even possible)

I figured it out!

First add the following modules to the <project-name>.Build.cs file:

PublicDependencyModuleNames.AddRange(new string[] { 
   ...
   "ControlRig",
   "RigVM"
});

Then rebuild your project, since I’m on linux using the pre compiled Unreal Engine I use the following command:

cd /path/to/Engine/Build/BatchFiles/Linux/

./Build.sh Development Linux -Project="/path/to/your/project/<project-name>.uproject" -TargetType=Editor

1. Creating a custom Control Rig class

Here’s the template to create a custom control rig class:

CustomControlRig.h

#include "ControlRig.h"
#include "CustomControlRig.generated.h"

UCLASS(Blueprintable) // without this the class won't show up in the editor
class MYPROJECT_API UCustomControlRig : public UControlRig {
    
    GENERATED_BODY()

   protected:
    virtual bool Execute(const FName& InEventName) override; // Tick() equivalent
    virtual void Initialize(bool bRequestInit) override; // BeginPlay() equivalent, initialize variables here

};

CustomControlRig.cpp

#include "CustomControlRig.h"

void UCustomControlRig::Initialize(bool bRequestInit) {
    Super::Initialize(bRequestInit);
}

bool UCustomControlRig::Execute(const FName& InEventName) {
    Super::Execute(InEventName);
    return true;
}

You can override the Initialize() & Execute() methods and add your logic.
You can conditionally check the InEventName to see if it’s Forwards Solve or Backwards Solve
You can also use it as a parent class for your blueprint since we added UCLASS(Blueprintable):


2. Creating custom nodes

Incase you want to create reusable nodes inside the rig graph editor you can use the following template

RigUnit_CustomNode.h

#pragma once

#include "Units/RigUnit.h"
#include "RigUnit_CustomNode.generated.h"

USTRUCT(meta = (DisplayName = "My Custom Node", Category = "Custom"))
struct CONTROLRIG_API FRigUnit_CustomNode : public FRigUnit {
    GENERATED_BODY()

    FRigUnit_CustomNode()
        : A(0.f), B(0.f), Result(0.f) {}

    RIGVM_METHOD()
    virtual void Execute() override;

    UPROPERTY(meta = (Input))
    float A;

    UPROPERTY(meta = (Input))
    float B;

    UPROPERTY(meta = (Output))
    float Result;

};

RigUnit_CustomNode.cpp

#include "RigUnit_CustomNode.h"

FRigUnit_CustomNode_Execute()
{
    Result = A + B;
}

(Note: Unreal is strict with the naming convention so name your rig unit class as such FRigUnit_<name>)

If all is done properly you should see your node in the rig graph editor as such:

3 Likes

I tried it. But, those error occerd. Can you give me advice?

image

btw you can inherit from FRigUnitMutable instead of FRigUnit if you want the execute pin as such:

image

1 Like

I have no idea but maybe try rebuilding your project?

Thank you for reply.

Could you tell me the rebuild you said, if you know in Windows?
I did ‘right click .uproject → Generate Visual Studio Project Files’ and built by Visual Studio.

I has resolved it.

The 'CONTROLRIG_API ’ macro is not good because it’s DLL_IMPORT, I think.
The build has been success when I changed the macro to my own project.

struct CONTROLRIG_API FRigUnit_CustomNode : public FRigUnit {
to
struct MYPROJECT_API FRigUnit_CustomNode : public FRigUnit {

I can add a custom node because of you! thank you so much!

1 Like

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