Skip to content
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

Open
Horopter opened this issue Feb 12, 2024 · 1 comment
Labels
a-core Relates to the core package b-enhancement New feature or request c-discussion The issue is being discussed

Comments

@Horopter
Copy link

Description
A clear and concise description of why the feature is needed.

What problem does it aim to fix?

  • I am having to create multiple projects or manually plugging in data to generate VOD. It could be avoided if make2DScene had a props options where I could get data from the caller and generate video based on that.

What benefits does it bring?

  • Additional layer of programmability and video generation at scale.

Proposed solution

A clear and concise description of how the feature would work.

  • It's simple, add another argument to the API and let user use the thing as they see fit.

If applicable, provide an example of the API and how it would be used.

  • It is extremely helpful in visualizing dry run of the code.

Considered alternatives
A clear and concise description of any alternative solutions or features you've considered.

  • I tried forking the code and starting from scratch. However, although the docs are good, a tutorial might go a long way decreasing the friction.

Additional context
Add any other context or screenshots about the feature request here.

  • None I can think of.
@Horopter Horopter added the b-enhancement New feature or request label Feb 12, 2024
@aarthificial
Copy link
Contributor

aarthificial commented Feb 12, 2024

I'll think about some proper support for that but in the meantime, you can already achieve this without forking the project.
First define these two functions in some utils file:

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).
Sadly, ?scene erases the type so the params are not strongly typed.

@aarthificial aarthificial removed their assignment Feb 12, 2024
@aarthificial aarthificial added c-discussion The issue is being discussed a-core Relates to the core package labels Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-core Relates to the core package b-enhancement New feature or request c-discussion The issue is being discussed
Projects
None yet
Development

No branches or pull requests

2 participants