collada_funcs.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from lxml import etree
  2. from mathutils import Matrix
  3. # function to check if a collada file is valid
  4. # for now if it wil well formatted
  5. # it will also remove namespaces
  6. def check_collada(xml_path):
  7. # check for errors
  8. try:
  9. xml = etree.parse(xml_path)
  10. except:
  11. return None
  12. # function to remove namespaces
  13. def strip_namespace(element):
  14. if element.tag.startswith("{"):
  15. element.tag = element.tag.split("}")[1]
  16. for child in element:
  17. strip_namespace(child)
  18. return element
  19. # return the clean xml element
  20. strip_namespace(xml.getroot())
  21. return xml
  22. # function to return the matrix contained in a matrix element
  23. def get_text_mat4x4(text, offset):
  24. # remove the starting whitespace characters
  25. while (text[0] in " \n\t\r"):
  26. text = text[1:]
  27. text = text.split()
  28. a = []
  29. for num in text:
  30. a.append(float(num))
  31. for i in range(0, offset):
  32. a.pop(0)
  33. mat = Matrix(([a[0], a[1], a[2], a[3]],
  34. [a[4], a[5], a[6], a[7]],
  35. [a[8], a[9], a[10], a[11]],
  36. [a[12], a[13], a[14], a[15]]))
  37. return mat