blender-x3d-fix / bx3d_fix.py

#!/bin/python
from xml.etree.ElementTree import parse, tostring

def fix_tree(app):
    assert app.tag == 'Appearance'
    mat = app.find('Material')
    texts = app.findall('ImageTexture')
    if mat is None:
        return
    for idx, tex in enumerate(texts):
        app.remove(tex)
        app.insert(idx, tex)


def is_broken_tree(app):
    assert app.tag == 'Appearance'
    mat = app.find('Material')
    texts = app.findall('ImageTexture')
    if not (mat is not None and texts):
        return
    elems = list(app)
    mat_idx = elems.index(mat)
    for tex in texts:
        print mat_idx, elems.index(tex)
        if mat_idx<elems.index(tex):
            return True
    return False


def fix_document(tree):
    for app in tree.findall('.//Appearance'):
        if is_broken_tree(app):
            fix_tree(app)

if __name__ == '__main__':

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('source')
    parser.add_argument('target', default=None, nargs='?')
    opts = parser.parse_args()
    doc = parse(opts.source)
    fix_document(doc)

    if opts.target is None:
        print tostring(doc.getroot())
    else:
        doc.write(opts.target)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.