Skip to content

Commit

Permalink
Merge pull request #441 from og76/patch-2
Browse files Browse the repository at this point in the history
change dir/rot  and rot/dir to use algo
  • Loading branch information
JacquesLucke committed Mar 25, 2016
2 parents 0fb359e + 6bd81fc commit 185f299
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
1 change: 1 addition & 0 deletions algorithms/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
input socket name: rotation (euler), length (float),
output socket name: direction (vector)
axis in ("X", "Y", "Z", "-X", "-Y", "-Z")
needs mathutils module
'''

def generateRotationToDirectionCode(rotationName, lengthName, directionOutputName, axis):
Expand Down
45 changes: 15 additions & 30 deletions nodes/rotation/direction_to_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from bpy.props import *
from mathutils import Vector
from ... events import executionCodeChanged
from ... algorithms.rotation import generateDirectionToRotationCode
from ... base_types.node import AnimationNode

trackAxisItems = [(axis, axis, "") for axis in ("X", "Y", "Z", "-X", "-Y", "-Z")]
Expand All @@ -18,6 +19,8 @@ def create(self):
self.inputs.new("an_VectorSocket", "Direction", "direction")
self.inputs.new("an_VectorSocket", "Guide", "guide").value = [0.0, 0.0, 1.0]
self.outputs.new("an_EulerSocket", "Euler Rotation", "eulerRotation")
self.outputs.new("an_QuaternionSocket", "Quaternion Rotation", "quaternionRotation").hide = True
self.outputs.new("an_MatrixSocket", "Matrix Rotation", "matrixRotation").hide = True
self.width += 20

def draw(self, layout):
Expand All @@ -28,36 +31,18 @@ def draw(self, layout):
layout.label("Must be different", icon = "ERROR")

def getExecutionCode(self):
if self.trackAxis[-1:] == self.guideAxis[-1:]:
yield "eulerRotation = mathutils.Euler((0, 0, 0), 'XYZ')"
return

yield "zero = mathutils.Vector((0, 0, 0))"

yield "if direction == zero: eulerRotation = mathutils.Euler((0, 0, 0), 'XYZ')"
yield "else:"
yield " z = direction.normalized()"
yield " if guide != zero and z.cross(guide) != zero: y = z.cross(guide.normalized())"
if "X" == self.guideAxis: yield " else: y = z.cross(mathutils.Vector((1, 0, 0))) if z.cross(mathutils.Vector((1, 0, 0))) != zero else mathutils.Vector((0, 0, 1))"
if "Y" == self.guideAxis: yield " else: y = z.cross(mathutils.Vector((0, 1, 0))) if z.cross(mathutils.Vector((0, 1, 0))) != zero else mathutils.Vector((0, 0, 1))"
if "Z" == self.guideAxis: yield " else: y = z.cross(mathutils.Vector((0, 0, 1))) if z.cross(mathutils.Vector((0, 0, 1))) != zero else mathutils.Vector((0, 1, 0))"

yield " x = y.cross(z)"

yield " mx, my, mz = " + getAxesChange(self.trackAxis, self.guideAxis)

yield " mat3x3 = mathutils.Matrix().to_3x3()"
yield " mat3x3.col[0], mat3x3.col[1], mat3x3.col[2] = mx, my, mz"
yield " eulerRotation = mat3x3.to_euler()"
isLinked = self.getLinkedOutputsDict()
if not any(isLinked.values()): return ""

rot = "eulerRotation" if isLinked["eulerRotation"] else ""
quat = "quaternionRotation" if isLinked["quaternionRotation"] else ""
mat = "matrixRotation" if isLinked["matrixRotation"] else ""

return generateDirectionToRotationCode("direction", "guide",
self.trackAxis, self.guideAxis,
matrixOutputName = mat,
rotationOutputName = rot,
quaternionOutputName = quat)

def getUsedModules(self):
return ["mathutils"]

def getAxesChange(track, guide):
if track == "X": a = "( z,-y, x)" if guide == "Z" else "( z, x, y)"
elif track == "Y": a = "( y, z, x)" if guide == "Z" else "( x, z,-y)"
elif track == "Z": a = "( x, y, z)" if guide == "X" else "(-y, x, z)"
elif track == "-X": a = "(-z, y, x)" if guide == "Z" else "(-z, x,-y)"
elif track == "-Y": a = "(-y,-z, x)" if guide == "Z" else "( x,-z, y)"
elif track == "-Z": a = "( x,-y,-z)" if guide == "X" else "( y, x,-z)"
return a
14 changes: 12 additions & 2 deletions nodes/rotation/rotation_to_direction.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import bpy
from bpy.props import *
from ... events import executionCodeChanged
from ... algorithms.rotation import generateRotationToDirectionCode
from ... base_types.node import AnimationNode

directionAxisItems = [(axis, axis, "") for axis in ("X", "Y", "Z", "-X", "-Y", "-Z")]

class RotationToDirectionNode(bpy.types.Node, AnimationNode):
bl_idname = "an_RotationToDirectionNode"
bl_label = "Rotation to Direction"

directionAxis = EnumProperty(items = directionAxisItems, update = executionCodeChanged, default = "Z")

def create(self):
self.inputs.new("an_EulerSocket", "Rotation", "rotation")
self.inputs.new("an_FloatSocket", "Length", "length").value = 1
self.outputs.new("an_VectorSocket", "Direction", "direction")
self.width += 20

def draw(self, layout):
layout.prop(self, "directionAxis", expand = True)

def getExecutionCode(self):
yield "matrix = rotation.to_matrix(); matrix.resize_4x4()"
yield "direction = matrix * mathutils.Vector((0, 0, length))"
return generateRotationToDirectionCode("rotation", "length", "direction", self.directionAxis)

def getUsedModules(self):
return ["mathutils"]

0 comments on commit 185f299

Please sign in to comment.