File size: 1,827 Bytes
baac5bb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import re
from .utils import FlexibleOptionalInputType, any_type
from .constants import get_category, get_name
def cast_to_str(x):
"""Handles our cast to a string."""
if x is None:
return ''
try:
return str(x)
except (ValueError, TypeError):
return ''
def cast_to_float(x):
"""Handles our cast to a float."""
try:
return float(x)
except (ValueError, TypeError):
return 0.0
def cast_to_bool(x):
"""Handles our cast to a bool."""
try:
return bool(float(x))
except (ValueError, TypeError):
return str(x).lower() not in ['0', 'false', 'null', 'none', '']
output_to_type = {
'STRING': {
'cast': cast_to_str,
'null': '',
},
'FLOAT': {
'cast': cast_to_float,
'null': 0.0,
},
'INT': {
'cast': lambda x: int(cast_to_float(x)),
'null': 0,
},
'BOOLEAN': {
'cast': cast_to_bool,
'null': False,
},
# This can be removed soon, there was a bug where this should have been BOOLEAN
'BOOL': {
'cast': cast_to_bool,
'null': False,
},
}
class RgthreePowerPrimitive:
"""The Power Primitive Node."""
NAME = get_name('Power Primitive')
CATEGORY = get_category()
@classmethod
def INPUT_TYPES(cls): # pylint: disable = invalid-name, missing-function-docstring
return {
"required": {},
"optional": FlexibleOptionalInputType(any_type),
}
RETURN_TYPES = (any_type,)
RETURN_NAMES = ('*',)
FUNCTION = "main"
def main(self, **kwargs):
"""Outputs the expected type."""
output = kwargs.get('value', None)
output_type = re.sub(r'\s*\([^\)]*\)\s*$', '', kwargs.get('type', ''))
output_type = output_to_type[output_type]
cast = output_type['cast']
output = cast(output)
return (output,)
|