Skip to content

Commit

Permalink
refactored compression/layer selector
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed Feb 8, 2019
1 parent 9c1fb30 commit dc3b6dc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 75 deletions.
95 changes: 36 additions & 59 deletions packages/nexrender-core/src/assets/nexrender.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,100 +6,77 @@ try{Object.defineProperty({},'a',{value:0})}catch(err){(function(){var definePro
/* start of nexrender script */
var nexrender = {
composition: null,
compositionName: '/*COMPOSITION*/',
defaultCompositionName: '/*COMPOSITION*/',
types: [CompItem, FolderItem, FootageItem, AVLayer, ShapeLayer, TextLayer, CameraLayer, LightLayer, Property, PropertyGroup],
};
nexrender.types = [CompItem, FolderItem, FootageItem, AVLayer, ShapeLayer, TextLayer, CameraLayer, LightLayer, Property, PropertyGroup];
nexrender.typesMatch = function (types, layer) {
return nexrender.types.filter(function (t) {
return layer instanceof t;
}).length > 0;
};
nexrender.getComposition = function (composition, callback) {
if(!composition){ composition = nexrender.compositionName; }
nexrender.replaceFootage = function (layer, filepath) {
if (!layer) { return false; }
if (composition == nexrender.compositionName && nexrender.composition) {
callback(nexrender.composition);
return;
var file = new File(filepath);if (!file.exists) {
return false;
}
var importOptions = new ImportOptions(file);
//importOptions.importAs = ImportAsType.COMP; // you can do stuff like this at this point for PSDs
var theImport = app.project.importFile(importOptions);
layer.replaceSource(theImport, true);
return true;
};
/* call callback for an every compostion matching specific name */
nexrender.selectCompositionsByName = function(name, callback) {
for (var i = 1; i <= app.project.items.length; i++) {
var item = app.project.items[i];
if (!item instanceof CompItem) {
continue;
}
if (!item instanceof CompItem) continue;
if(composition == "*"){
callback(item)
if (name == "*") {
callback(item);
continue;
}
if (item.name != composition) {
if (item.name != name) {
continue;
}
if(item.name == nexrender.compositionName){
nexrender.composition = item;
}
callback(item);
return;
}
}
};
nexrender.layers = function (name, types, composition) {
/* call callback for an every layer matching specific name and composition */
nexrender.selectLayersByName = function(compositionName, name, callback, types) {
if (!compositionName) compositionName = nexrender.defaultCompositionName;
if (!types) types = nexrender.types;
var layers = [];
nexrender.getComposition(composition, function(comp){
nexrender.selectCompositionsByName(compositionName, function(comp) {
for (var j = 1; j <= comp.numLayers; j++) {
var layer = comp.layer(j);
if (layer.name != name) continue;
if (nexrender.typesMatch(types, layer)) {
layers.push(layer);
callback(layer, name);
}
}
return layers.filter(function (l) {
return l.name == name;
});
});
})
};
/*return a layer by name*/
nexrender.layerName = function (name, types, composition) {
var results = nexrender.layers(name, types, composition);
if (results.length > 0) {
return results[0];
}
return null;
};
/* call callback for an every layer matching specific index and composition */
nexrender.selectLayersByIndex = function(compositionName, index, callback, types) {
if (!compositionName) compositionName = nexrender.defaultCompositionName;
if (!types) types = nexrender.types;
/*return a layer by index*/
nexrender.layerIndex = function (index, composition) {
return nexrender.getComposition(composition, function(comp){
return comp.layer(index);
nexrender.selectCompositionsByName(compositionName, function(comp) {
var layer = comp.layer(index)
if (layer) { callback(layer, index); }
})
}
nexrender.replaceFootage = function (layer, filepath) {
if (!layer) { return false; }
var file = new File(filepath);if (!file.exists) {
return false;
}
var importOptions = new ImportOptions(file);
//importOptions.importAs = ImportAsType.COMP; // you can do stuff like this at this point for PSDs
var theImport = app.project.importFile(importOptions);
layer.replaceSource(theImport, true);
return true;
};
/* end of nexrender script */
Expand Down
45 changes: 29 additions & 16 deletions packages/nexrender-core/src/tasks/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@ const fs = require('fs')
const path = require('path')
const script = require('../assets/nexrender.jsx')

const wrapLayer = (layerName, layerIndex, composition) => (layerName
? `nexrender.layerName('${layerName}', null, ${composition !== undefined ? `'${composition}'` : `null`})`
: `nexrender.layerIndex('${layerIndex}', ${composition !== undefined ? `'${composition}'` : `null`})`
)

const wrapFootage = ({ layerName, layerIndex, dest, composition }) => (`(function() {
nexrender.replaceFootage(
${wrapLayer(layerName, layerIndex, composition)},
'${dest.replace(/\\/g, "\\\\")}'
);
/* helpers */

const selectLayers = ({ composition, layerName, layerIndex }, callbackString) => {
const method = layerName ? 'selectLayersByName' : 'selectLayersByIndex';
const compo = composition === undefined ? 'null' : `'${composition}'`;
const value = layerName ? `'${layerName}'` : layerIndex;

return (`nexrender.${method}(${compo}, ${value}, ${callbackString});`);
}

const renderIf = (value, string, encode) => {
const encoded = !encode ? value : typeof value == 'string' ? `'${value}'` : JSON.stringify(value);
return value === undefined ? '' : string.replace('$value', encoded);
}

/* scripting wrappers */

const wrapFootage = ({ dest, ...asset }) => (`(function() {
${selectLayers(asset, `function(layer) {
nexrender.replaceFootage(layer, '${dest.replace(/\\/g, "\\\\")}')
}`)}
})();\n`)

const wrapData = ({ layerName, layerIndex, property, value, expression, composition }) => (`(function() {
var layer = ${wrapLayer(layerName, layerIndex, composition)}; if (!layer) return false;
var property = layer.property('${property}'); if (!property) return false;
const wrapData = ({ property, value, expression, ...asset }) => (`(function() {
${selectLayers(asset, `function(layer) {
var property = layer.property('${property}');
if (!property) { return false; }
${value !== undefined ? `property.setValue(${typeof value == 'string' ? `'${value}'` : JSON.stringify(value)});` : ''}
${expression !== undefined ? `property.expression = '${expression}';` : ''}
${renderIf(value, `property.setValue($value);`, 1)}
${renderIf(expression, `property.expression = '$value;'`, 0)}
return true;
return true;
}`)}
})();\n`)

const wrapScript = ({ dest }) => (`(function() {
Expand Down

0 comments on commit dc3b6dc

Please sign in to comment.