ThreeMFReader.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Mesh.MeshReader import MeshReader
  4. from UM.Mesh.MeshBuilder import MeshBuilder
  5. from UM.Logger import Logger
  6. from UM.Math.Matrix import Matrix
  7. from UM.Math.Vector import Vector
  8. from UM.Scene.SceneNode import SceneNode
  9. from UM.Scene.GroupDecorator import GroupDecorator
  10. from UM.Math.Quaternion import Quaternion
  11. from UM.Job import Job
  12. import math
  13. import zipfile
  14. try:
  15. import xml.etree.cElementTree as ET
  16. except ImportError:
  17. import xml.etree.ElementTree as ET
  18. ## Base implementation for reading 3MF files. Has no support for textures. Only loads meshes!
  19. class ThreeMFReader(MeshReader):
  20. def __init__(self):
  21. super().__init__()
  22. self._supported_extensions = [".3mf"]
  23. self._namespaces = {
  24. "3mf": "http://schemas.microsoft.com/3dmanufacturing/core/2015/02",
  25. "cura": "http://software.ultimaker.com/xml/cura/3mf/2015/10"
  26. }
  27. def read(self, file_name):
  28. result = SceneNode()
  29. # The base object of 3mf is a zipped archive.
  30. archive = zipfile.ZipFile(file_name, "r")
  31. try:
  32. root = ET.parse(archive.open("3D/3dmodel.model"))
  33. # There can be multiple objects, try to load all of them.
  34. objects = root.findall("./3mf:resources/3mf:object", self._namespaces)
  35. if len(objects) == 0:
  36. Logger.log("w", "No objects found in 3MF file %s, either the file is corrupt or you are using an outdated format", file_name)
  37. return None
  38. for entry in objects:
  39. mesh_builder = MeshBuilder()
  40. node = SceneNode()
  41. vertex_list = []
  42. # for vertex in entry.mesh.vertices.vertex:
  43. for vertex in entry.findall(".//3mf:vertex", self._namespaces):
  44. vertex_list.append([vertex.get("x"), vertex.get("y"), vertex.get("z")])
  45. Job.yieldThread()
  46. triangles = entry.findall(".//3mf:triangle", self._namespaces)
  47. mesh_builder.reserveFaceCount(len(triangles))
  48. for triangle in triangles:
  49. v1 = int(triangle.get("v1"))
  50. v2 = int(triangle.get("v2"))
  51. v3 = int(triangle.get("v3"))
  52. mesh_builder.addFaceByPoints(vertex_list[v1][0], vertex_list[v1][1], vertex_list[v1][2],
  53. vertex_list[v2][0], vertex_list[v2][1], vertex_list[v2][2],
  54. vertex_list[v3][0], vertex_list[v3][1], vertex_list[v3][2])
  55. Job.yieldThread()
  56. # Rotate the model; We use a different coordinate frame.
  57. rotation = Matrix()
  58. rotation.setByRotationAxis(-0.5 * math.pi, Vector(1, 0, 0))
  59. # TODO: We currently do not check for normals and simply recalculate them.
  60. mesh_builder.calculateNormals()
  61. mesh_builder.setFileName(file_name)
  62. mesh_data = mesh_builder.build().getTransformed(rotation)
  63. if not len(mesh_data.getVertices()):
  64. Logger.log("d", "One of the objects does not have vertices. Skipping it.")
  65. continue # This object doesn't have data, so skip it.
  66. node.setMeshData(mesh_data)
  67. node.setSelectable(True)
  68. transformations = root.findall("./3mf:build/3mf:item[@objectid='{0}']".format(entry.get("id")), self._namespaces)
  69. transformation = transformations[0] if transformations else None
  70. if transformation is not None and transformation.get("transform"):
  71. splitted_transformation = transformation.get("transform").split()
  72. ## Transformation is saved as:
  73. ## M00 M01 M02 0.0
  74. ## M10 M11 M12 0.0
  75. ## M20 M21 M22 0.0
  76. ## M30 M31 M32 1.0
  77. ## We switch the row & cols as that is how everyone else uses matrices!
  78. temp_mat = Matrix()
  79. # Rotation & Scale
  80. temp_mat._data[0,0] = splitted_transformation[0]
  81. temp_mat._data[1,0] = splitted_transformation[1]
  82. temp_mat._data[2,0] = splitted_transformation[2]
  83. temp_mat._data[0,1] = splitted_transformation[3]
  84. temp_mat._data[1,1] = splitted_transformation[4]
  85. temp_mat._data[2,1] = splitted_transformation[5]
  86. temp_mat._data[0,2] = splitted_transformation[6]
  87. temp_mat._data[1,2] = splitted_transformation[7]
  88. temp_mat._data[2,2] = splitted_transformation[8]
  89. # Translation
  90. temp_mat._data[0,3] = splitted_transformation[9]
  91. temp_mat._data[1,3] = splitted_transformation[10]
  92. temp_mat._data[2,3] = splitted_transformation[11]
  93. node.setTransformation(temp_mat)
  94. try:
  95. node.getBoundingBox() # Selftest - There might be more functions that should fail
  96. except:
  97. Logger.log("w", "Bounding box test for object failed. Skipping this object")
  98. continue
  99. result.addChild(node)
  100. Job.yieldThread()
  101. # If there is more then one object, group them.
  102. if len(objects) > 1:
  103. group_decorator = GroupDecorator()
  104. result.addDecorator(group_decorator)
  105. elif len(objects) == 1:
  106. if result.getChildren():
  107. result = result.getChildren()[0] # Only one object found, return that.
  108. else: # we failed to load any data
  109. return None
  110. except Exception as e:
  111. Logger.log("e", "exception occured in 3mf reader: %s", e)
  112. try: # Selftest - There might be more functions that should fail
  113. bounding_box = result.getBoundingBox()
  114. bounding_box.isValid()
  115. except:
  116. return None
  117. return result