Can it be done? A curve from a script or blueprint editor utility?..
I am attempting to create color curves (and then a color atlas) via python scripting. I can’t figure how how to add a key value (or understand if this is even possible or not).
Below is my code that fails to add a key. Up to that point things are fine. Please shine your wisdom upon me!
Bonus: How do find what are available get/set properties?.. I tried dir(asset). For example I know a
asset.get_editor_property(‘adjust_hue’) and asset.set_editor_property(‘adjust_hue’, 0.5) but I don’t know how I should know that apart from having guessed (i.e., open the editor and I see those…)
def create_color_curve(asset_name, asset_path, color_data):
"""
Creates or updates a Color Curve (CurveLinearColor).
Parameters:
- asset_name (str): Name of the Color Curve asset.
- asset_path (str): Path to store the Color Curve.
- color_data (list of unreal.LinearColor): List of colors to add as curve keys.
Returns:
- None
"""
full_asset_path = f"{asset_path}/{asset_name}"
# Check if an asset already exists at the specified path
if unreal.EditorAssetLibrary.does_asset_exist(full_asset_path):
# Delete the existing asset to prevent overwrite prompt
unreal.EditorAssetLibrary.delete_asset(full_asset_path)
unreal.log(f"Existing asset deleted at {full_asset_path}")
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
factory = unreal.CurveLinearColorFactory()
asset = asset_tools.create_asset(asset_name, asset_path, unreal.CurveLinearColor, factory)
time_step = 1.0 / (len(color_data) - 1) if len(color_data) > 1 else 1.0
for i, color in enumerate(color_data):
time = i * time_step
asset.get_editor_property("float_curve").add_key(time, color) # Fails here. Alternative I could create a csv/json and load that instead... if that is better...
# Save the asset to make the changes permanent in the editor's content library.
unreal.EditorAssetLibrary.save_asset(asset.get_path_name())
unreal.log(f"Created asset: {asset_name} at {asset.get_path_name()} with custom properties.")
Bumpityy bump bump. I call upon the mighty Q & A god to shine wisdom upon this inquiry!
Throws color crystals into the fire and the onlookers ooo and ahh as the advocate dances on the swirling multi-colored smoke… Strange shadows dance on the wall…
Could the deity be descending to have mercy on this suplicant?..
What does this silence mean?.. Hmm. My spells and performance have not been enough. The Gods have no ears or have I failed them someone?.. Perhaps the impossible truly is impossible…
With halting motion his skin dries and starts to peal. The eyes sink. He crumbles to the floor in a cloud of ash. A soft wind swirls the remains and carries them off into the darkness of the night
Ah yes. Going straing to a curve_linear_color from the CSVImport… Excellent idea. I was so fixated on modifying the asset directly. I shall go and test this out post haste. Thank you reminding me there are other ways around this issue potentially!
Here is the working code using the workaround bit by loading a CSV file directly.
Thanks again @3dRaven for reminding me there are other ways!
def linear_color_to_csv(file, color_data):
if os.path.exists(file):
os.remove(file)
with open(file, mode="w", newline="") as file:
writer = csv.writer(file)
# Write Unreal's expected header for Linear Color Curve import
writer.writerow(["Time", "R", "G", "B", "A"]) # Header format
# Write color data with time values (assuming even spacing)
time_scale = len(color_data)-1 if len(color_data) > 1 else 1
for index, color in enumerate(color_data):
writer.writerow([index/time_scale, color.r, color.g, color.b, color.a]) # Time, R, G, B, A
def create_color_curve(asset_name, asset_path, color_data):
"""
Creates or updates a Color Curve (CurveLinearColor).
Parameters:
- asset_name (str): Name of the Color Curve asset.
- asset_path (str): Path to store the Color Curve.
- color_data (list of unreal.LinearColor): List of colors to add as curve keys.
Returns:
- None
"""
# Convert linear color to csv in a temporary file to be imported
current_path = pathlib.Path(__file__).parent
temp_file = pathlib.Path(f'temp/{asset_name}.csv')
temp_file = (current_path / temp_file).resolve()
linear_color_to_csv(temp_file, color_data)
task = unreal.AssetImportTask()
task.filename = temp_file.as_posix()
task.destination_path = asset_path
task.replace_existing = True
task.automated = True
task.save = True
factory = unreal.CSVImportFactory()
# Set up import settings
import_settings = unreal.CSVImportSettings()
import_settings.import_type = unreal.CSVImportType.ECSV_CURVE_LINEAR_COLOR # Set to Linear Color Curve
factory.automated_import_settings = import_settings
task.factory = factory
# Perform the import
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
asset_tools.import_asset_tasks([task])
# Construct full asset path
full_asset_path = f"{asset_path}/{asset_name}"
unreal.log(f"Created linear color curve asset: {full_asset_path}.uasset.")