Commit c56ff446 authored by Falk Hofmann's avatar Falk Hofmann
Browse files

Merge branch '4-align-other-axis-when-to-close' into 'master'

Resolve "align other axis when to close"

Closes #4

See merge request falkolon/nuke_align_motion!4
parents 037a9f83 a028835e
import math
from nuke_align_motion import view
from nuke_align_motion.settings import THRESHOLD
from nuke_align_motion.settings import THRESHOLD_ANGLE
from nuke_align_motion import model
reload(view)
......@@ -25,14 +25,15 @@ class Controller:
angle = self.analyze_angle(start_pos, end_pos)
alignment = self.get_alignment(angle)
model.align_nodes(alignment, angle, h_direction, v_direction)
print angle
model.align_nodes(alignment, h_direction, v_direction)
@staticmethod
def get_alignment(angle):
if abs(angle) < THRESHOLD:
if abs(angle) < THRESHOLD_ANGLE:
direction = 'horizontal'
elif 90 - abs(angle) < THRESHOLD:
elif 90 - abs(angle) < THRESHOLD_ANGLE:
direction = 'vertical'
else:
direction = 'diagonal'
......
......@@ -4,8 +4,10 @@ import math
# Import third party modules
import nuke
from nuke_align_motion import settings
def align_nodes(direction, angle, h_direction, v_direction):
def align_nodes(direction, h_direction, v_direction):
nodes = nuke.selectedNodes()
......@@ -16,6 +18,8 @@ def align_nodes(direction, angle, h_direction, v_direction):
reference = edges['west']
else:
reference = edges['east']
if check_threshold(reference, nodes, 'xpos'):
spread_along_axis(nodes, reference, h_direction, 'xpos')
align_horizontal(reference, nodes)
elif direction == 'vertical':
......@@ -23,9 +27,12 @@ def align_nodes(direction, angle, h_direction, v_direction):
reference = edges['north']
else:
reference = edges['south']
if check_threshold(reference, nodes, 'ypos'):
spread_along_axis(nodes, reference, -v_direction, 'ypos')
align_vertical(reference, nodes)
else:
align_diagonal(nodes, edges, h_direction, v_direction, abs(angle))
align_diagonal(nodes, h_direction, v_direction)
def align_horizontal(reference, nodes):
......@@ -38,21 +45,34 @@ def align_vertical(reference, nodes):
node.knob('xpos').setValue(reference.xpos())
def align_diagonal(nodes, edges, h_direction, v_direction, angle):
def align_diagonal(nodes, h_direction, v_direction):
selection = nodes
if h_direction == 1:
x_start = edges['east']
else:
x_start = edges['west']
sorted_nodes = sorted(nodes, key=lambda node: node.xpos()*h_direction)
x_start = sorted_nodes[0]
for node in selection:
for node in sorted_nodes:
if node is not x_start:
ankathete = abs(abs(x_start.xpos()) - abs(node.xpos()))
ankathete = abs(x_start.xpos() - node.xpos())
gegenkathete = abs(math.sin(45) * abs(ankathete))
node['ypos'].setValue(x_start.ypos() - (gegenkathete * v_direction))
def spread_along_axis(nodes, reference, direction, axis):
for index, node in enumerate(nodes):
offset = index * settings.DEFAULT_DISTANCE * direction
node.knob(axis).setValue(reference[axis].value()+offset)
def check_threshold(reference, nodes, axis):
include = [node for node in nodes if abs(reference[axis].value() - node[axis].value()) < abs(node[axis].value())/settings.THRESHOLD_DISTANCE]
if include == nodes:
return True
else:
return False
def get_direction(start_pos, end_pos):
if start_pos[0] - end_pos[0] < 0:
......
OVERLAY_COLOR = (255, 153, 0, 50)
THRESHOLD = 10
COLOR_ENABLE = (255, 120, 0, 70)
COLOR_DRAW = (0, 120, 150, 70)
......@@ -11,4 +7,14 @@ COLOR_START = (200, 100, 20)
COLOR_END = (25, 25, 25)
GRADIENT_WIDTH = 200
DEFAULT_DISTANCE = 100
ELIPSE_WIDTH = 3
GRADIENT_WIDTH = 300
OVERLAY_COLOR = (255, 153, 0, 50)
THRESHOLD_ANGLE = 10
THRESHOLD_DISTANCE = 10.0
......@@ -58,15 +58,29 @@ class Interface(QtWidgets.QWidget):
def draw_line(self, painter):
begin = 4
end = begin/2
points = (QtCore.QPoint(self.start.x() - begin, self.start.y() - begin),
QtCore.QPoint(self.start.x() + begin, self.start.y() + begin),
QtCore.QPoint(self.end.x() + end, self.end.y() + end),
QtCore.QPoint(self.end.x() - end, self.end.y() - end)
)
path = QtGui.QPainterPath()
path.addPolygon(points)
painter.fillPath(path, self._gradient())
# points = (QtCore.QPoint(self.start.x() - begin, self.start.y() - begin),
# QtCore.QPoint(self.start.x() + begin, self.start.y() + begin),
#
# QtCore.QPoint(self.end.x() + end, self.end.y() + end),
# QtCore.QPoint(self.end.x() - end, self.end.y() - end)
# )
#
# path = QtGui.QPainterPath()
# path.addPolygon(points)
# painter.fillPath(path, self._gradient())
pen = QtGui.QPen(self._gradient(), 5)
painter.setPen(pen)
painter.drawLine(self.start, self.end)
self._elipse(painter)
def _elipse(self, painter):
size = settings.ELIPSE_WIDTH
painter.setBrush(QtGui.QColor(*settings.COLOR_START))
painter.drawEllipse(self.start, size, size)
painter.setBrush(QtGui.QColor(*settings.COLOR_END))
painter.drawEllipse(self.end, size, size)
def _gradient(self):
gradient = QtGui.QRadialGradient(self.start, settings.GRADIENT_WIDTH)
......@@ -77,6 +91,7 @@ class Interface(QtWidgets.QWidget):
def get_painter(self):
painter = QtGui.QPainter(self)
painter.setRenderHints(QtGui.QPainter.HighQualityAntialiasing)
painter.setPen(QtCore.Qt.NoPen)
return painter
def keyPressEvent(self, event):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment