Created
August 4, 2019 17:22
-
-
Save u27a4/c018c538659d8678aea0a6e3d596ca0c to your computer and use it in GitHub Desktop.
aescript; checking if footages are replaceable with each others.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JSON_.stringify - from: https://github.com/douglascrockford/JSON-js | |
"object"!=typeof JSON_&&(JSON_={}),function(){"use strict";var p,l,n,y,e=/[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;function t(t){return t<10?"0"+t:t}function o(){return this.valueOf()}function a(t){return e.lastIndex=0,e.test(t)?'"'+t.replace(e,function(t){var e=n[t];return"string"==typeof e?e:"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+t+'"'}"function"!=typeof Date.prototype.toJSON_&&(Date.prototype.toJSON_=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+t(this.getUTCMonth()+1)+"-"+t(this.getUTCDate())+"T"+t(this.getUTCHours())+":"+t(this.getUTCMinutes())+":"+t(this.getUTCSeconds())+"Z":null},Boolean.prototype.toJSON_=o,Number.prototype.toJSON_=o,String.prototype.toJSON_=o),"function"!=typeof JSON_.stringify&&(n={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},JSON_.stringify=function(t,e,n){var o;if(l=p="","number"==typeof n)for(o=0;o<n;o+=1)l+=" ";else"string"==typeof n&&(l=n);if((y=e)&&"function"!=typeof e&&("object"!=typeof e||"number"!=typeof e.length))throw new Error("JSON_.stringify");return function t(e,n){var o,r,u,f,i,s=p,c=n[e];switch(c&&"object"==typeof c&&"function"==typeof c.toJSON_&&(c=c.toJSON_(e)),"function"==typeof y&&(c=y.call(n,e,c)),typeof c){case"string":return a(c);case"number":return isFinite(c)?String(c):"null";case"boolean":case"null":return String(c);case"object":if(!c)return"null";if(p+=l,i=[],"[object Array]"===Object.prototype.toString.apply(c)){for(f=c.length,o=0;o<f;o+=1)i[o]=t(o,c)||"null";return u=0===i.length?"[]":p?"[\n"+p+i.join(",\n"+p)+"\n"+s+"]":"["+i.join(",")+"]",p=s,u}if(y&&"object"==typeof y)for(f=y.length,o=0;o<f;o+=1)"string"==typeof y[o]&&(u=t(r=y[o],c))&&i.push(a(r)+(p?": ":" : ")+u);else for(r in c)Object.prototype.hasOwnProperty.call(c,r)&&(u=t(r,c))&&i.push(a(r)+(p?" : ":" : ")+u);return u=0===i.length?"{}":p?"{\n"+p+i.join(", \n"+p)+"\n"+s+"}":"{"+i.join(", ")+"}",p=s,u}}("",{"":t})})}(); | |
function serializeTransform(t) { | |
var xyz = function (v) { return { x: v[0], y: v[1], z: v[2] } }; | |
return { | |
anchorPoint: xyz(t.anchorPoint.value), | |
position: xyz(t.position.value), | |
scale: xyz(t.scale.value), | |
orientation: xyz(t.orientation.value), | |
rotation: xyz(t.rotation.value), | |
opacity: t.opacity.value, | |
}; | |
} | |
function serializeProperty(p) { | |
var properties = []; | |
if (p.numProperties > 0) { | |
for (var i = 1; i <= p.numProperties; i++) { | |
if (p.property(i).matchName === "ADBE Transform Group") continue; | |
if (p.property(i).isModified === false) continue; | |
properties.push(serializeProperty(p.property(i))); | |
} | |
} | |
var keys = []; | |
if (p.numKeys > 0) { | |
for (var i = 1; i <= p.numKeys ; i++) { | |
var key = { | |
keyTime: p.keyTime(i), | |
keyValue: p.keyValue(i), | |
keyInInterpolationType: p.keyInInterpolationType(i), | |
keyInTemporalEase: p.keyInTemporalEase(i), | |
keyOutInterpolationType: p.keyOutInterpolationType(i), | |
keyOutTemporalEase: p.keyOutTemporalEase(i), | |
}; | |
if (p.isSpatial) { | |
key.keyRoving = p.keyRoving(i); | |
key.keyInSpatialTangent = p.keyInSpatialTangent(i); | |
key.keyOutSpatialTangent = p.keyOutSpatialTangent(i); | |
key.keySpatialAutoBezier = p.keySpatialAutoBezier(i); | |
key.keySpatialContinuous = p.keySpatialContinuous(i); | |
key.keyTemporalAutoBezier = p.keyTemporalAutoBezier(i); | |
key.keyTemporalContinuous = p.keyTemporalContinuous(i); | |
} | |
keys.push(key); | |
} | |
} | |
return { | |
matchName: p.matchName, | |
isEffect: p.isEffect, | |
isMask: p.isMask, | |
isModified: p.isModified, | |
isTimeVarying: p.isTimeVarying, | |
isSpatial: p.isSpatial, | |
expression: p.expression, | |
expressionEnabled: p.expressionEnabled, | |
expressionError: p.expressionError, | |
numKeys: p.numKeys, | |
value: p.propertyValueType !== PropertyValueType.NO_VALUE ? p.value : undefined, | |
keys: p.numKeys > 0 ? keys : undefined, | |
properties: properties, | |
}; | |
} | |
function selializeAVLayers(layers) { | |
var serialized = []; | |
var transformToArray = function (t) { return [t.value[0], t.value[1], t.value[2]]; }; | |
for (var i = 0; i < layers.length; i++) { | |
var layer = layers[i]; | |
var sourceProperties = { | |
duration: layer.source.duration, | |
frameDuration: layer.source.frameDuration, | |
frameRate: layer.source.frameRate, | |
height: layer.source.height, | |
pixelAspect: layer.source.pixelAspect, | |
width: layer.source.width, | |
}; | |
if (layer.source instanceof CompItem) { | |
sourceProperties.bgColor = layer.source.bgColor; | |
sourceProperties.frameBlending = layer.source.frameBlending; | |
sourceProperties.frameDuration = layer.source.frameDuration; | |
sourceProperties.motionBlur = layer.source.motionBlur; | |
sourceProperties.motionBlurAdaptiveSampleLimit = layer.source.motionBlurAdaptiveSampleLimit; | |
sourceProperties.motionBlurSamplesPerFrame = layer.source.motionBlurSamplesPerFrame; | |
sourceProperties.motionGraphicsTemplateName = layer.source.motionGraphicsTemplateName; | |
sourceProperties.preserveNestedFrameRate = layer.source.preserveNestedFrameRate; | |
sourceProperties.preserveNestedResolution = layer.source.preserveNestedResolution; | |
sourceProperties.shutterAngle = layer.source.shutterAngle; | |
sourceProperties.shutterPhase = layer.source.shutterPhase; | |
} | |
serialized.push({ | |
autoOrient: layer.autoOrient, | |
blendingMode: layer.blendingMode, | |
collapseTransformation: layer.collapseTransformation, | |
effectsActive: layer.effectsActive, | |
frameBlending: layer.frameBlending, | |
frameBlendingType: layer.frameBlendingType, | |
height: layer.height, | |
motionBlur: layer.motionBlur, | |
preserveTransparency: layer.preserveTransparency, | |
samplingQuality: layer.samplingQuality, | |
timeRemapEnabled: layer.timeRemapEnabled, | |
trackMatteType: layer.trackMatteType, | |
width: layer.width, | |
inPoint: layer.inPoint, | |
outPoint: layer.outPoint, | |
startTime: layer.startTime, | |
stretch: layer.stretch, | |
transform: serializeTransform(layer.transform), | |
property: serializeProperty(layer), | |
sourceProperties: sourceProperties, | |
}); | |
} | |
return serialized; | |
} | |
function collectNestedLayers(parent) { | |
var parents = [parent]; | |
var layers = []; | |
while (parents.length > 0) { | |
var layer = parents.pop(); | |
if (layer.source instanceof CompItem) { | |
for (var i = 1; i <= layer.source.numLayers; i++) { | |
if (layer instanceof AVLayer === false) continue; | |
if (layer.source instanceof FootageItem && | |
layer.source.mainSource instanceof SolidSource) continue; | |
parents.push(layer.source.layers[i]); | |
} | |
} | |
layers.push(layer); | |
} | |
return layers; | |
} | |
function comparePrimitive(a, b, errors, parents, names) { | |
for (var i = 0; i < names.length; i++) { | |
a = a[names[i]]; | |
b = b[names[i]]; | |
} | |
if (a !== b) { | |
errors.push([ | |
parents.concat(names).join(".") + ": ", | |
"\texpected: " + a, | |
"\tactual: " + b, | |
].join('\n')); | |
} | |
return (errors.length === 0); | |
} | |
function compareObject(a, b, errors, parents) { | |
if (comparePrimitive(typeof a, typeof b, errors, parents, [])) { | |
if (typeof a === Array && comparePrimitive(a, b, errors, parents, ['length'])) { | |
for (var i = 0; i < a.length && errors.length <= 0; i++) { | |
compareObject(a[i], b[i], errors, parents.concat([i])); | |
} | |
} else if (typeof a === "object") { | |
for (var key in a) { | |
compareObject(a[key], b[key], errors, parents.concat([key])); | |
if (errors.length > 0) break; | |
} | |
} else { | |
comparePrimitive(a, b, errors, parents, []); | |
} | |
} | |
return errors.length === 0; | |
} | |
function selialize(object, filepath) { | |
var file = new File(filepath); | |
if (file.open("w")) { | |
var layers = app.project.activeItem.selectedLayers; | |
for (var i = 0; i < layers.length; i++) { | |
file.write(JSON_.stringify(object, null, ' ')); | |
} | |
file.execute(); | |
file.close(); | |
} | |
} | |
function isReplaceable(layers) { | |
var errors = []; | |
if (layers.length > 1) { | |
var first = selializeAVLayers(collectNestedLayers(layers[0])); | |
for (var i = 1; i < layers.length && errors.length <= 0; i++) { | |
var other = selializeAVLayers(collectNestedLayers(layers[i])); | |
compareObject(first, other, errors, [layers[i].name]); | |
} | |
} else { | |
errors.push("require 2 or more selected layers"); | |
} | |
return errors; | |
} | |
{ | |
var errors = []; | |
if (app.project.activeItem && app.project.activeItem.selectedLayers) { | |
errors = isReplaceable(app.project.activeItem.selectedLayers); | |
} else { | |
errors.push("require 2 or more selected layers"); | |
} | |
clearOutput(); | |
if (errors.length > 0) { | |
for (var i = 0; i < errors.length; i++) { | |
var lines = errors[i].split('\n'); | |
for (var j = 0; j < lines.length; j++) { | |
writeLn(lines[j]); | |
} | |
} | |
} else { | |
writeLn("no problem fonud"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment