Minify DAE (collada) files

C++, C#, Java, PHP, ect...
Post Reply
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

Minify DAE (collada) files

Post by a_bertrand »

Some times you really need to think about odd things. As you may know I'm currently working on the 3D port of my game, and not all will be constructed via code. Actually many things will be designed through my softs pipeline (ZBrush and Modo) and then exported / loaded to Three.js which is the framework I use for the 3D.

Three.js can load a couple of formats, and the easiest to use for me is Collada which has a few great points:
- It's actually an XML format
- It's open and well documented
- And my tool (modo) supports it very well.

If you are still following me, you would have already pointed out a weakness: XML files are usually not small. Actually XML tends to be big instead of binary counterparts. You should also know as web developer that big files are not that smart. So I thought why not try to minify my DAE (collada) file? Well, some stuff are obvious: reduce the spaces, returns etc, and some others are like dropping useless information out of the file.

I choose to write a small C# code for that, and... you shall see it's REALLY SMALL:

Code: Select all

        static void Main(string[] args)
        {
            if(args.Length != 1)
            {
                Console.WriteLine("collada_minify.exe <file.dae>");
                return;
            }
            XElement doc = XElement.Load(args[0]);
            doc.Descendants(doc.GetDefaultNamespace() + "asset").Remove();
            doc.Descendants(doc.GetDefaultNamespace() + "extra").Remove();
            doc.Descendants(doc.GetDefaultNamespace() + "technique").Where(row => row.Attribute("profile") != null && row.Attribute("profile").Value == "modo401").Remove();
            StringWriter writer = new StringWriter();
            doc.Save(writer,SaveOptions.DisableFormatting);
            Regex replaceLeaderSpaces = new Regex("^(\\s+)", RegexOptions.Multiline);
            File.WriteAllText(args[0], replaceLeaderSpaces.Replace(writer.GetStringBuilder().ToString(), ""));
        }
That's it! And with this little tool, I reduce my file from about 600Kb to 350Kb, so not really 50% but nearly. So what does it do? It open the DAE file as XML, go through it and drop all the "asset" and "extra" tags. It will then check out all the "technique" tags which have an attribute profile set to "modo401" and drop them as well. Convert the tree back to XML without formatting (removing the spaces and all), and finally go through a Regexp to remove spaces at the beginning of the lines.

If you wonder how that works? It's using LINQ to XML which let you query the XML file nearly like a DB, pretty cool stuff.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Minify DAE (collada) files

Post by Jackolantern »

Nice work! :cool:
The indelible lord of tl;dr
Post Reply

Return to “Coding”