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

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