top of page

PCG to CSV

Unreal Procedural Content Generation is catching a lot of attention. At root it's a scattering system, so let's say you want to take the results of all that scattering out to another application, such as Blender or Houdini, or any software that lets you script transforms of assets from a CSV file. In the use case I'm thinking of, all the mesh assets exist in the target application (so they can be called as instances from a library). Because they must remain as instances, I don't want to be importing new assets from unreal for each cool PCG that's created; I just want to map mesh names to assets already in the library and assign them transforms.

Create a Struct that contains the needed parts

  • AssetPath [/game/ path of the asset; let the target application's side unpack the string]

  • Transform: Position, Rotation, Scale.

  • Optional Bool: Interactable [should be set on the spawned Actor to classify whether the imported mesh is an Item or Scenery] ... this one may depend on the asset name prefixes so it could be filtered by prefix without using a bool, but it's an option.

  • Optional Tag? Tags might be useful if we want to use the same PCG graph to force in different content (ie, Winter, Summer). Tags could also be assigned if you want the editor (importing the CSV) to filter certain actors so they get assigned certain properties that differ from their root) or get added triggers or get assigned to certain SelectionSets.

What about centers and grouping?

Since we are taking dry list of vectors from 0,0,0 of the actor, we don’t need any preset group values or transform.

In the image the XYZ at Origin has a little offset just for ease of drawing - imagine they are matching.


[Notes to self]

Remember that Z is Y in some software is Y is Z. Unpacking the output CSV string rows in a target application is supplemental to this guide (for now).


In case this changes, the node below lets us get the center of the input points as a vector ; it might be useful, but the actual transform of the center to their spawn origin probably matters more. We would assume the 0,0,0 is where we want to situate PCG actors exported in this way, regardless of how they were laid out in some level. Move the Set of objects so the Set’s pivot is at origin before exporting. Don’t move the pivot to origin and have a massive offset of the actual content.

Local Center as a vector in the world assumes that the world in the target editor matches unreal’s world space, which would be a headache to be sure.


1 Spawn Actors not Meshes

We can sample all the meshes in the scene, but it’s hard to exclude non-participating meshes (unless they are tagged). We can spawn actors (with meshes in them) and then collect the mesh list from the spawned actors (the actors can report their own locations and the assigned Mesh variable value.


Spreading some points in a spline's area and projecting it on a landscape, then spawning Actors on the points.

Super important! The spawn actor node, by default, Collapses all its produced actors into one, which isn't handy for outputting individual transforms. Luckily, there's an Option setting in which you can choose Individual Actors.


2 Assigned Mesh

Make the Construction set AssignedMesh variable with whatever mesh you want to use (a rock, a tree, etc); we use this variable in the Export function.

In this system, we have only one mesh per actor, and the PCG graph has to spawn lots of Actors. We do this instead of spawning lots of StaticMeshes, because there’s no method to look up directly spawned meshes of a PCG graph (or at least, none I could spot). Also, an actor has more controllable scripting features, so it’s better to spawn these. Use the BP class ‘PCG_SpawnMesh actor’ (or update the ExportPCG Data blueprint to collect your custom actor if you prefer).

We run the function by pressing PIE Play in the unreal level (so long as it spawns some PCG_SpawnMesh actors & has a PCG_Exporter BP placed in the world) then pressing a hotkey. Then each actor gets added to an array on the exporter. If there’s no PCG_Exporter BP in the world, nothing happens. This could be a Editor Utility operation, but it works fine with PIE.


3 PCG Exporter

In the playercontroller (hooked up to World Settings>GameMode) we can add a hotkey to execute the function ‘PCGtoCSV’. This function goes in an actor in the world called ‘ExportPCGData’. In my example, it uses the Z key.

What the function PCGtoCSV does:

  • collect the array of “PCGSpawnMesh” actors found in the world

  • passes the ‘Path’ and ‘Transform’ of each of them to a struct array

  • send the struct array entries to a CSV string

  • saves the CSV string to a set path+filename+extension (mine is C:\Users\tom.mooney\Documents\PCG_to_CSV.csv )


Apart from the path, nothing has to be edited here.


Update: The path can be set in the Default values of the actor in the world.

The critical node in the function is “Generate CSV from Array”. This is from a plugin ‘Runtime Datatable’ by Jared Therriault. [https://marketplace-website-node-launcher-prod.ol.epicgames.com/ue/marketplace/en-US/profile/Jared+Therriault]


1 This takes a struct array where the entries are strings.

2 Members to Include is what matters - it’s the string data from each variable in the object entries in the Array to Export.

3 Keys are the enumeration of each object in the supplied array.

4 The array fed to the generator could be ALL the PCG actors in a scene or just one.


And test it out... the CSV in a notepad: "Key","Path","Transform"

"Row0","SM_ChamferCube","Translation: X=-718.213 Y=1826.485 Z=100.000 Rotation: P=-0.000000 Y=0.000000 R=180.000000 Scale: X=1.000 Y=1.000 Z=1.000"


If you don't want the "Key" value to include "Row#" and just be "#", you could add each index of the Actor array parsed by the For Each Loop into a name array, instead of using Auto Generate Keys.

Comments


Featured Posts
Recent Posts
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page