New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a scene with custom data so that we could reuse the code for building multiple videos through automation NATIVELY #954
Comments
I'll think about some proper support for that but in the meantime, you can already achieve this without forking the project. import {View2D, makeScene2D} from '@motion-canvas/2d';
import {
FullSceneDescription,
ThreadGenerator,
ThreadGeneratorFactory,
ValueDispatcher,
} from '@motion-canvas/core';
type CyclicConfig<T> = (params: T) => CyclicConfig<T>;
export function parametrize<T>(scene: FullSceneDescription, params: T) {
const typeScene = scene as FullSceneDescription<CyclicConfig<T>>;
let newScene = {
...typeScene,
config: typeScene.config(params),
onReplaced: new ValueDispatcher(scene),
};
typeScene.onReplaced.subscribe(value => {
newScene.onReplaced.current = {
...newScene,
config: value.config(params),
};
}, false);
return newScene;
}
export function makeParametrizedScene<T>(
factory: (view: View2D, params: T) => ThreadGenerator,
) {
return makeScene2D(
((params: T) =>
function* (view: View2D) {
yield* factory(view, params);
}) as unknown as ThreadGeneratorFactory<View2D>,
);
} Now you can create a parametrized scene just like you would a normal one: import {waitFor, waitUntil} from '@motion-canvas/core';
import {makeParametrizedScene} from '../utils';
export default makeParametrizedScene(function* (view, color: string) {
view.fill(color);
yield* waitFor(1);
yield* waitUntil('blue');
}); And reuse it multiple times in your project as follows: import {makeProject} from '@motion-canvas/core';
import example from './scenes/example?scene';
import {parametrize} from './utils';
export default makeProject({
scenes: [
parametrize(example, 'red'),
parametrize(example, 'green'),
parametrize(example, 'blue'),
],
}); This should work properly with automatically updating the preview (HMR) and meta files (things like time events). |
Description
A clear and concise description of why the feature is needed.
What problem does it aim to fix?
What benefits does it bring?
Proposed solution
A clear and concise description of how the feature would work.
If applicable, provide an example of the API and how it would be used.
Considered alternatives
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: