Skip to content

Commit

Permalink
first pass on proper reference distance units in 2.8+
Browse files Browse the repository at this point in the history
  • Loading branch information
stuffmatic committed Dec 4, 2018
1 parent 456603d commit 26b09e5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
53 changes: 53 additions & 0 deletions fspy_blender/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,58 @@ def set_up_3d_area(self, project, camera, update_existing_camera, set_background

break # only set up one 3D area

def set_reference_distance_unit(self, project, camera):
scene = bpy.context.scene
unit_settings = scene.unit_settings
if not hasattr(unit_settings, 'length_unit'):
return

unit = project.reference_distance_unit
is_imperial = False
blender_unit = None
scale_length = None
if unit == 'Millimeters':
blender_unit = 'MILLIMETERS'
scale_length = 0.001
elif unit == 'Centimeters':
blender_unit = 'CENTIMETERS'
scale_length = 0.01
elif unit == 'Meters':
blender_unit = 'METERS'
scale_length = 1.0
blender_unit = 'METERS'
elif unit == 'Kilometers':
blender_unit = 'KILOMETERS'
scale_length = 1000.0
blender_unit = 'KILOMETERS'
elif unit == 'Inches':
blender_unit = 'INCHES'
scale_length = 1.0 / 12.0
is_imperial = True
elif unit == 'Feet':
blender_unit = 'FEET'
scale_length = 1.0
is_imperial = True
elif unit == 'Miles':
blender_unit = 'MILES'
scale_length = 5280.0
is_imperial = True

if blender_unit:
distance_scale = 1.0
if is_imperial:
distance_scale = 1.0 / 3.2808399
unit_settings.system = 'IMPERIAL'
else:
unit_settings.system = 'METRIC'
unit_settings.length_unit = blender_unit
unit_settings.scale_length = scale_length
camera.location.x *= distance_scale
camera.location.y *= distance_scale
camera.location.z *= distance_scale
else:
unit_settings.system = 'NONE'
unit_settings.scale_length = 1.0

def import_fpsy_project(self, context, filepath, update_existing_camera, set_background_image):
try:
Expand All @@ -205,6 +257,7 @@ def import_fpsy_project(self, context, filepath, update_existing_camera, set_bac
return { 'CANCELLED' }
self.set_render_resolution(project)
self.set_up_3d_area(project, camera, update_existing_camera, set_background_image)
self.set_reference_distance_unit(project, camera)
self.report({ 'INFO' }, "Finished setting up camera '" + project.file_name + "'")
return {'FINISHED'}
except fspy.ParsingError as e:
Expand Down
3 changes: 2 additions & 1 deletion fspy_blender/fspy.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, project_path):
project_file.seek(16)
state = json.loads(project_file.read(state_string_size).decode('utf-8'))
self.camera_parameters = CameraParameters(state["cameraParameters"])

calibration_settings = state["calibrationSettingsBase"]
self.reference_distance_unit = calibration_settings["referenceDistanceUnit"]
self.image_data = project_file.read(image_buffer_size)
self.file_name = os.path.basename(project_path)
Binary file added test_data/fspy_github_bug_5.fspy
Binary file not shown.

0 comments on commit 26b09e5

Please sign in to comment.