@@ -32,15 +32,25 @@ def _parse_common(raw_object: etree.Element) -> TiledObject:
3232 coordinates = OrderedPair (
3333 float (raw_object .attrib ["x" ]), float (raw_object .attrib ["y" ])
3434 ),
35- visible = bool (int (raw_object .attrib ["visible" ])),
36- size = Size (
37- float (raw_object .attrib ["width" ]), float (raw_object .attrib ["height" ])
38- ),
39- rotation = float (raw_object .attrib ["rotation" ]),
40- name = raw_object .attrib ["name" ],
41- type = raw_object .attrib ["type" ],
4235 )
4336
37+ if raw_object .attrib .get ("width" ) is not None :
38+ common .size = Size (
39+ float (raw_object .attrib ["width" ]), float (raw_object .attrib ["height" ])
40+ )
41+
42+ if raw_object .attrib .get ("visible" ) is not None :
43+ common .visible = bool (int (raw_object .attrib ["visible" ]))
44+
45+ if raw_object .attrib .get ("rotation" ) is not None :
46+ common .rotation = float (raw_object .attrib ["rotation" ])
47+
48+ if raw_object .attrib .get ("name" ) is not None :
49+ common .name = raw_object .attrib ["name" ]
50+
51+ if raw_object .attrib .get ("type" ) is not None :
52+ common .type = raw_object .attrib ["type" ]
53+
4454 properties_element = raw_object .find ("./properties" )
4555 if properties_element :
4656 common .properties = parse_properties (properties_element )
@@ -94,9 +104,11 @@ def _parse_polygon(raw_object: etree.Element) -> Polygon:
94104 Polygon: The Polygon object created from the raw object
95105 """
96106 polygon = []
97- for raw_point in raw_object .attrib ["points" ].split (" " ):
98- point = raw_point .split ("," )
99- polygon .append (OrderedPair (float (point [0 ]), float (point [1 ])))
107+ polygon_element = raw_object .find ("./polygon" )
108+ if polygon_element is not None :
109+ for raw_point in polygon_element .attrib ["points" ].split (" " ):
110+ point = raw_point .split ("," )
111+ polygon .append (OrderedPair (float (point [0 ]), float (point [1 ])))
100112
101113 return Polygon (points = polygon , ** _parse_common (raw_object ).__dict__ )
102114
@@ -204,23 +216,19 @@ def _get_parser(raw_object: etree.Element) -> Callable[[etree.Element], TiledObj
204216 Returns:
205217 Callable[[Element], Object]: The parser function.
206218 """
207- if raw_object .find ("./ellipse" ):
219+ if raw_object .find ("./ellipse" ) is not None :
208220 return _parse_ellipse
209221
210- if raw_object .find ("./point" ):
222+ if raw_object .find ("./point" ) is not None :
211223 return _parse_point
212224
213- if raw_object .attrib .get ("gid" ):
214- # Only tile objects have the `gid` attribute
215- return _parse_tile
216-
217- if raw_object .find ("./polygon" ):
225+ if raw_object .find ("./polygon" ) is not None :
218226 return _parse_polygon
219227
220- if raw_object .find ("./polyline" ):
228+ if raw_object .find ("./polyline" ) is not None :
221229 return _parse_polyline
222230
223- if raw_object .find ("./text" ):
231+ if raw_object .find ("./text" ) is not None :
224232 return _parse_text
225233
226234 # If it's none of the above, rectangle is the only one left.
@@ -263,13 +271,22 @@ def parse(raw_object: etree.Element, parent_dir: Optional[Path] = None) -> Tiled
263271 new_tileset_path = tileset_path .parent
264272
265273 new_object = template .find ("./object" )
266- if raw_object .attrib .get ("id" ) and new_object :
267- new_object .attrib ["id" ] = raw_object .attrib ["id" ]
274+ if new_object is not None :
275+ if raw_object .attrib .get ("id" ) is not None :
276+ new_object .attrib ["id" ] = raw_object .attrib ["id" ]
277+
278+ if raw_object .attrib .get ("x" ) is not None :
279+ new_object .attrib ["x" ] = raw_object .attrib ["x" ]
280+
281+ if raw_object .attrib .get ("y" ) is not None :
282+ new_object .attrib ["y" ] = raw_object .attrib ["y" ]
268283
269- if new_object :
270284 raw_object = new_object
271285
286+ if raw_object .attrib .get ("gid" ):
287+ return _parse_tile (raw_object , new_tileset , new_tileset_path )
288+
272289 if raw_object .attrib .get ("gid" ):
273- return _parse_tile (raw_object , new_tileset , new_tileset_path )
290+ return _parse_tile (raw_object )
274291
275292 return _get_parser (raw_object )(raw_object )
0 commit comments