Change post process properties in editor mode

Hi! I´ve been developing a tool that automaticaly changes the depth of field from a cinemachine virtual camera volume. When I update the values they change in editor, and modify the view as they are supposed, but when I try to modify again the values through editor, the view no longer changes until I save and reload the scene. Here is the code:

var cmBrain = Camera.main.GetComponent<CinemachineBrain>();
if (cmBrain == null) return;

var cmLive = cmBrain.ActiveVirtualCamera;
if (cmLive == null) return;

var volume = cmLive.VirtualCameraGameObject.GetComponent<CinemachineVolumeSettings>();
  
VolumeProfile volumeProfile = volume.m_Profile;

DepthOfField tmp;
if (volumeProfile.TryGet<DepthOfField>(out tmp))
{
    dofComponent = tmp;
}
Undo.RecordObject(dofComponent, volume.name);
dofComponent.nearFocusEnd = new MinFloatParameter(minmax.x, 0f, true);
dofComponent.farFocusStart = new MinFloatParameter(minmax.y, 0f, true);

volume.m_Profile = volumeProfile;

Thanks for the help! ^^

You need to call volume.InvalidateCachedProfile(), where volume is the CinemachineVolumeSettings component. This is because CinemachineVolumeSettings is not designed to work by default with dynamically-changing volumes.

Hi Gregoryl! I´ve tryed different places to call the function, but it still doesnt work, could you be more specific? Thank you ^^

Call it when you change anything in the profile

I´ve called it before and after changing anything and still doesn´t work

var cmBrain = Camera.main.GetComponent<CinemachineBrain>();
if (cmBrain == null) return;

var cmLive = cmBrain.ActiveVirtualCamera;
if (cmLive == null) return;

var volume = cmLive.VirtualCameraGameObject.GetComponent<CinemachineVolumeSettings>();
  
VolumeProfile volumeProfile = volume.m_Profile;

DepthOfField tmp;
if (volumeProfile.TryGet<DepthOfField>(out tmp))
{
    dofComponent = tmp;
}
volume.InvalidateCachedProfile();
dofComponent.nearFocusEnd = new MinFloatParameter(minmax.x, 0f, true);
volume.InvalidateCachedProfile();
dofComponent.farFocusStart = new MinFloatParameter(minmax.y, 0f, true);
volume.InvalidateCachedProfile();



volume.InvalidateCachedProfile();
volume.m_Profile = volumeProfile;
volume.InvalidateCachedProfile();

You only nee to call it once, and the exact place doesn’t matter. Perhaps I’m misunderstanding what you’re trying to do. Can you show me an image of the Volume Settings component inspector in your vcam?

Normally CinemachineVolumeSettings can do dynamic focus tracking. Do you have it set up like this?

image

I was trying to change the end of the near range and the start of the far range on a manual depth of field. I´ve tryed calling it once in different lines of the code so then I called it multiple times :sweat_smile:. Here is the inspector:

ok, I understand now. The cached volume only applies when focus tracking is enabled. In your case it’s a little trickier. You have to delete the hidden dynamic volume that CM constructs under the hood.

When you change a setting in the profile, delete the hidden child of the main camera named “__CMVolumes”. Let me know if that works.

I´ve tryed destroying the child called “_CMVolumes”. I succesfuly destroy it but still doesn´t work. After calling my script ,values in editor doesnt change the view

I´ve solved the problem creating a depth of field component with the values I wanted, copying it as a Json into the clipboard and then pasting it on my component.

Captura de pantalla 2023-11-30 122030

Captura de pantalla 2023-11-30 122059