[USDImporter] Unique names for imported components

Hi!

I’m using the USD Importer to import files into my level. However, the actors created do not respect Unreal’s naming convention.

For example, if I imported such a file :

USD file example (assume it works and is correct)
#usda 1.0

def "Scene"
{
  def Xform "Component1"
  {
    def Mesh "Cube"
    {}
  }
  def Xform "Component2"
  {
    def Mesh "Cube"
    {}
  }
}

The resulting actor would be :

Actor (with the autogenerated suffixes)
Actor
  > Scene_0 (RootComponent)
      > Component1_0
          > Cube_0
      > Component2_1
          > Cube_0

So I tried dealing with this in C++ like this :

// Check for duplicate names
bool bAllUnique = true;
TArray<FString> Names;
for (auto Comp : Actor->GetComponents()) {
	if (Names.Contains(Comp->GetName())) {
		bAllUnique = false;
		break;
	}
	Names.Add(Comp->GetName());
}

if (!bAllUnique) {
	for (auto Component : Actor->GetComponents()) {
        // Try removing the suffix first
		FString NameNoSuffix = RemoveSuffix(Component->GetName());
		bool bRenameSucess = Component->Rename(*NameNoSuffix);

		int32 I = 0;
		while (!bRenameSucess) {
            // If removing the suffix wasn't enough, add a new suffix until it works
			FString NewName(NameNoSuffix + "_" + FString::FromInt(I++));
			bRenameSucess = Component->Rename(*NewName);
		}
	}
}

This issue is that I end up with only the suffixes removed (Rename doesn’t fail even though a component already has the name).

How can I make sure the components have unique names, while being as close as possible to the USD names?

Solved it:

if (Comp->Rename(NewName, nullptr, REN_ForceGlobalUnique | REN_Test))
    bSuccess = Comp->Rename(NewName, nullptr, REN_ForceGlobalUnique);

First try with REN_Test to make sure it can be renamed sucessfully, then rename for real (remove REN_Test).

Use REN_ForceGlobalUnique if for some reason your actor allows same name components.

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