Skip to content

Commit

Permalink
initial commit of proof of concept import of background image and cam…
Browse files Browse the repository at this point in the history
…era parameters
  • Loading branch information
stuffmatic committed Nov 12, 2018
0 parents commit 88ffa82
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* symlink addon folder
3 changes: 3 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Read version

# Create zip called fspy-import-[version].zip
1 change: 1 addition & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python -m test.test
146 changes: 146 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Add-on metadata
bl_info = {
"name": "Import fSpy project",
"author": "Per Gantelius",
"description": "Imports the background image and camera parameters from an fSpy project.",
"version": (1, 0, 0),
"blender": (2, 79, 0),
"location": "File > Import > fSpy",
"url": "TODO",
"wiki_url": "TODO",
"category": "Import-Export"
}

# Reload logic
if "bpy" in locals():
import importlib
importlib.reload(fspy)
else:
from . import fspy

# Wrap the blender related code in a try-catch block to silently fail if
# import bpy fails. This is to allow the unit testing code to import fspy.py
try:
import bpy
import mathutils
import tempfile

def import_fpsy_project(context, filepath, use_some_setting):
project = fspy.Project(filepath)

camera_parameters = project.camera_parameters

# Create a camera
bpy.ops.object.camera_add()
camera = bpy.context.active_object
camera.data.type = 'PERSP'
camera.data.lens_unit = 'FOV'
camera.data.angle = camera_parameters.fov_horiz
camera.name = project.file_name

camera.matrix_world = mathutils.Matrix(camera_parameters.camera_transfrom)


# Set render resolution
render_settings = bpy.context.scene.render
render_settings.resolution_x = camera_parameters.image_width
render_settings.resolution_y = camera_parameters.image_height

x_shift_scale = 1
y_shift_scale = 1
if camera_parameters.image_height > camera_parameters.image_width:
x_shift_scale = camera_parameters.image_width / camera_parameters.image_height
else:
y_shift_scale = camera_parameters.image_height / camera_parameters.image_width

# camera.data.shift_x = x_shift_scale * (0.5 - pp[0])
# camera.data.shift_y = y_shift_scale * (-0.5 + pp[1])

for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
space_data = area.spaces.active

rv3d = space_data.region_3d # Reference 3D view region
space_data.show_background_images = True # Show BG images

bg = space_data.background_images.new()

# Clean up a NamedTemporaryFile on your own
# delete=True means the file will be deleted on close
tmp = tempfile.NamedTemporaryFile(delete=True)
try:
tmp.write(project.image_data)
img = bpy.data.images.load(tmp.name)
img.name = project.file_name
img.pack()
bg.image = img

finally:
tmp.close() # deletes the file

break

return {'FINISHED'}


# ImportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ImportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator


class ImportfSpyProject(Operator, ImportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "io_fspy.import_project" # important since its how bpy.ops.import_test.some_data is constructed
bl_label = "Import fSpy project"

# ImportHelper mixin class uses this
filename_ext = ".fspy"

filter_glob = StringProperty(
default="*.fspy",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)

# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
use_setting = BoolProperty(
name="Example Boolean",
description="Example Tooltip",
default=True,
)

type = EnumProperty(
name="Example Enum",
description="Choose between two items",
items=(('OPT_A', "First Option", "Description one"),
('OPT_B', "Second Option", "Description two")),
default='OPT_A',
)

def execute(self, context):
return import_fpsy_project(context, self.filepath, self.use_setting)

# Only needed if you want to add into a dynamic menu
def menu_func_import(self, context):
self.layout.operator(ImportfSpyProject.bl_idname, text="fSpy import (.fspy)")


def register():
bpy.utils.register_class(ImportfSpyProject)
bpy.types.INFO_MT_file_import.append(menu_func_import)


def unregister():
bpy.utils.unregister_class(ImportfSpyProject)
bpy.types.INFO_MT_file_import.remove(menu_func_import)


if __name__ == "__main__":
register()

except ImportError:
# assume no bpy module. fail silently
pass
32 changes: 32 additions & 0 deletions src/fspy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import json
from struct import *

class CameraParameters:
def __init__(self, json_dict):
principal_point_dict = json_dict["principalPoint"]
self.principal_point = (principal_point_dict["x"], principal_point_dict["y"])
self.fov_horiz = json_dict["horizontalFieldOfView"]
print(json_dict["cameraTransform"]["rows"])
self.camera_transfrom = json_dict["cameraTransform"]["rows"]
self.image_width = json_dict["imageWidth"]
self.image_height = json_dict["imageHeight"]

class Project:
def __init__(self, project_path):

project_file = open(project_path, "rb")

file_id = unpack('<I', project_file.read(4))[0]
self.project_version = unpack('<I', project_file.read(4))[0]
state_string_size = unpack('<I', project_file.read(4))[0]
image_buffer_size = unpack('<I', project_file.read(4))[0]
project_file.seek(16)
state = json.loads(project_file.read(state_string_size).decode('utf-8'))
self.camera_parameters = CameraParameters(state["cameraParameters"])

self.image_data = project_file.read(image_buffer_size)

self.file_name = os.path.basename(project_path)

self.file_size = 0 # TODO
Empty file added test/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from src import fspy

project = fspy.Project('test_data/canon5d_16mm.fspy')
print(project.file_name)
print(project.file_size)
Binary file added test_data/canon5d_16mm.fspy
Binary file not shown.

0 comments on commit 88ffa82

Please sign in to comment.