从extendscript中的路径递归填充树视图

时间:2017-09-28 02:15:37

标签: javascript recursion treeview extendscript

我正在研究Adobe After Effects的extendcript函数,它需要能够将下面的文件路径列表转换为树视图。到目前为止,这是我的代码,它遍历了第一个"级别"很好,但我遇到了递归问题。我非常感谢帮助,我对这个问题的打击并不能让它开始工作!

//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/test image2_0001.txt
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/test image3.0001.txt
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/test image_001.txt
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/0040-Major Trans M13_Logo_v02.xml
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.AO.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.MultiMatteElement.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.RGB_color.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRaySelfIllumination.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRaySpecular.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRayZDepth.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.origRGB.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/Thumbs.db
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/0040-Major Trans M13_logo_02.0080.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/Thumbs.db
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/AO/0040-Major Trans M13_logo_02._AO0080.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/VRaySelfIllumination/0040-Major Trans M13_logo_02._VRaySelfIllumination0080.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/VRaySpecular/0040-Major Trans M13_logo_02._VRaySpecular0080.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/VRayZDepth/0040-Major Trans M13_logo_02._VRayZDepth0080.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/T1/Thumbs.db
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.AO.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.MultiMatteElement.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.RGB_color.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.VRaySelfIllumination.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.VRaySpecular.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.VRayZDepth.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.origRGB.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/Thumbs.db
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..AO.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..MatteText.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..RGB_color.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRaySelfIllumination.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRaySpecular.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRayZDepth.0000.png
//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..origRGB.0000.png

示例输入列表如下所示:

//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/
    test image2_0001.txt
    test image3.0001.txt
    test image_001.txt
    Logo/
        0040-Major Trans M13_Logo_v02.xml
        T1/
            0040-Major Trans M13_01_logo_.AO.0000.png
            0040-Major Trans M13_01_logo_.MultiMatteElement.0000.png
            0040-Major Trans M13_01_logo_.RGB_color.0000.png
            0040-Major Trans M13_01_logo_.VRaySelfIllumination.0000.png
            0040-Major Trans M13_01_logo_.VRaySpecular.0000.png
            0040-Major Trans M13_01_logo_.VRayZDepth.0000.png
            0040-Major Trans M13_01_logo_.origRGB.0000.png
            Thumbs.db
        T2/
            0040-Major Trans M13_logo_02.0080.png
            Thumbs.db
            AO/
                0040-Major Trans M13_logo_02._AO0080.png
            VRaySelfIllumination/
                0040-Major Trans M13_logo_02._VRaySelfIllumination0080.png
            VRaySpecular/
                0040-Major Trans M13_logo_02._VRaySpecular0080.png
            VRayZDepth/
                0040-Major Trans M13_logo_02._VRayZDepth0080.png
    T1/
        Thumbs.db
    Text/
        T1/
            0040-Major Trans M13_01_text_.AO.0000.png
            0040-Major Trans M13_01_text_.MultiMatteElement.0000.png
            0040-Major Trans M13_01_text_.RGB_color.0000.png
            0040-Major Trans M13_01_text_.VRaySelfIllumination.0000.png
            0040-Major Trans M13_01_text_.VRaySpecular.0000.png
            0040-Major Trans M13_01_text_.VRayZDepth.0000.png
            0040-Major Trans M13_01_text_.origRGB.0000.png
            Thumbs.db
        T2 ext/
            NY18_MajorTrans Ext_01_M13_text..AO.0000.png
            NY18_MajorTrans Ext_01_M13_text..MatteText.0000.png
            NY18_MajorTrans Ext_01_M13_text..RGB_color.0000.png
            NY18_MajorTrans Ext_01_M13_text..VRaySelfIllumination.0000.png
            NY18_MajorTrans Ext_01_M13_text..VRaySpecular.0000.png
            NY18_MajorTrans Ext_01_M13_text..VRayZDepth.0000.png
            NY18_MajorTrans Ext_01_M13_text..origRGB.0000.png

最后,上面看起来像是:

{{1}}

1 个答案:

答案 0 :(得分:0)

我通过删除递归来简化你的代码。因为你没有走层次,所以不需要它;每条路径都有层次结构。

结果如下:

enter image description here

{
   //----| Build UI
   var stImporterWin = new Window("palette", "ST Import Panel", undefined, {resizeable:true});
   stImporterWin.orientation = "column";

   var imageSequenceGroup = stImporterWin.add("group");
   imageSequenceGroup.alignment = ["fill", "fill"];
   imageSequenceGroup.orientation = "column";
   var imageSequenceTree = imageSequenceGroup.add("treeview");
   imageSequenceTree.preferredSize = [600,650];
   imageSequenceTree.alignment = ["fill", "fill"];


   stImporterWin.onResize = function () {
      this.layout.resize();
   }

   stImporterWin.show();

   filesArray = ["//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/test image2_0001.txt",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/test image3.0001.txt",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/test image_001.txt",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/0040-Major Trans M13_Logo_v02.xml",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.AO.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.MultiMatteElement.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.RGB_color.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRaySelfIllumination.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRaySpecular.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRayZDepth.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.origRGB.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/Thumbs.db",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/0040-Major Trans M13_logo_02.0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/Thumbs.db",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/AO/0040-Major Trans M13_logo_02._AO0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/VRaySelfIllumination/0040-Major Trans M13_logo_02._VRaySelfIllumination0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/VRaySpecular/0040-Major Trans M13_logo_02._VRaySpecular0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/VRayZDepth/0040-Major Trans M13_logo_02._VRayZDepth0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/T1/Thumbs.db",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.AO.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.MultiMatteElement.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.RGB_color.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.VRaySelfIllumination.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.VRaySpecular.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.VRayZDepth.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.origRGB.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/Thumbs.db",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..AO.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..MatteText.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..RGB_color.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRaySelfIllumination.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRaySpecular.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRayZDepth.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..origRGB.0000.png"];

   rootPath = "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders";

   var node0 = imageSequenceTree.add("node", rootPath);
   node0.expanded = true;

   //recursive tree building
   var rootAsArray = rootPath.split("/");

   //build out arrays from the files
   var subFilesArray = [];
   for (var i = 0; i < filesArray.length; i++) {
      var split = filesArray[i].split("/");
      subFilesArray[i] = split.slice(rootAsArray.length, split.length);
   }

   BuildChildNodes(subFilesArray, node0);

   function BuildChildNodes(subPaths, rootNode) {
      for (var i=0; i<subPaths.length; i++) {
         var subPath = subPaths[i];
         var curr = rootNode;

         for (var j=0; j<subPath.length-1; j++) {
            var path = subPath[j];
            var node = getItem(curr, path);

            if (node) {
               curr = node;
            } else {
               curr = curr.add("node", path);
            }
         }

         curr.add("item", subPath[subPath.length-1]);
      }
   }

   function getItem(node, text) {
      for (var i=0; i<node.items.length; i++) {
         var item = node.items[i];
         if (item.text === text) {
            return item;
         }
      }
      return null;
   }
}