Skip to content

Commit

Permalink
Fix perspective camera importing
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnberger committed Feb 17, 2015
1 parent 48b62de commit a5c4849
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
38 changes: 30 additions & 8 deletions sketchup.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class keep_offset(defaultdict):
self[item] = number
return number

cdef double m(double v):
cdef inline double m(double& v):
"""
:param v: value to be converted from inches to meters
:return: value in meters
Expand Down Expand Up @@ -230,22 +230,44 @@ cdef class Camera:
def GetOrientation(self):
cdef SUPoint3D position
cdef SUPoint3D target
cdef SUVector3D up_vector
cdef SUVector3D up_vector = [0,0,0]
check_result(SUCameraGetOrientation(self.camera, &position, &target, &up_vector))
return (m(position.x), m(position.y), m(position.z)), \
(m(target.x), m(target.y), m(target.z)), \
(m(up_vector.x), m(up_vector.y), m(up_vector.x))
(m(up_vector.x), m(up_vector.y), m(up_vector.z))

property fov:
def __get__(self):
#Retrieves the field of view in degrees of a camera object. The field of view is measured along the vertical direction of the camera.
cdef double fov
try:
check_result(SUCameraGetPerspectiveFrustumFOV(self.camera, &fov))
except IOError as e:
fov = 55
return fov
cdef SU_RESULT res = SUCameraGetPerspectiveFrustumFOV(self.camera, &fov)
if res == SU_ERROR_NONE:
return fov
return 55.0

property perspective:
def __get__(self):
cdef bool p
cdef SU_RESULT call_res = SUCameraGetPerspective(self.camera, &p)
if call_res == SU_ERROR_NONE:
return p
return False

property scale:
def __get__(self):
cdef double height = 0
cdef SU_RESULT res = SUCameraGetOrthographicFrustumHeight(self.camera, &height)
if res == SU_ERROR_NONE:
return height
return 1.0

property aspect_ratio:
def __get__(self):
cdef double asp = 1.0
cdef SU_RESULT res = SUCameraGetAspectRatio(self.camera, &asp)
if res == SU_ERROR_NONE:
return asp
return False



Expand Down
49 changes: 14 additions & 35 deletions sketchup_importer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class proxy_dict(dict):
"""
def __init__(self, *args, **kwargs ):
dict.__init__(self, *args, **kwargs )
print(self.keys())

def __getitem__(self, key):
if key.lower().endswith("_proxy"):
Expand Down Expand Up @@ -108,33 +107,6 @@ def inherent_default_mat(mat, default_material):
return mat_name


def figure_out_sketchup_scaling(transform):
loc, rotation, scale = transform.decompose()
if scale[0] < 0 and scale[1] < 0 and scale[2] < 0:
#print("seems we have flipped component", scale, rotation.to_euler())
euler = rotation.to_euler()
x_factor = 1.0
y_factor = 1.0
z_factor = 1.0
if 3.1415 < abs(euler.x) < 3.1417:
y_factor = -1.0
z_factor = -1.0
euler.x = 0
if 3.1415 < abs(euler.y) < 3.1417:
x_factor = -1.0
z_factor = -1.0
euler.y = 0
if 3.1415 < abs(euler.z) < 3.1417:
x_factor = -1.0
y_factor = -1.0
euler.z = 0
scale = x_factor * scale[0], y_factor * scale[1], z_factor * scale[2]
rot = euler.to_quaternion()
#print("real scale -> {} rot -> {}", scale, rot.to_euler())
return Matrix.Translation(loc) * rot.to_matrix().to_4x4() * Matrix.Scale(1, 4, scale )#= mat_loc * mat_rot * mat_sca
return transform


class SceneImporter():
def __init__(self):
self.filepath = '/tmp/untitled.skp'
Expand All @@ -155,6 +127,7 @@ def load(self, context, **options):
self.component_skip = proxy_dict()
self.component_depth = proxy_dict()
self.group_written ={}
self.aspect_ratio = context.scene.render.resolution_x / context.scene.render.resolution_y

sketchupLog('importing skp %r' % self.filepath)

Expand Down Expand Up @@ -220,20 +193,20 @@ def write_duplicateable_groups(self):
for k, v in component_stats.items():
name, mat = k
depth = self.component_depth[name]
print(k, len(v), depth)
#print(k, len(v), depth)
comp_def = self.skp_components[name]
if comp_def and depth == 1:
#self.component_skip[(name,mat)] = comp_def.entities
pass
elif comp_def and depth == i:
gname = group_name(name,mat)
if gname in bpy.data.groups:
print("Group {} already defined".format(gname))
#print("Group {} already defined".format(gname))
self.component_skip[(name,mat)] = comp_def.entities
self.group_written[(name,mat)] = bpy.data.groups[gname]
else:
group = bpy.data.groups.new(name=gname)
print("Component written as group".format(gname))
#print("Component written as group".format(gname))
self.conponent_definition_as_group(comp_def.entities, name, Matrix(), default_material=mat, type="Outer", group=group)
self.component_skip[(name,mat)] = comp_def.entities
self.group_written[(name,mat)] = group
Expand Down Expand Up @@ -607,16 +580,22 @@ def write_camera(self, camera, name="Active Camera"):
ob = self.context.object
ob.name = name

z = (Vector(pos) - Vector(target)).normalized()
y = Vector(up).normalized()
x = (y.cross(z)).normalized()
z = (Vector(pos) - Vector(target))
x = Vector(up).cross(z)
y = z.cross(x)


x.normalize()
y.normalize()
z.normalize()

ob.matrix_world.col[0] = x.resized(4)
ob.matrix_world.col[1] = y.resized(4)
ob.matrix_world.col[2] = z.resized(4)

cam = ob.data
cam.angle = pi * camera.fov / 180
aspect_ratio = camera.aspect_ratio if camera.aspect_ratio else self.aspect_ratio
cam.angle = (pi * camera.fov / 180 ) * aspect_ratio
cam.clip_end = self.prefs.camera_far_plane
cam.name = name

Expand Down

0 comments on commit a5c4849

Please sign in to comment.