Skip to content

Commit 5398a48

Browse files
authored
fix: handle errors when capturing Render results to files (#461)
1 parent 15f5180 commit 5398a48

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

Editor/Scripts/Features/RenderCache/RenderCachePlayableAssetInspector.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ internal static IEnumerator UpdateRenderCacheCoroutine(PlayableDirector director
275275
default: outputExt = "png"; break;;
276276
}
277277

278-
bool cancelled = false;
279-
while (!cancelled) {
278+
bool cancelled = false;
279+
bool captureSuccessful = true;
280+
while (!cancelled && captureSuccessful) {
280281

281282
//Always recalculate from start to avoid floating point errors
282283
double directorTime = timelineClip.start + (fileCounter * timePerFrame);
@@ -305,10 +306,8 @@ internal static IEnumerator UpdateRenderCacheCoroutine(PlayableDirector director
305306

306307
//Unload texture because it may be overwritten
307308
StreamingImageSequencePlugin.UnloadImageAndNotify(outputFilePath);
308-
renderCapturer.CaptureToFile(outputFilePath, outputFormat);
309-
309+
captureSuccessful = renderCapturer.TryCaptureToFile(outputFilePath, outputFormat);
310310
}
311-
Assert.IsTrue(File.Exists(outputFilePath));
312311

313312
++fileCounter;
314313
cancelled = EditorUtility.DisplayCancelableProgressBar(
@@ -336,6 +335,10 @@ internal static IEnumerator UpdateRenderCacheCoroutine(PlayableDirector director
336335

337336
}
338337
}
338+
339+
if (!captureSuccessful) {
340+
EditorUtility.DisplayDialog("Streaming Image Sequence", "Capturing failed", "Ok");
341+
}
339342

340343
//Notify
341344
FolderContentsChangedNotifier.GetInstance().Notify(outputFolder);

Runtime/Scripts/Features/RenderCache/Components/BaseRenderCapturer.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.IO;
4+
using JetBrains.Annotations;
45
using Unity.FilmInternalUtilities;
56
using UnityEngine;
67

@@ -38,13 +39,17 @@ public abstract class BaseRenderCapturer : MonoBehaviour {
3839
public Texture GetInternalTexture() { return m_rt;}
3940

4041
/// <summary>
41-
/// Capture the contents of RenderTexture into file
42+
/// Capture the contents of RenderTexture to file
4243
/// </summary>
4344
/// <param name="outputFilePath">The path of the file</param>
4445
/// <param name="outputFormat">The output file format</param>
45-
public void CaptureToFile(string outputFilePath, RenderCacheOutputFormat outputFormat = RenderCacheOutputFormat.PNG)
46+
/// <returns>True if successful, false otherwise</returns>
47+
public bool TryCaptureToFile(string outputFilePath, RenderCacheOutputFormat outputFormat = RenderCacheOutputFormat.PNG)
4648
{
4749
RenderTexture rt = UpdateRenderTextureV();
50+
if (null == rt)
51+
return false;
52+
4853
TextureFormat textureFormat = TextureFormat.RGBA32;
4954
bool isPNG = true;
5055
if (RenderCacheOutputFormat.EXR == outputFormat) {
@@ -56,14 +61,29 @@ public void CaptureToFile(string outputFilePath, RenderCacheOutputFormat outputF
5661
if (!writeSuccess) {
5762
Debug.LogError($"[SIS] Can't write to file: {outputFilePath}." + Environment.NewLine);
5863
}
64+
65+
return true;
5966
}
6067

68+
/// <summary>
69+
/// Capture the contents of RenderTexture to file
70+
/// </summary>
71+
/// <param name="outputFilePath">The path of the file</param>
72+
/// <param name="outputFormat">The output file format</param>
73+
[Obsolete("Replaced by TryCaptureToFile()")]
74+
public void CaptureToFile(string outputFilePath, RenderCacheOutputFormat outputFormat = RenderCacheOutputFormat.PNG)
75+
{
76+
//[TODO-sin: 2022-9-26] Remove CaptureToFile() function in 0.17.x-preview
77+
TryCaptureToFile(outputFilePath, outputFormat);
78+
}
79+
6180
//----------------------------------------------------------------------------------------------------------------------
6281

6382
/// <summary>
6483
/// Updates the render texture used for the capturing process
6584
/// </summary>
66-
/// <returns>The updated render texture</returns>
85+
/// <returns>The updated render texture if successful, null otherwise</returns>
86+
[CanBeNull]
6787
protected abstract RenderTexture UpdateRenderTextureV();
6888

6989

Runtime/Scripts/Features/RenderCache/Components/PencilLineRenderCapturer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public override void EndCaptureV() {
8181
protected override RenderTexture UpdateRenderTextureV() {
8282
#if AT_USE_PENCILLINE
8383
PencilLineRenderer lineRenderer = m_pencilLineEffect.PencilRenderer;
84-
Assert.IsNotNull(lineRenderer);
84+
if (null == lineRenderer)
85+
return null;
8586
Graphics.Blit(lineRenderer.Texture, m_rt);
8687
#endif
8788
return m_rt;

0 commit comments

Comments
 (0)