22from bpy .types import NodeSocketStandard
33import nodeitems_utils
44from .state import State
5+ import geometry_script
56
67def map_case_name (i ):
78 return ('_' if not i .identifier [0 ].isalpha () else '' ) + i .identifier .replace (' ' , '_' ).upper ()
@@ -16,7 +17,16 @@ def socket_type_to_data_type(socket_type):
1617 return socket_type
1718
1819# The base class all exposed socket types conform to.
19- class Type :
20+ class _TypeMeta (type ):
21+ def __getitem__ (self , args ):
22+ for s in filter (lambda x : isinstance (x , slice ), args ):
23+ if (isinstance (s .start , float ) or isinstance (s .start , int )) and (isinstance (s .stop , float ) or isinstance (s .stop , int )):
24+ print (f"minmax: ({ s .start } , { s .stop } )" )
25+ elif isinstance (s .start , str ):
26+ print (f"{ s .start } = { s .stop } " )
27+ return self
28+
29+ class Type (metaclass = _TypeMeta ):
2030 socket_type : str
2131
2232 def __init__ (self , socket : bpy .types .NodeSocket = None , value = None ):
@@ -41,14 +51,10 @@ def __init__(self, socket: bpy.types.NodeSocket = None, value = None):
4151 self .socket_type = type (socket ).__name__
4252
4353 def _math (self , other , operation , reverse = False ):
44- math_node = State .current_node_tree .nodes .new ('ShaderNodeVectorMath' if self ._socket .type == 'VECTOR' else 'ShaderNodeMath' )
45- math_node .operation = operation
46- State .current_node_tree .links .new (self ._socket , math_node .inputs [1 if reverse else 0 ])
47- if issubclass (type (other ), Type ):
48- State .current_node_tree .links .new (other ._socket , math_node .inputs [0 if reverse else 1 ])
54+ if self ._socket .type == 'VECTOR' :
55+ return geometry_script .vector_math (operation = operation , vector = (other , self ) if reverse else (self , other ))
4956 else :
50- math_node .inputs [0 if reverse else 1 ].default_value = other
51- return Type (math_node .outputs [0 ])
57+ return geometry_script .math (operation = operation , value = (other , self ) if reverse else (self , other ))
5258
5359 def __add__ (self , other ):
5460 return self ._math (other , 'ADD' )
@@ -81,30 +87,19 @@ def __rmod__(self, other):
8187 return self ._math (other , 'MODULO' , True )
8288
8389 def _compare (self , other , operation ):
84- compare_node = State .current_node_tree .nodes .new ('FunctionNodeCompare' )
85- compare_node .data_type = 'FLOAT' if self ._socket .type == 'VALUE' else self ._socket .type
86- compare_node .operation = operation
87- a = None
88- b = None
89- for node_input in compare_node .inputs :
90- if not node_input .enabled :
91- continue
92- elif a is None :
93- a = node_input
94- else :
95- b = node_input
96- State .current_node_tree .links .new (self ._socket , a )
97- if issubclass (type (other ), Type ):
98- State .current_node_tree .links .new (other ._socket , b )
99- else :
100- b .default_value = other
101- return Type (compare_node .outputs [0 ])
90+ return geometry_script .compare (operation = operation , a = self , b = other )
10291
10392 def __eq__ (self , other ):
104- return self ._compare (other , 'EQUAL' )
93+ if self ._socket .type == 'BOOLEAN' :
94+ return self ._boolean_math (other , 'XNOR' )
95+ else :
96+ return self ._compare (other , 'EQUAL' )
10597
10698 def __ne__ (self , other ):
107- return self ._compare (other , 'NOT_EQUAL' )
99+ if self ._socket .type == 'BOOLEAN' :
100+ return self ._boolean_math (other , 'XOR' )
101+ else :
102+ return self ._compare (other , 'NOT_EQUAL' )
108103
109104 def __lt__ (self , other ):
110105 return self ._compare (other , 'LESS_THAN' )
@@ -118,6 +113,44 @@ def __gt__(self, other):
118113 def __ge__ (self , other ):
119114 return self ._compare (other , 'GREATER_EQUAL' )
120115
116+ def _boolean_math (self , other , operation , reverse = False ):
117+ boolean_math_node = State .current_node_tree .nodes .new ('FunctionNodeBooleanMath' )
118+ boolean_math_node .operation = operation
119+ a = None
120+ b = None
121+ for node_input in boolean_math_node .inputs :
122+ if not node_input .enabled :
123+ continue
124+ elif a is None :
125+ a = node_input
126+ else :
127+ b = node_input
128+ State .current_node_tree .links .new (self ._socket , a )
129+ if other is not None :
130+ if issubclass (type (other ), Type ):
131+ State .current_node_tree .links .new (other ._socket , b )
132+ else :
133+ b .default_value = other
134+ return Type (boolean_math_node .outputs [0 ])
135+
136+ def __and__ (self , other ):
137+ return self ._boolean_math (other , 'AND' )
138+
139+ def __rand__ (self , other ):
140+ return self ._boolean_math (other , 'AND' , reverse = True )
141+
142+ def __or__ (self , other ):
143+ return self ._boolean_math (other , 'OR' )
144+
145+ def __ror__ (self , other ):
146+ return self ._boolean_math (other , 'OR' , reverse = True )
147+
148+ def __invert__ (self ):
149+ if self ._socket .type == 'BOOLEAN' :
150+ return self ._boolean_math (None , 'NOT' )
151+ else :
152+ return self ._math (- 1 , 'MULTIPLY' )
153+
121154 def _get_xyz_component (self , component ):
122155 if self ._socket .type != 'VECTOR' :
123156 raise Exception ("`x`, `y`, `z` properties are not available on non-Vector types." )
0 commit comments