Skip to content

Commit

Permalink
support more math dunder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alphadito authored and carson-katri committed Oct 8, 2023
1 parent f42e0ba commit 1ffed55
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ def __init__(self, socket: bpy.types.NodeSocket = None, value = None):
self.socket_type = type(socket).__name__

def _math(self, other, operation, reverse=False):
if other is None:
vector_or_value = self
else:
vector_or_value = (other, self) if reverse else (self, other)

if self._socket.type == 'VECTOR':
return geometry_script.vector_math(operation=operation, vector=(other, self) if reverse else (self, other))
return geometry_script.vector_math(operation=operation, vector=vector_or_value)
else:
return geometry_script.math(operation=operation, value=(other, self) if reverse else (self, other))
return geometry_script.math(operation=operation, value=vector_or_value)

def __add__(self, other):
return self._math(other, 'ADD')
Expand Down Expand Up @@ -92,6 +97,36 @@ def __mod__(self, other):
def __rmod__(self, other):
return self._math(other, 'MODULO', True)

def __floordiv__(self, other):
return self._math(other, 'DIVIDE')._math(None,'FLOOR')

def __rfloordiv__(self, other):
return self._math(other, 'DIVIDE',True)._math(None,'FLOOR')

def __pow__(self, other):
return self._math(other, 'POWER')

def __rpow__(self, other):
return self._math(other, 'POWER', True)

def __matmul__(self, other):
return self._math(other, 'DOT_PRODUCT')

def __rmatmul__(self, other):
return self._math(other, 'DOT_PRODUCT', True)

def __abs__(self):
return self._math(None,'ABSOLUTE')

def __neg__(self):
return self._math(-1, 'MULTIPLY')

def __pos__(self):
return self

def __round__(self):
return self._math(None,'ROUND')

def _compare(self, other, operation):
return geometry_script.compare(operation=operation, a=self, b=other)

Expand Down

0 comments on commit 1ffed55

Please sign in to comment.