// 名称:直接复制 V1.01
// 作者: Brennan Chapman
// 创建合成副本,包括子合成.
// 当然,如果某个合成重复使用,只会复制一次
// 其他的重复项也会指向该合成

function duplicateStructure(comp) {
// Duplicate the incoming comp
var comp = comp.duplicate();

// For each of the layers in the comp, check for subcomps
for (var i=1; i<=comp.numLayers; i++) {

var layer = comp.layer(i);
//检查是否为合成
if (layer.source && layer.source.typeName == "Composition") {

// 检查合成是否被复制
check = checkPreviousComps(layer.source.id);

if (check == null) {
// 子合成没有复制过

// 保存合成id到合成集
var sourceID = layer.source.id;
// Replace the source of the layer, and recursively check in that subcomp for sub-subcomps
layer.replaceSource(duplicateStructure(layer.source), false);
// Store the new comp id to remember the correlation
var destID = layer.source.id;
//Add the correlation to an array
previousComps[sourceID] = destID;
} else {
// Replace the source with the already duplicated comp
layer.replaceSource(check, false);
}
}
}

// For the recursion, return the duplicate comp
return comp;
}

// Checks previous duplications to make sure a comp isn't duplicated twice
function checkPreviousComps(checkID) {
if (previousComps[checkID]) {
return getItemWithID(previousComps[checkID]);
}
return null;
}

// Returns the proect item with the specified ID
function getItemWithID(id) {
for (x=1; x<=app.project.numItems; x++) {
if (app.project.item(x).id == id) {
return app.project.item(x);
}
}
return null;
}

mainComp = app.project.activeItem;
if (mainComp && mainComp.typeName == "Composition") {
app.beginUndoGroup("Duplicate Hierarchy");

previousComps = [];

// Go!
duplicateStructure(mainComp);

app.endUndoGroup();
} else if (!mainComp) {
alert("请选择一个合成进行复制.");
} else {
alert("ERROR: 没有选择合成.n 请在项目面板重新选择.");
}

Leave a Reply

Your email address will not be published.