[READ-ONLY] Mirror of https://github.com/probablykasper/2dcam. After Effects 2d camera
after-effects camera extendscript script
0

Configure Feed

Select the types of activity you want to include in your feed.

Started work on handling new output button

Kasper (Mar 9, 2020, 4:14 AM +0100) 9eee19b9 f12fa3ac

+153 -30
+153 -30
2dcam.jsx
··· 15 15 newOutputButton: IconButton{ \ 16 16 properties: {style:"toolbutton"}, \ 17 17 preferredSize: [30, 30], \ 18 - helpTip: "New 2dCam output comp\\nAlt+click: Apply 2dCam output on the selected layer" \ 18 + helpTip: "New 2dCam output comp\\nAlt+click: Apply 2dCam output to the selected layer" \ 19 19 }, \ 20 20 }'; 21 21 win.grp = win.add(res); ··· 80 80 dialog.show(); 81 81 } 82 82 83 + function applyOutputToLayer(name, outputLayer) { 84 + var outputComp = app.project.items.addComp( 85 + 'applyOutputToLayer', 86 + 192, 87 + 108, 88 + sourceComp.pixelAspect, 89 + sourceComp.duration, 90 + sourceComp.frameRate 91 + ); 92 + outputComp.parentFolder = sourceComp.parentFolder; 93 + // set outputLayer expression for 94 + // - anchor point 95 + // - pos 96 + // - scale 97 + // - rotation 98 + } 99 + 100 + function newOutputComp(name, sourceComp) { 101 + var outputComp = app.project.items.addComp( 102 + sourceComp.name+' '+name, 103 + 192, 104 + 108, 105 + sourceComp.pixelAspect, 106 + sourceComp.duration, 107 + sourceComp.frameRate 108 + ); 109 + outputComp.parentFolder = sourceComp.parentFolder; 110 + outputComp.openInViewer(); 111 + // take outputComp width and height from 112 + // - active 2dCam at current time 113 + // - otherwise, first 2dCam at it's inPoint 114 + // add sourceComp as layer in output comp 115 + // apply output to layer 116 + } 117 + 83 118 // New 2dCam 84 119 win.grp.newCamButton.onClick = function() { 85 - if (app.project.activeItem == undefined || app.project.activeItem == null) { 86 - alert('Please select a composition'); 87 - } else { 88 - currentComp = app.project.activeItem; 120 + var currentComp = app.project.activeItem; 121 + if (!currentComp) return alert('Please select a comp'); 122 + 123 + // ask for name, width, height then apply preset. 124 + var createOutputComp = ScriptUI.environment.keyboardState.altKey; 125 + newCamDialog(currentComp, createOutputComp, function(options) { 126 + var camWidth = options.width; 127 + var camHeight = options.height; 128 + var strokeWidth = Math.ceil(Math.min(camWidth/40, camHeight/40)); 129 + 130 + app.beginUndoGroup('New 2dCam and Output Comp'); 131 + 132 + var shapeLayer = app.project.activeItem.layers.addShape(); 133 + shapeLayer.name = '2dCam'; 134 + shapeLayer.guideLayer = true; 135 + shapeLayer.blendingMode = BlendingMode.DIFFERENCE; 136 + 137 + var shapeContents = shapeLayer.property('ADBE Root Vectors Group'); 138 + 139 + var rectangle = shapeContents.addProperty('ADBE Vector Shape - Rect'); 140 + rectangle.name = 'Rectangle'; 141 + rectangle.property('ADBE Vector Rect Size').setValue([camWidth, camHeight]); 142 + 143 + var stroke = shapeContents.addProperty('ADBE Vector Graphic - Stroke'); 144 + stroke.name = 'Stroke'; 145 + stroke.property('ADBE Vector Stroke Color').setValue([255,255,255]); 146 + stroke.property('ADBE Vector Stroke Width').setValue(strokeWidth); 147 + 148 + var mask = shapeLayer.Masks.addProperty('Mask'); 149 + mask.name = 'Mask'; 150 + mask.locked = true; 151 + mask.property('maskShape').expression = 152 + 'w = content("Rectangle").size[0]/2;'+ 153 + 'h = content("Rectangle").size[1]/2;'+ 154 + 'createPath(points = [[-w,-h], [w,-h], [w,h], [-w,h]])'; 155 + 156 + app.endUndoGroup(); 157 + }); 158 + } 159 + 160 + function newOutputDialog(ok) { 161 + var dialog = new Window("dialog", "New 2dCam", undefined, { resizeable: false }); 162 + 163 + var width = currentComp.width; 164 + var height = currentComp.height; 165 + res = 'group{ \ 166 + orientation:"column", \ 167 + nameGroup: Group{orientation:"row", \ 168 + nameText: StaticText{text:"Name:", justify:"right", preferredSize: [40, 20]}, \ 169 + name: EditText{text:"2dCam", characters:15}, \ 170 + }, \ 171 + buttonsGroup: Group{ \ 172 + orientation:"row", \ 173 + cancelButton: Button{text:"Cancel"}, \ 174 + okButton: Button{text:"OK"}, \ 175 + }, \ 176 + }'; 89 177 90 - // ask for name, width, height then apply preset. 91 - newCamDialog(currentComp, ScriptUI.environment.keyboardState.altKey, function(options) { 92 - var camWidth = options.width; 93 - var camHeight = options.height; 94 - var strokeWidth = Math.ceil(Math.min(camWidth/40, camHeight/40)); 178 + dialog.grp = dialog.add(res); 179 + dialog.grp.nameGroup.name.active = true; 95 180 96 - app.beginUndoGroup('New 2dCam'); 181 + dialog.grp.buttonsGroup.cancelButton.onClick = function() { 182 + dialog.close(); 183 + } 184 + dialog.grp.buttonsGroup.okButton.onClick = function() { 185 + var name = dialog.grp.nameGroup.name.text; 186 + dialog.close(); 187 + ok(dialog.grp.nameGroup.name.text); 188 + } 97 189 98 - var shapeLayer = app.project.activeItem.layers.addShape(); 99 - shapeLayer.name = '2dCam'; 100 - shapeLayer.guideLayer = true; 101 - shapeLayer.blendingMode = BlendingMode.DIFFERENCE; 190 + dialog.layout.layout(true); 191 + dialog.center(); 192 + dialog.show(); 193 + } 194 + 195 + // New output 196 + win.grp.newOutputButton.onClick = function() { 197 + if (!ScriptUI.environment.keyboardState.altKey) { 198 + // new output comp 102 199 103 - var shapeContents = shapeLayer.property('ADBE Root Vectors Group'); 200 + var currentComp = app.project.activeItem; 201 + if (!currentComp) return alert('Please select a comp'); 202 + newOutputDialog(function(name) { 203 + app.beginUndoGroup('New 2dCam Output Comp'); 204 + newOutputComp(name, currentComp); 205 + app.endUndoGroup(); 206 + }); 104 207 105 - var rectangle = shapeContents.addProperty('ADBE Vector Shape - Rect'); 106 - rectangle.name = 'Rectangle'; 107 - rectangle.property('ADBE Vector Rect Size').setValue([camWidth, camHeight]); 208 + } else { 209 + // apply to selected layer 210 + 211 + var currentComp = app.project.activeItem; 212 + if (!currentComp) return alert('Please select one or more comp layers'); 213 + 214 + var selectedLayers = currentComp.selectedLayers; 215 + if (selectedLayers.length == 0) { 216 + return alert('Please select one or more comp layers'); 217 + } 108 218 109 - var stroke = shapeContents.addProperty('ADBE Vector Graphic - Stroke'); 110 - stroke.name = 'Stroke'; 111 - stroke.property('ADBE Vector Stroke Color').setValue([255,255,255]); 112 - stroke.property('ADBE Vector Stroke Width').setValue(strokeWidth); 219 + var outputLayers = []; 220 + var nonCompLayersSelected, compLayersSelected; 221 + for (var i = 0; i < selectedLayers.length; i++) { 222 + var layer = selectedLayers[i]; 223 + if (layer.source && layer.source instanceof CompItem) { 224 + outputLayers.push(layer); 225 + compLayersSelected = true; 226 + } else { 227 + nonCompLayersSelected = true; 228 + } 229 + } 113 230 114 - var mask = shapeLayer.Masks.addProperty('Mask'); 115 - mask.name = 'Mask'; 116 - mask.locked = true; 117 - mask.property('maskShape').expression = 118 - 'w = content("Rectangle").size[0]/2;'+ 119 - 'h = content("Rectangle").size[1]/2;'+ 120 - 'createPath(points = [[-w,-h], [w,-h], [w,h], [-w,h]])'; 231 + if (!compLayersSelected) { 232 + return alert('Please select one or more comp layers'); 233 + } 234 + // if (nonCompLayersSelected) { 235 + // proceed = confirm('Non-comp layers have been selected, and will be skipped. Proceed?'); 236 + // if (!proceed) return; 237 + // } 121 238 239 + newOutputDialog(function(name) { 240 + app.beginUndoGroup('Apply 2dCam Output'); 241 + for (var i = 0; i < outputLayers.length; i++) { 242 + applyOutputToLayer(name, outputLayers[i]); 243 + } 122 244 app.endUndoGroup(); 123 245 }); 246 + 124 247 } 125 248 } 126 249