12 Replies Latest reply on Jun 14, 2017 12:46 AM by mschefers

    How to convert all groups to layers

    beloved-jewel Level 1

      Hello all,

       

      I have a gigantic file with multiple groups containing each mutiple subgroups wich contain... you guessed it, many other groups.

       

      I need to convert every single group of this file to a layer, and respect the same order (groups, subgroups and so on) to make this drawing usable and editable.

       

      Unfortunately, I have zero skills in JS or programming.

       

      Has anyone faced the same issue? I'm looking for a script that would create the layers, name them and move the content automatically to the appropriate layer. It would be just waaaayyy to long to do it manually because the file contains over 20,000 shapes (plus I have several other files that I need to convert the same way).

       

      Any help would be very, very appreciated!

       

      Thank you

        • 1. Re: How to convert all groups to layers
          Silly-V Adobe Community Professional

          They got a nice feature for this kind of thing inside the fly-out menu of the Layers panel, it's called "Release to Layers: Sequence". Highlight your layer and use this command to turn all your items in this layer into their own layers. Then, if you select all, you can do a single ungroup to get rid of the top-level groups in each of your layers, so what were your groups are now your layers!

          1 person found this helpful
          • 2. Re: How to convert all groups to layers
            beloved-jewel Level 1

            Hello Silly-V

             

            Unfortunately, this does not seem to work. It moves selected groups under new layers, but keeps the groups intact.

             

            What I am looking for is a way to create a layer with the same name as the group and move the content from the group to this new layer.

             

            But thank you for your suggestion

            • 3. Re: How to convert all groups to layers
              Silly-V Adobe Community Professional

              Ok, give this a try. It works best when you start out with only 1 layer and that layer has only groups inside the top-level.

               

              #target illustrator-19
              function test(){
                  var doc = app.activeDocument;
                  var originalLayer = doc.layers[0];
                  var newLayer, thisGroup, thisContent;
                  for(var i=originalLayer.groupItems.length - 1; i > -1; i--){
                      thisGroup = originalLayer.groupItems[i];
                      newLayer = doc.layers.add();
                      newLayer.name = thisGroup.name;
                      for(var j=thisGroup.pageItems.length - 1; j > -1; j--){
                          thisContent = thisGroup.pageItems[j];
                          thisContent.move(newLayer, ElementPlacement.PLACEATBEGINNING);
                      };
                  };
                  app.redraw();
                  if(originalLayer.pageItems.length == 0){
                      originalLayer.remove();
                  }
              }
              
              test();
              
              • 4. Re: How to convert all groups to layers
                beloved-jewel Level 1

                Hummmm... very interesting! You are definitely onto something here!!

                 

                It works, but only for first level groups... My file goes down to 4 levels.... so close!!!!!

                 

                I am currently on CS6, do you believe it would make any difference if I was on CC instead?

                • 5. Re: How to convert all groups to layers
                  Silly-V Adobe Community Professional

                  Ok so you want nested groups become nested (sub) layers?

                  Yea that's all possible too. Just needs some more code.

                  • 6. Re: How to convert all groups to layers
                    beloved-jewel Level 1

                    Yes, exactly! Nested would be the word!

                    • 7. Re: How to convert all groups to layers
                      beloved-jewel Level 1

                      Problem solved! With the bit of code you posted earlier, I was able to sit down with a programmer and figure out something.

                       

                      Thank you so much Silly-V!!

                      • 8. Re: How to convert all groups to layers
                        W_J_T Level 4

                        It would be beneficial to others searching the forum in the future, if you would post the solution you came up with to complete the thread.

                        • 9. Re: How to convert all groups to layers
                          beloved-jewel Level 1

                          Sure! Sorry, first post here!!

                           

                          // Polyfill for forEach

                          function forEach(array, fn, scope) {

                            scope = scope || array;

                            // array is cloned in order to allow mutations

                            // in `fn` but not suffer from them, so iteration

                            // keeps normally.

                            array = Array.prototype.slice.call(array,0);

                            for(var i = 0, len = array.length; i < len; i++) {

                              fn.call(scope, array[i], i, array);

                            }

                          }

                          function convertGroupToLayer(item) {  

                            var newLayer = app.activeDocument.layers.add(); // creates a new layer

                            newLayer.name = item.name; // rename layer same as group

                            forEach(item.pageItems, function(el) {

                              el.move(newLayer,ElementPlacement.PLACEATEND); // move group to new layer

                            });

                            newLayer.move(item, ElementPlacement.PLACEBEFORE);

                            item.remove();

                              //Recursively convert all layers

                              if (newLayer.groupItems) {

                                  forEach(newLayer.groupItems, convertGroupToLayer);

                              }   

                           

                          }

                          // forEach pageItem of activeLayer, convertGroupToLayer

                          forEach(app.activeDocument.layers[0].groupItems, convertGroupToLayer);

                          • 10. Re: How to convert all groups to layers
                            Silly-V Adobe Community Professional

                            This is a good & very elegant way to do it! I'm curious what kind of workflow this is a part of that you have to have layers instead of groups specifically?

                            • 11. Re: How to convert all groups to layers
                              beloved-jewel Level 1

                              First thing, for a technical reason that I can't explain, using layers accelerates significantly the CPU processing required to manipulate huge files... in a crazy way... I don't know if it's only my PC that behaves like this or if its the same for everyone, but without layers, my PC would have to re-render the drawing every time I clicked on something.... Even on stuff that has nothing to do with the drawing itself; like browsing through menus and toolbars... Insane... My PC would take a minute or two to render the file everysingle time... crazy... With the groups gone, it's smooth sailing. Rendering the document would require 5 seconds instead of a minute. I would be curious why this is happening. Also, performance is significantly better if I move a lot of elements around within that drawing.

                               

                              Second, using layers allows to select elements from differents parts/groups (now layers) and align them together quickly and efficiently rather than having to go inside each nested group, select the desired item, align it, go back a few levels up, then down again, then up again and so on... (or shift selecting items from different groups within the layers toolbar)

                               

                              The only flaw so far with the above code is that everything has to be contained within a group, if some groups already live within a layer, the nested groups won't be processed.

                               

                              As I mentioned earlier, I'm working on drawings containing over 20,000 items each... These drawings are initially handed over in SVG format, then I need to clean them up and re-arrange them in Illustrator. Unfortunately, groups in SVGs do not translate as "layers" in Illustrator. So my challenge was to convert every group to layers by respecting and keeping the layer structure and names.

                               

                              In the end, saving a few minutes here and there while aligning stuff (and waiting for rendering) translates in at least 2 hours saved daily, 10 hours saved every week and a whole week saved after a month..! It was totally worth it!

                              1 person found this helpful
                              • 12. Re: How to convert all groups to layers
                                mschefers

                                Glad to find this. My "problem" would be a little smaller i think.

                                 

                                I use the "Release to Layers > Sequence" as mentioned above, but i want to keep the group names to the new layers instead of Layer 1-x. I could not find out, if theres a way without scripting und i did not find an existing script like. Would this one work for me, too?

                                 

                                Edit: maybe i should've just try befor worked nearly fine. It just did not convert groups to layer, which had no subgroups, but just a few paths. They were just left untouched i guess.