Created
January 19, 2018 09:36
-
-
Save smukkejohan/678de59f77a5c6bb76329e8ef1332b4a to your computer and use it in GitHub Desktop.
sampleColor method for after effects extendscript
This file contains hidden or 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
| //By Johan Bichel Lindegaard 2018 | |
| // function sampleAverageFootageColor(options) { | |
| // return [r,g,b,a] 0-1 | |
| /* | |
| // options { | |
| footageName | |
| inTimeSeconds, | |
| outTimeSeconds, | |
| position, | |
| xy_samples, | |
| sampleType "PER_SECOND", "ABSOLUTE" | |
| samples, | |
| maybe add a minimum samples / totalsamples | |
| */ | |
| function sampleAverageFootageColor(options) { | |
| var footageName = options.footageName || "FOOTAGE-1"; | |
| var inT = options.inTimeSeconds || 0; | |
| var outT = options.outTimeSeconds || project.duration; | |
| var dur = outT - inT; | |
| var pos = options.position || [960,540]; | |
| //var xy_samples = options.xy_samples || [3,3]; | |
| var sampleExtent = options.sampleExtent || [180,180]; | |
| var sampleType = options.sampleType || "PER_SECOND"; | |
| var samples; | |
| if(sampleType === "PER_SECOND") { | |
| samples = options.samples || 4; | |
| samples *= dur; | |
| } else { | |
| samples = options.samples || 20; | |
| } | |
| var uCID = "SAMPLE " + footageName + "[" + inT + "-" + outT + "]"; | |
| var sampleComp = _.getComp("SAMPLE").clone(uCID); | |
| var mLayer = sampleComp.getLayer("SAMPLE_MOSAIC").cloneComp(uCID + "mosaic"); | |
| var mComp = mLayer.getComp(); | |
| //var mE = mLayer("Effects")("Mosaic"); | |
| //mE("Horizontal Blocks").setValue(xy_samples[0]); | |
| //mE("Vertical Blocks").setValue(xy_samples[1]); | |
| // add footage | |
| var fLayer = _.getComp(footageName).addToComp(mComp); | |
| for(var s=0; s<samples; s++) { | |
| var _l = fLayer.clone(" s:" + s + " " + footageName); | |
| // stack sample frames at time 0 | |
| _l.startTime = -(inT + ((dur/samples)*s)); | |
| // set opacity | |
| _l("Transform")("Opacity").setValue(Math.min(100, Math.floor(100.0 / samples * (s+1)))); | |
| } | |
| var expr = "sampleImage([" + pos[0] + "," + pos[1] + "], [" + sampleExtent[0] + "," + sampleExtent[1] + "])"; | |
| var cC = mLayer("Effects")("Color Control")("Color"); | |
| cC.expressionEnabled = true; | |
| cC.expression = expr; | |
| //$.sleep(1000); // hacky way recommended by ADOBE engineers to avoid this being async when it should not be | |
| //var timeStr = system.callSystem("date +%s"); | |
| //$.writeln("Start time: " + timeStr); | |
| while(cC.value[3] === 0) { // TODO: use value of another slider to indicate completion, this would stall indefinetely on completely black footage | |
| // wait for result of expr - in profiling on johans computer under 1 ms | |
| } | |
| var res = cC.value; | |
| // timeStr = system.callSystem("date +%s"); | |
| // $.writeln("After time: " + timeStr); | |
| //$.bp(); | |
| //$.writeln(res); | |
| return res; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment