nexrender icon indicating copy to clipboard operation
nexrender copied to clipboard

Passing arguments to jsx script

Open ahardworker opened this issue 7 years ago • 6 comments

Is your feature request related to a problem? Please describe. It seems that this library doesn't support passing arguments to a JSX script. It would be really handy.

Describe the solution you'd like It would be great if there was a way to send an array of parameters in the JSON object.

Describe alternatives you've considered The only solution is to create a file with the parameters and read it in the script, however that is a bit of a hassle since it would limit the rendering to one item a time.

ahardworker avatar Oct 12 '19 19:10 ahardworker

Nvm, you can easily pass parameters using assets.

ahardworker avatar Oct 13 '19 01:10 ahardworker

@ahardworker pass parameters using assets to the script or to the global operation? If it's the former, what's the syntax for it?

dukuo avatar Jan 31 '20 17:01 dukuo

I'd still like this as a feature (unless passing params with assets is actually easier than I realize—not clear atm).

danielgwilson avatar Feb 03 '20 17:02 danielgwilson

Apparently the current solution, which was briefly discussed on discord (invite), is to compile a script with the correct parameters, encode it in base64 and pass it to the JSON file as an asset using the data:// protocol.

So one solution off the top of my head could be to enhance the ~~data~~ script asset with a parameter parsing/injection feature, which would in turn require to have a different syntax of the script code to handle blank/placeholder parameters maybe?

dukuo avatar Feb 03 '20 17:02 dukuo

So one solution off the top of my head could be to enhance the data asset with a parameter parsing/injection feature, which would in turn require to have a different syntax of the script code to handle blank/placeholder parameters maybe?

I think this idea is in the right direction; I've found the following approach most convenient:

{
    type: "data",
    property: "startTime",
    expression: `
      var comp;
      for (var i = 1; i <= app.project.numItems; i++) {
          if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name === "${compName}")) {
              comp = app.project.item(i);
              break;
          }
      }
      comp.duration = comp.layer(1).outPoint;
      layer.startTime;
    `
  }

Basically, this uses a data/expression asset to execute a script with injected parameters. The final line of the script sets the layer property startTime (arbitrary) to the same value. This allows the opportunity to run an arbitrary script defined in code rather than a file.

If this approach isn't too crazy, it would be nice to have a { type: "script", expression: "..." } option or similar which didn't require dummy values for layerIndex / layerName and property.

—

More useful example:

...
assets.push(
      getAssetForSetPropertyToParentProperty({
        layer: { index: 1, property: "startTime" },
        parent: { index: 2, property: "outPoint" }
      })
    );
...
function getAssetForSetPropertyToParentProperty({
  layer,
  parent
}: {
  layer:
    | { name: string; property: string }
    | { index: number; property: string };
  parent:
    | { name: string; property: string }
    | { index: number; property: string };
}) {
  const asset = {
    type: "data",
    property: `${layer.property}`,
    expression: `
      var comp;
      for (var i = 1; i <= app.project.numItems; i++) {
          if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name === "${compName}")) {
              comp = app.project.item(i);
              break;
          }
      }
      comp.layer(${
        "name" in parent ? '"' + parent.name + '"' : parent.index
      }).${parent.property};
    `
  };
  return "name" in layer
    ? { layerName: layer.name, ...asset }
    : { layerIndex: layer.index, ...asset };
}

danielgwilson avatar Feb 03 '20 21:02 danielgwilson

My PR #307 has an implementation of dynamic variables! Try it out @danielgwilson and let me know if it's of any use to your case.

dukuo avatar Feb 05 '20 08:02 dukuo