Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Montagne <montagne29@wanadoo.fr>2012-09-08 13:15:52 +0400
committerBastien Montagne <montagne29@wanadoo.fr>2012-09-08 13:15:52 +0400
commit878a739f3c3b516b0c5d08389c40a685aa4ab5ea (patch)
tree4ad9051cfe773e075ad67813c6427aeddae591ed /io_mesh_ply
parent31e961e8c4d2b7a05abe93b149767d08f4f60f65 (diff)
Fix [#32511] blender hangs on importing malformed ply file
Made a few refactoring/style cleanup of code here...
Diffstat (limited to 'io_mesh_ply')
-rw-r--r--io_mesh_ply/import_ply.py112
1 files changed, 55 insertions, 57 deletions
diff --git a/io_mesh_ply/import_ply.py b/io_mesh_ply/import_ply.py
index f5c96d7b..6c80a390 100644
--- a/io_mesh_ply/import_ply.py
+++ b/io_mesh_ply/import_ply.py
@@ -148,68 +148,64 @@ def read(filepath):
b'double': 'd',
b'string': 's'}
obj_spec = object_spec()
+ invalid_ply = (None, None, None)
- file = open(filepath, 'rb') # Only for parsing the header, not binary data
- signature = file.readline()
+ with open(filepath, 'rb') as plyf:
+ signature = plyf.readline()
- if not signature.startswith(b'ply'):
- print('Signature line was invalid')
- return None
+ if not signature.startswith(b'ply'):
+ print('Signature line was invalid')
+ return invalid_ply
- while 1:
- tokens = re.split(br'[ \r\n]+', file.readline())
+ valid_header = False
+ for line in plyf:
+ tokens = re.split(br'[ \r\n]+', line)
- if len(tokens) == 0:
- continue
- if tokens[0] == b'end_header':
- break
- elif tokens[0] == b'comment':
- if len(tokens) < 2:
+ if len(tokens) == 0:
continue
- elif tokens[1] == b'TextureFile':
- if len(tokens) < 4:
- print('Invalid texture line')
+ if tokens[0] == b'end_header':
+ valid_header = True
+ break
+ elif tokens[0] == b'comment':
+ if len(tokens) < 2:
+ continue
+ elif tokens[1] == b'TextureFile':
+ if len(tokens) < 4:
+ print('Invalid texture line')
+ else:
+ texture = tokens[2]
+ continue
+ elif tokens[0] == b'obj_info':
+ continue
+ elif tokens[0] == b'format':
+ if len(tokens) < 3:
+ print('Invalid format line')
+ return invalid_ply
+ if tokens[1] not in format_specs:
+ print('Unknown format', tokens[1])
+ return invalid_ply
+ if tokens[2] != version:
+ print('Unknown version', tokens[2])
+ return invalid_ply
+ format = tokens[1]
+ elif tokens[0] == b'element':
+ if len(tokens) < 3:
+ print(b'Invalid element line')
+ return invalid_ply
+ obj_spec.specs.append(element_spec(tokens[1], int(tokens[2])))
+ elif tokens[0] == b'property':
+ if not len(obj_spec.specs):
+ print('Property without element')
+ return invalid_ply
+ if tokens[1] == b'list':
+ obj_spec.specs[-1].properties.append(property_spec(tokens[4], type_specs[tokens[2]], type_specs[tokens[3]]))
else:
- texture = tokens[2]
- continue
- elif tokens[0] == b'obj_info':
- continue
- elif tokens[0] == b'format':
- if len(tokens) < 3:
- print('Invalid format line')
- return None
- if tokens[1] not in format_specs:
- print('Unknown format', tokens[1])
- return None
- if tokens[2] != version:
- print('Unknown version', tokens[2])
- return None
- format = tokens[1]
- elif tokens[0] == b'element':
- if len(tokens) < 3:
- print(b'Invalid element line')
- return None
- obj_spec.specs.append(element_spec(tokens[1], int(tokens[2])))
- elif tokens[0] == b'property':
- if not len(obj_spec.specs):
- print('Property without element')
- return None
- if tokens[1] == b'list':
- obj_spec.specs[-1].properties.append(property_spec(tokens[4], type_specs[tokens[2]], type_specs[tokens[3]]))
- else:
- obj_spec.specs[-1].properties.append(property_spec(tokens[2], None, type_specs[tokens[1]]))
-
- if format != b'ascii':
- file.close() # was ascii, now binary
- file = open(filepath, 'rb')
+ obj_spec.specs[-1].properties.append(property_spec(tokens[2], None, type_specs[tokens[1]]))
+ if not valid_header:
+ print("Invalid header ('end_header' line not found!)")
+ return invalid_ply
- # skip the header...
- while not file.readline().startswith(b'end_header'):
- pass
-
- obj = obj_spec.load(format_specs[format], file)
-
- file.close()
+ obj = obj_spec.load(format_specs[format], plyf)
return obj_spec, obj, texture
@@ -364,6 +360,8 @@ def load_ply(filepath):
ply_name = bpy.path.display_name_from_filepath(filepath)
mesh = load_ply_mesh(filepath, ply_name)
+ if not mesh:
+ return {'CANCELLED'}
scn = bpy.context.scene
@@ -373,8 +371,8 @@ def load_ply(filepath):
obj.select = True
print('\nSuccessfully imported %r in %.3f sec' % (filepath, time.time() - t))
+ return {'FINISHED'}
def load(operator, context, filepath=""):
- load_ply(filepath)
- return {'FINISHED'}
+ return load_ply(filepath)