Skip to content

Instantly share code, notes, and snippets.

@turbodrive
Created September 24, 2014 17:15
Show Gist options
  • Save turbodrive/d4547545ed8787a05e45 to your computer and use it in GitHub Desktop.
Save turbodrive/d4547545ed8787a05e45 to your computer and use it in GitHub Desktop.
Positions and rotations JSON exporter for After Effects 3D elements
function getJsonObjectFromLayer(layer) {
var name = layer.name;
var pX = layer.property("Position").value[0];
var pY = layer.property("Position").value[1];
var pZ = -layer.property("Position").value[2];
var rX = -layer.property("xRotation").value;
var rY = -layer.property("yRotation").value;
var rZ = layer.property("zRotation").value;
var jsonObject = '{"name":"' + name + '","x":' + pX + ',"y":' + pY + ',"z":' + pZ + ',"rotationX":' + rX + ',"rotationY":' + rY + ',"rotationZ":' + rZ + '}';
return jsonObject;
}
function getAndWriteData(comp, layer) {
var file = new File("positions.json");
// create file
var numEffects = layer.Effects.numProperties;
// number of effects for the selected layer
if (file.open("w")) {
// open the file
file.writeln('{"targets":[');
// start write the JSON array "targets"
var effectLayer, matchName, i, idLayer, jsonObject;
var listTransformEffectsToExport = [];
var list3dLayerToExport = [];
var j = 1
var nbrTargetExported = 0;
// let's look for a specific Custom Control
for (i = 1; i <= numEffects; i++) {
effectLayer = layer.Effects.property(i);
matchName = effectLayer.matchName;
if (matchName == "Custom TargetFCam") {
// get ids of the targets and store them in an array
idLayer = effectLayer("Target " + j)
while (effectLayer("Target " + j).value != 0) {
idLayer = effectLayer("Target " + j).value;
listTransformEffectsToExport.push(idLayer);
j++
}
}
if (matchName == "3dLayer_Exporter") {
// get ids of the other 3dlayer and store them in an array
for (j = 1; j <= 50; j++) {
idLayer = effectLayer("Layer " + j).value
if (idLayer > 0) {
list3dLayerToExport.push(idLayer);
}
}
}
}
for (i = 0; i < listTransformEffectsToExport.length; i++) {
jsonObject = getJsonObjectFromLayer(comp.layer(listTransformEffectsToExport[i]))
if (i < listTransformEffectsToExport.length - 1) {
jsonObject += ',';
}
// write JSON objects for each stored id Target
file.writeln(jsonObject);
// increase count of exported objects
nbrTargetExported++;
}
file.writeln(']');
// close JSON Array
if (list3dLayerToExport.length > 0) {
file.writeln(',"sprites3d":[');
// start write the JSON array "sprites3d"
for (i = 0; i < list3dLayerToExport.length; i++) {
var layer = comp.layer(list3dLayerToExport[i]);
jsonObject = getJsonObjectFromLayer(comp.layer(list3dLayerToExport[i]));
if (i < list3dLayerToExport.length - 1) {
jsonObject += ',';
}
// write JSON objects for each stored id Layer
file.writeln(jsonObject);
// increase count of exported objects
nbrTargetExported++;
}
file.writeln(']');
// close JSON array
}
file.writeln('}');
// close JSON object
file.close();
// close file
}
var stringTarget = nbrTargetExported > 1 ? "objects" : "object";
alert(nbrTargetExported + " " + stringTarget + " exported successfully.");
// provide info about number of objects exported
}
var comp = app.project.activeItem;
var layer = comp.selectedLayers[0];
// get current selected comp and layer
getAndWriteData(comp, layer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment