Skip to content

Commit

Permalink
add pixel grid tests plus small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocupe committed Nov 25, 2019
1 parent 24ed7fc commit 3aa0cc6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
9 changes: 5 additions & 4 deletions projector.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ def update_pixel_grid(proj_settings, context):
""" Update the pixel grid. Meaning, make it visible by linking the right node and updating the resolution. """
root_tree = get_projector(context).children[0].data.node_tree
nodes = root_tree.nodes
pixel_grid_nodes = nodes['pixel_grid'].node_tree.nodes
width, height = get_resolution(proj_settings, context)
pixel_grid_nodes['_width'].outputs[0].default_value = width
pixel_grid_nodes['_height'].outputs[0].default_value = height
if proj_settings.show_pixel_grid:
pixel_grid_nodes = nodes['pixel_grid'].node_tree.nodes
width, height = get_resolution(proj_settings, context)
pixel_grid_nodes['_width'].outputs[0].default_value = width
pixel_grid_nodes['_height'].outputs[0].default_value = height
root_tree.links.new(nodes['pixel_grid'].outputs[0], nodes['Light Output'].inputs[0])
else:
root_tree.links.new(nodes['Emission'].outputs[0], nodes['Light Output'].inputs[0])
Expand Down Expand Up @@ -509,6 +509,7 @@ def init_projector(proj_settings, context):
update_checker_color(proj_settings, context)
update_lens_shift(proj_settings, context)
update_power(proj_settings, context)
update_pixel_grid(proj_settings, context)


class PROJECTOR_OT_create_projector(Operator):
Expand Down
58 changes: 52 additions & 6 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def setUp(self):
bpy.ops.projector.create()
self.c = bpy.context.object # Camera
self.s = self.c.children[0] # Spot Light
self.nodes = self.s.data.node_tree.nodes

def test_correct_projector_creation(self):
# Camera
Expand Down Expand Up @@ -50,7 +51,8 @@ def test_update_throw_ratio(self):
self.assertAlmostEqual(mapping_node.scale[1], 0.5625)
else:
self.assertEqual(mapping_node.inputs['Scale'].default_value[0], 1)
self.assertAlmostEqual(mapping_node.inputs['Scale'].default_value[1], 0.5625)
self.assertAlmostEqual(
mapping_node.inputs['Scale'].default_value[1], 0.5625)
# Test 2
self.c.proj_settings.throw_ratio = 0.8
self.assertAlmostEqual(self.c.proj_settings.throw_ratio, 0.8)
Expand All @@ -59,8 +61,10 @@ def test_update_throw_ratio(self):
self.assertEqual(mapping_node.scale[0], 1.250)
self.assertAlmostEqual(mapping_node.scale[1], 0.703125)
else:
self.assertEqual(mapping_node.inputs['Scale'].default_value[0], 1.250)
self.assertAlmostEqual(mapping_node.inputs['Scale'].default_value[1], 0.703125)
self.assertEqual(
mapping_node.inputs['Scale'].default_value[0], 1.250)
self.assertAlmostEqual(
mapping_node.inputs['Scale'].default_value[1], 0.703125)

def test_update_lens_shift(self):
self.c.proj_settings.throw_ratio = 1
Expand All @@ -77,8 +81,50 @@ def test_update_lens_shift(self):
self.assertAlmostEqual(mapping_node.translation[0], 0.1)
self.assertAlmostEqual(mapping_node.translation[1], 0.1)
else:
self.assertAlmostEqual(mapping_node.inputs['Location'].default_value[0], 0.1)
self.assertAlmostEqual(mapping_node.inputs['Location'].default_value[1], 0.1)
self.assertAlmostEqual(
mapping_node.inputs['Location'].default_value[0], 0.1)
self.assertAlmostEqual(
mapping_node.inputs['Location'].default_value[1], 0.1)

def test_pixel_gird_on_off(self):
# Turn Pixel Grid on
self.c.proj_settings.show_pixel_grid = True
links_as_node_names = []
for link in self.s.data.node_tree.links:
links_as_node_names.append(
(link.from_node.name, link.to_node.name))
else:
self.assertIn(('pixel_grid', 'Light Output'), links_as_node_names)
self.assertNotIn(('Emission', 'Light Output'), links_as_node_names)
# Turn Pixel Grid off
self.c.proj_settings.show_pixel_grid = False
links_as_node_names = []
for link in self.s.data.node_tree.links:
links_as_node_names.append(
(link.from_node.name, link.to_node.name))
else:
self.assertNotIn(('pixel_grid', 'Light Output'),
links_as_node_names)
self.assertIn(('Emission', 'Light Output'), links_as_node_names)

def test_pixel_grid_resolution(self):
nodes = self.s.data.node_tree.nodes['pixel_grid'].node_tree.nodes
# Check Pixel Grid default resolution
width, height = self.c.proj_settings.resolution.split('x')
self.assertEqual(
nodes['_width'].outputs[0].default_value, float(width))
self.assertEqual(
nodes['_height'].outputs[0].default_value, float(height))
# Check Pixel Grid resolution update
x, y = 1024, 768
self.c.proj_settings.resolution = f'{x}x{y}'
self.assertEqual(
nodes['_width'].outputs[0].default_value, float(x))
self.assertEqual(
nodes['_height'].outputs[0].default_value, float(y))




def test_update_power(self):
new_power = 30
Expand All @@ -93,7 +139,7 @@ def tearDown(self):

def run_tests():
testLoader = unittest.TestLoader()
testLoader.testMethodPrefix = "test" # <- change to only run selected tests
testLoader.testMethodPrefix = "test"
all_tests = testLoader.discover(".", pattern="test*")
unittest.TextTestRunner(verbosity=1).run(all_tests)

Expand Down

0 comments on commit 3aa0cc6

Please sign in to comment.