|
1 | 1 | ( function() {
|
2 | 2 | //@include ../dist/aeq.js
|
3 | 3 |
|
| 4 | +aeq.snippet.forEachSelectedLayerOrAll( 'Transform2Effect', transform2Effect ) |
4 | 5 |
|
5 |
| -var comp = aeq.getActiveComp() |
6 |
| -if ( comp === null ) { |
7 |
| - return alert( 'No Comp' ) |
8 |
| -} |
9 |
| - |
10 |
| -var layers = comp.selectedLayers |
11 |
| -if ( !layers.length ) { |
12 |
| - var applyToAll = confirm( 'No layers selected, apply to all?', true ) |
13 |
| - if ( applyToAll ) { |
14 |
| - layers = aeq.getLayers( comp ) |
15 |
| - } else { |
16 |
| - return |
17 |
| - } |
18 |
| -} |
19 |
| - |
20 |
| -aeq.undoGroup( 'Transform2Effect', main, [ layers ] ) |
21 |
| - |
22 |
| -function main( layers ) { |
23 |
| - aeq.forEach( layers, transform2Effect ) |
24 |
| -} |
25 |
| - |
| 6 | +/** |
| 7 | + * Adds a 'Transform' effect to a layer and moves the values and keyframes |
| 8 | + * from the layers transform group to the matching properties on the |
| 9 | + * 'Transform' effect. |
| 10 | + * @param {Layer} layer The layer to move transform values to the transform effect. |
| 11 | + */ |
26 | 12 | function transform2Effect( layer ) {
|
27 | 13 | var effects = layer.property( 'ADBE Effect Parade' )
|
28 | 14 | if ( effects === null || !effects.canAddProperty( 'ADBE Geometry2' ) ) {
|
29 | 15 | return
|
30 | 16 | }
|
31 | 17 |
|
32 | 18 | var effect = effects.addProperty( 'ADBE Geometry2' )
|
33 |
| - var transformGroup = layer.property( 'ADBE Transform Group' ) |
34 |
| - |
35 |
| - moveToEffect( layer, effect, transformGroup ) |
| 19 | + moveToEffect( layer, effect ) |
36 | 20 | }
|
37 | 21 |
|
38 |
| -function moveToEffect( layer, effect, transformGroup ) { |
| 22 | +/** |
| 23 | + * Moves the values and keyframes from the layers transform group to the matching |
| 24 | + * properties a 'Transform' effect. |
| 25 | + * @param {Layer} layer The layer to get transform property values from. |
| 26 | + * @param {PropertyGroup} effect The transform effect to copy properties to. |
| 27 | + */ |
| 28 | +function moveToEffect( layer, effect ) { |
| 29 | + var transformGroup = layer.property( 'ADBE Transform Group' ) |
| 30 | + |
| 31 | + // Get the middle of the comp, used by as the default value of the |
| 32 | + // Position and Anchor Point properties. |
39 | 33 | var comp = layer.containingComp
|
40 | 34 | var middleOfComp = [ comp.width / 2, comp.height / 2, 0 ]
|
| 35 | + |
| 36 | + // The properties matchname in the transform group and their matching |
| 37 | + // matchname in the transform effect. |
| 38 | + // Second value in the array is the default value of the property |
41 | 39 | var effects = {
|
42 | 40 | 'ADBE Anchor Point': [ 'ADBE Geometry2-0001', middleOfComp ],
|
43 | 41 | 'ADBE Position': [ 'ADBE Geometry2-0002', middleOfComp ],
|
44 | 42 | 'ADBE Rotate Z': [ 'ADBE Geometry2-0007', 0 ],
|
45 | 43 | 'ADBE Opacity': [ 'ADBE Geometry2-0008', 100 ]
|
46 | 44 | }
|
47 | 45 |
|
48 |
| - aeq.forEach( effects, setValue ) |
49 |
| - |
50 |
| - function setValue( tranformName, info ) { |
| 46 | + // Copy the values of the transform group properties to the transform |
| 47 | + // effect properties |
| 48 | + aeq.forEach( effects, function( tranformName, info ) { |
51 | 49 | var effectsName = info[ 0 ]
|
52 | 50 | var defaultValue = info[ 1 ]
|
53 |
| - var value = transformGroup.property( tranformName ).value |
54 |
| - if ( aeq.isArray( value ) && value.length === 3 ) { |
55 |
| - value = [ value[0], value[1] ] |
| 51 | + var prop = transformGroup.property( tranformName ) |
| 52 | + |
| 53 | + if ( prop.numKeys ) { |
| 54 | + moveKeys( prop, effect.property( effectsName ) ) |
| 55 | + } else { |
| 56 | + setValue( prop, effect.property( effectsName ) ) |
56 | 57 | }
|
57 |
| - effect.property( effectsName ).setValue( value ) |
58 |
| - transformGroup.property( tranformName ).setValue( defaultValue ) |
59 |
| - } |
60 | 58 |
|
61 |
| - // Scale |
62 |
| - var scaleValue = transformGroup.property( 'ADBE Scale' ).value |
63 |
| - if ( scaleValue[0] !== scaleValue[1] ) { |
| 59 | + prop.setValue( defaultValue ) |
| 60 | + } ) |
| 61 | + |
| 62 | + // Scale is divided into two properties on the transform effect, |
| 63 | + // So can't set in the same ways as the other properties |
| 64 | + var scaleProp = transformGroup.property( 'ADBE Scale' ) |
| 65 | + |
| 66 | + // Check if the scale is not proportional and switch the 'Use uniform scale' switch. |
| 67 | + if ( scaleProp.value[ 0 ] !== scaleProp.value[ 1 ] ) { |
64 | 68 | effect.property( 'ADBE Geometry2-0011' ).setValue( 0 )
|
65 | 69 | }
|
66 |
| - effect.property( 'ADBE Geometry2-0004' ).setValue( scaleValue[0] ) |
67 |
| - effect.property( 'ADBE Geometry2-0003' ).setValue( scaleValue[1] ) |
68 |
| - transformGroup.property( 'ADBE Scale' ).setValue( [ 100, 100, 100 ] ) |
| 70 | + if ( scaleProp.numKeys ) { |
| 71 | + moveScaleKeys( effect, scaleProp ) |
| 72 | + } else { |
| 73 | + setScaleValue( effect, scaleProp ) |
| 74 | + } |
| 75 | + // Set scale to default value |
| 76 | + scaleProp.setValue( [ 100, 100, 100 ] ) |
| 77 | +} |
| 78 | + |
| 79 | +// Normal property functio |
| 80 | + |
| 81 | +/** |
| 82 | + * Moves keys from a source property to a target property. |
| 83 | + * Copies the keys to the target property then removes them from the source. |
| 84 | + * @param {Property} source The property to get keys from |
| 85 | + * @param {Property} target The property to move keys to. |
| 86 | + */ |
| 87 | +function moveKeys( source, target ) { |
| 88 | + var aeqProp = new aeq.Property( source ) |
| 89 | + aeqProp.forEachKey( function( key ) { |
| 90 | + key.copyTo( target ) |
| 91 | + key.remove() |
| 92 | + } ) |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Copies the value of a source property in the transorm group to a target |
| 97 | + * property on the 'transform' effect. |
| 98 | + * @method setValue |
| 99 | + * @param {[type]} source [description] |
| 100 | + * @param {[type]} target [description] |
| 101 | + */ |
| 102 | +function setValue( source, target ) { |
| 103 | + var value = source.value |
| 104 | + |
| 105 | + // The 'Transform' effect is only 2D, so any arry with 3 values needs to |
| 106 | + // be converted to an array with 2 values. |
| 107 | + if ( aeq.isArray( value ) && value.length === 3 ) { |
| 108 | + value = [ value[ 0 ], value[ 1 ] ] |
| 109 | + } |
| 110 | + target.setValue( value ) |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +// Scale property functions |
| 115 | + |
| 116 | +/** |
| 117 | + * Moves the keyframes on a Scale property in the transform group to the two |
| 118 | + * scale properties on the 'Transform' effect. |
| 119 | + * Copies the keyframes and removes the originals. |
| 120 | + * @param {PropertyGroup} effect The 'Transform' effect |
| 121 | + * @param {Property} scaleProp THe Scale property. |
| 122 | + */ |
| 123 | +function moveScaleKeys( effect, scaleProp ) { |
| 124 | + var theProp = new aeq.Property( scaleProp ) |
| 125 | + theProp.forEachKey( function( key ) { |
| 126 | + var keyInfo = key.getKeyinfo() |
| 127 | + var value = keyInfo.value |
| 128 | + var inEase = keyInfo.temporalEase.inEase |
| 129 | + var outEase = keyInfo.temporalEase.outEase |
| 130 | + |
| 131 | + // Paste key for the first scale direction |
| 132 | + keyInfo.property = effect.property( 'ADBE Geometry2-0004' ) |
| 133 | + keyInfo.value = value[ 0 ] |
| 134 | + |
| 135 | + // Only use first value in temporal ease |
| 136 | + keyInfo.temporalEase.inEase = [ inEase[ 0 ] ] |
| 137 | + keyInfo.temporalEase.outEase = [ outEase[ 0 ] ] |
| 138 | + aeq.pasteKey( keyInfo ) |
| 139 | + |
| 140 | + // Paste key for the second scale direction |
| 141 | + keyInfo.property = effect.property( 'ADBE Geometry2-0003' ) |
| 142 | + keyInfo.value = value[ 1 ] |
| 143 | + |
| 144 | + // Only use second value in temporal ease |
| 145 | + keyInfo.temporalEase.inEase = [ inEase[ 1 ] ] |
| 146 | + keyInfo.temporalEase.outEase = [ outEase[ 1 ] ] |
| 147 | + aeq.pasteKey( keyInfo ) |
| 148 | + |
| 149 | + // Remove the key. |
| 150 | + key.remove() |
| 151 | + } ) |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Copies the value of a scale property in the transform group to the two |
| 156 | + * properties on the 'Transform' effect. |
| 157 | + * @param {PropertyGroup} effect The 'Transform' effect |
| 158 | + * @param {Property} scaleProp THe Scale property. |
| 159 | + */ |
| 160 | +function setScaleValue( effect, scaleProp ) { |
| 161 | + var scaleValue = scaleProp.value |
| 162 | + effect.property( 'ADBE Geometry2-0004' ).setValue( scaleValue[ 0 ] ) |
| 163 | + effect.property( 'ADBE Geometry2-0003' ).setValue( scaleValue[ 1 ] ) |
69 | 164 | }
|
70 |
| -})() |
| 165 | +} )() |
0 commit comments