The VS Code of video editing, built for agents.
TypeScript
Refactor Object Mask Handling and Introduce useObjectMasks Hook
konstantin-pauluscaefe16d6755
diff --git a/apps/web/src/components/sidebar-right/inspector/effects-inspector.tsx b/apps/web/src/components/sidebar-right/inspector/effects-inspector.tsxindex c092d12d..923b0af4 100644--- a/apps/web/src/components/sidebar-right/inspector/effects-inspector.tsx+++ b/apps/web/src/components/sidebar-right/inspector/effects-inspector.tsx@@ -34,10 +34,10 @@ import {import { SliderInput } from "@/components/ui/slider-input";import { ControlledTextField } from "@/components/ui/text-field";import { useHas, useTrait, useWorld } from "@diffusionstudio/koota-solid";-import { Cache, Computed, Effect, Hidden, getParentNode } from "@diffusionstudio/runtime";+import { AssetId, Cache, Computed, Effect, Hidden, getParentNode } from "@diffusionstudio/runtime";import { useDerived, useEditor } from "@/engine/hooks";import { removeKeyframeTrack, syncKeyframe } from "@/engine/keyframes";-import { copyObjectMask, effectHasObjectMask, getVideoRect, listObjectMasks } from "@/engine/object-mask";+import { copyObjectMask, useObjectMasks } from "@/engine/object-mask";import { EFFECT_OPTIONS, effectOption } from "./effect-types";import { ObjectMaskInspector, objectMaskName } from "./object-mask";@@ -78,16 +78,16 @@ export function EffectsInspector(props: EffectsInspectorProps) {const value = useDerived(() => props.effect.get(Computed)?.value ?? 0);const masks = useDerived(() => props.effect.get(Cache)?.masks ?? NO_MASKS);- const isVideo = useDerived(() => {- const node = getParentNode(props.effect);- return node !== null && getVideoRect(world, node) !== null;- });+ const { footage, masks: sources } = useObjectMasks(() => getParentNode(props.effect));++ const used = useDerived(() =>+ (props.effect.get(Cache)?.masks ?? NO_MASKS).map((mask) => mask.get(AssetId)?.value ?? "").join("\n"),+ );- const shareable = useDerived(() => {- const node = getParentNode(props.effect);- if (!node) return [];- return listObjectMasks(world, node).filter((source) => !effectHasObjectMask(props.effect, source));- }, (a, b) => a.length === b.length && a.every((source, i) => source.asset.id === b[i]!.asset.id));+ const shareable = createMemo(() => {+ const taken = new Set(used().split("\n"));+ return sources().filter((source) => !taken.has(source.asset.id));+ });const editingMask = createMemo(() => {const mask = pickedMask();@@ -225,7 +225,7 @@ export function EffectsInspector(props: EffectsInspectorProps) {</ControlRow></Show>- <Show when={isVideo()}>+ <Show when={footage()}><For each={masks()}>{(mask, index) => (<ControlRow label="Mask" labelClass={index() > 0 ? "invisible" : undefined}>diff --git a/apps/web/src/components/sidebar-right/inspector/object-mask.tsx b/apps/web/src/components/sidebar-right/inspector/object-mask.tsxindex 962b5b79..30c0c549 100644--- a/apps/web/src/components/sidebar-right/inspector/object-mask.tsx+++ b/apps/web/src/components/sidebar-right/inspector/object-mask.tsx@@ -37,7 +37,6 @@ import {getMaskRestore,getMaskRestoreOf,getObjectTrack,- listObjectMasks,objectMaskMode,objectMaskModel,objectMaskModelLoad,@@ -49,6 +48,7 @@ import {targetEffect,trackObjectMask,undoMaskStroke,+ useObjectMasks,} from "@/engine/object-mask";import { effectOption } from "./effect-types";@@ -337,13 +337,7 @@ export function ObjectMaskInspector(props: ObjectMaskInspectorProps) {const name = useDerived(() => objectMaskName(world, props.mask));// The tracked masks of the clip's footage: the header picks which of them this mask is.- const sources = useDerived(- () => {- const clip = getParentNode(getParentNode(props.mask));- return clip ? listObjectMasks(world, clip) : [];- },- (a, b) => a.length === b.length && a.every((source, i) => source.asset.id === b[i]!.asset.id && source.name === b[i]!.name),- );+ const { masks: sources } = useObjectMasks(() => getParentNode(getParentNode(props.mask)));const source = () => sources().find((option) => option.asset.id === props.mask.get(AssetId)?.value);/** Points the mask at another tracked mask's frames, placed where they were written for. */diff --git a/apps/web/src/components/sidebar-right/inspector/tracking.tsx b/apps/web/src/components/sidebar-right/inspector/tracking.tsxindex a3b132a8..4f9916c2 100644--- a/apps/web/src/components/sidebar-right/inspector/tracking.tsx+++ b/apps/web/src/components/sidebar-right/inspector/tracking.tsx@@ -11,8 +11,7 @@ import { PanelSection } from "@/components/ui/panel-section";import { toast } from "somoto";import { useWorld } from "@diffusionstudio/koota-solid";import { Tool, ToolType } from "@diffusionstudio/runtime";-import { useDerived } from "@/engine/hooks";-import { getVideoRect, listObjectMasks, removeObjectMask } from "@/engine/object-mask";+import { removeObjectMask, useObjectMasks } from "@/engine/object-mask";import type { Entity } from "koota";import type { ObjectMaskSource } from "@/engine/object-mask";@@ -32,11 +31,7 @@ export function TrackingSettings(props: TrackingSettingsProps) {const world = useWorld();const entity = () => props.selection[0]!;- const isVideo = useDerived(() => getVideoRect(world, entity()) !== null);- const tracks = useDerived(- () => listObjectMasks(world, entity()),- (a, b) => a.length === b.length && a.every((track, i) => track.asset.id === b[i]!.asset.id && track.name === b[i]!.name),- );+ const { footage, masks: tracks } = useObjectMasks(entity);const handleRemove = async (track: ObjectMaskSource) => {try {@@ -47,7 +42,7 @@ export function TrackingSettings(props: TrackingSettingsProps) {};return (- <Show when={isVideo()}>+ <Show when={footage()}><PanelSectiontitle="Tracking"actions={diff --git a/apps/web/src/engine/object-mask/copy.tsx b/apps/web/src/engine/object-mask/copy.tsxindex 5be8ed57..816fc4d2 100644--- a/apps/web/src/engine/object-mask/copy.tsx+++ b/apps/web/src/engine/object-mask/copy.tsx@@ -7,11 +7,10 @@ import { Mask } from '@diffusionstudio/reconciler';import { AssetId, Blur, Cache, Library, Mask as MaskTrait, getParentNode } from '@diffusionstudio/runtime';import { getDocumentEditor } from '../editor';-import { getVideoRect } from './media';import { getMaskRestoreOf } from './tracking';import type { Entity, World } from 'koota';-import type { MaskAsset } from '@diffusionstudio/assets';+import type { AssetLibrary, MaskAsset } from '@diffusionstudio/assets';/** A tracked mask in the library: what an effect can be given. */export type ObjectMaskSource = {@@ -23,19 +22,17 @@ export type ObjectMaskSource = {};/**- * The tracked masks of `clip`'s footage, from the library: every mask file- * whose recipe names the video the clip plays, whichever effect — or none —- * holds it now. The recipe is the record: a mask outlives the effects that- * used it, and the library is where it is found again.+ * The tracked masks of the video `footage` (its asset id) in `library`: every+ * mask file whose recipe names it, whichever effect — or none — holds it now.+ * The recipe is the record: a mask outlives the effects that used it, and+ * the library is where it is found again. It reads the library's asset list,+ * a signal, so a memo over it runs again only when the library changes (see+ * `useObjectMasks`).*/-export function listObjectMasks(world: World, clip: Entity): ObjectMaskSource[] {- const library = world.get(Library);- const rect = getVideoRect(world, clip);- if (!library || !rect) return [];-+export function objectMasksOf(library: AssetLibrary, footage: string): ObjectMaskSource[] {const sources: ObjectMaskSource[] = [];for (const asset of library.list()) {- if (asset.type !== 'MASK' || asset.recipe?.source !== rect.asset.id) continue;+ if (asset.type !== 'MASK' || asset.recipe?.source !== footage) continue;sources.push({asset,name: assetName(asset).replace(/\.[^.]+$/, ''),@@ -45,11 +42,6 @@ export function listObjectMasks(world: World, clip: Entity): ObjectMaskSource[]return sources;}-/** Whether `effect` already has a mask of the same frames as `source`. */-export function effectHasObjectMask(effect: Entity, source: ObjectMaskSource): boolean {- return (effect.get(Cache)?.masks ?? []).some((mask) => mask.get(AssetId)?.value === source.asset.id);-}-/*** Puts the tracked frames of `source` under `effect` as a `<mask>` of its* own: the frames, placed at the source time they were written for. The feather carries over from another mask of the same frames on thediff --git a/apps/web/src/engine/object-mask/index.ts b/apps/web/src/engine/object-mask/index.tsindex 4386934c..5ac43f08 100644--- a/apps/web/src/engine/object-mask/index.ts+++ b/apps/web/src/engine/object-mask/index.ts@@ -7,6 +7,7 @@ export * from './media';export * from './tracking';export * from './commit';export * from './copy';+export * from './use-object-masks';export * from './interaction';export * from './brush';export * from './overlay';diff --git a/apps/web/src/engine/object-mask/use-object-masks.ts b/apps/web/src/engine/object-mask/use-object-masks.tsnew file mode 100644index 00000000..13361a94--- /dev/null+++ b/apps/web/src/engine/object-mask/use-object-masks.ts@@ -0,0 +1,46 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * License, v. 2.0. If a copy of the MPL was not distributed with this+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */++import { findGeometryAsset } from '@diffusionstudio/runtime';+import { useWorld } from '@diffusionstudio/koota-solid';+import { createMemo } from 'solid-js';++import { useDerived } from '../hooks';+import { useLibrary } from '../library';+import { objectMasksOf } from './copy';++import type { Accessor } from 'solid-js';+import type { Entity } from 'koota';+import type { ObjectMaskSource } from './copy';++export type ObjectMasks = {+ /** The id of the video the clip plays, or null when it plays none. */+ footage: Accessor<string | null>;+ /** The tracked masks of that video in the library (see `objectMasksOf`). */+ masks: Accessor<ObjectMaskSource[]>;+};++/**+ * The tracked masks of `clip`'s footage, reactively. Which video the clip+ * plays is sampled per tick as an id — a lookup, compared by value — and the+ * library is scanned only when that id or the library's asset list changes,+ * not every frame.+ */+export function useObjectMasks(clip: () => Entity | null | undefined): ObjectMasks {+ const world = useWorld();+ const library = useLibrary();++ const footage = useDerived(() => {+ const node = clip();+ const asset = node ? findGeometryAsset(world, node) : null;+ return asset?.type === 'VIDEO' ? asset.id : null;+ });+ const masks = createMemo(() => {+ const id = footage();+ const current = library();+ return id && current ? objectMasksOf(current, id) : [];+ });++ return { footage, masks };+}