
    ai>                     z    S SK r S SKJr  S SKJr  S SKJr  SSKJ	r	J
r
JrJrJrJrJrJrJr   " S S\\	5      r\4rg)	    N)Operator)BoolProperty)pgettext_data   )	NodeEditorBasenode_editor_pollnode_space_type_pollget_group_output_nodeget_output_locationget_internal_socketis_visible_socketis_viewer_linkforce_updatec                      ^  \ rS rSr% SrSrSrSS1r\" SSS	9\	S
'   U 4S jr
\S 5       r\S 5       rS rSS jr\S 5       r\SS j5       r\S 5       r\S 5       rS rS rS rSS jrS rS rS rSrU =r$ )NODE_OT_connect_to_output   znode.connect_to_outputzConnect to Outputz>Connect active node to the active output node of the node treeREGISTERUNDOzRun in Geometry Nodes EditorT)namedefaultrun_in_geometry_nodesc                 4   > [         TU ]  " U0 UD6  SU l        g )N )super__init__shader_output_idname)selfargskwargs	__class__s      f/home/wildlama/Downloads/blender-5.1.2-linux-x64/5.1/scripts/startup/bl_operators/connect_to_output.pyr   "NODE_OT_connect_to_output.__init__$   s    $)&)$&!    c                 B    [        X5      =(       a    [        XSS15      $ )z3Already implemented natively for compositing nodes.ShaderNodeTreeGeometryNodeTree)r   r	   )clscontexts     r!   pollNODE_OT_connect_to_output.poll(   s)     !. [$S4DFX3YZ	\r#   c                     U R                   R                   Vs/ s H)  nUR                  S:X  d  M  UR                  S:X  d  M'  UPM+     sn$ s  snf )NSOCKETOUTPUT)	interface
items_tree	item_typein_out)	node_treeitems     r!   get_output_sockets,NODE_OT_connect_to_output.get_output_sockets.   sT    !*!4!4!?!? K!?>>X- 26++2I !? K 	K Ks   AAAc                     US:X  aF  UR                   [        R                  R                  R	                  5       ;   a  SU l        gSU l        gUS:X  a  SU l        gg)z(Get correct output node in shader editorOBJECTShaderNodeOutputLightShaderNodeOutputMaterialWORLDShaderNodeOutputWorldN)idbpydatalightsvaluesr   )r   spaceshader_types      r!   init_shader_variables/NODE_OT_connect_to_output.init_shader_variables3   sL    ("xx388??1133,C),F)G#(?D% $r#   c                    SnU R                  U5      n[        U5      (       a  [        U5       H  u  pgUR                  (       d  M  U R	                  U5      nU(       d  M2  Uc  M7  [        U5      n	U	R                  U   n
U
R                  nX; Vs/ s H  oR                  PM     sn;  a  M~  Un  O   Uc+  UR                  R                  [        S5      SUS9nSUl        U$ s  snf )zLCheck if a viewer output already exists in a node group, otherwise create itNz(Viewer)r-   )r1   socket_typeT)r4   len	enumerateis_inspect_outputhas_socket_other_usersr
   inputslinksfrom_socketr.   
new_socketdata_)r   r2   rF   connect_socketviewer_socketoutput_socketsisocketis_usedgroupoutgroupout_inputrL   links                r!   ensure_viewer_socket.NODE_OT_connect_to_output.ensure_viewer_socket=   s    00;~&~6	+++"99&AGw)1$#8#C)1); . 4 4)u1Uut2B2Bu1UU$(. 7  %//::j!( ; MM.2M+ 2Vs   C#c                     [        U 5      nUcV  U R                  R                  S5      n[        U 5      u  p#X!R                  l        X1R                  l        SUl        SUl        U$ )z8Check if a group output node exists, otherwise create itNodeGroupOutputFT)	r
   nodesnewr   locationxyselectis_active_output)r2   rV   loc_xloc_ys       r!   ensure_group_output-NODE_OT_connect_to_output.ensure_group_outputX   sa     )3 **+<=H.y9LE"'"'#HO(,H%r#   c                 H   [        UR                  5       GH  u  pEU(       a  XC:w  a  M  [        UR                  5      (       d  M0  UR                  S   nUR                  nUR
                  n[        US5      (       d  Mj  [        UR                  R                  R                  5       H!  u  pU
R                  UR                  :X  d  M!    O   W
R                  (       d  M  X;  d  M  UR                  U
5        [        UR                  5      nU R                  XW	S9  GM     g)zLFrom an output node, recursively scan node tree for connected viewer socketsr   r2   )indexN)rH   rK   rG   rL   	from_noderM   hasattrr2   r.   r/   
identifierrI   appendr
   search_connected_viewer_sockets)r'   output_node	r_socketsri   rS   input_socketrX   	next_nodeexternal_socketsocket_indexrT   rV   s               r!   rn   9NODE_OT_connect_to_output.search_connected_viewer_socketsf   s      )););<OA<%%&&#))!, NN	"&"2"29k2209):M:M:W:W:b:b0c,!,,0J0JJ! 1d ///F4K!((0#89L9L#M;;HWc;d!  =r#   c                 @   UR                    H  n[        US5      (       d  M  UR                  c  M%  U R                  UR                  5       H.  nUR                  (       d  M  XB;  d  M  UR                  U5        M0     U R                  UR                  U5        M     g)z1Recursively get all viewer sockets in a node treer2   N)r]   rk   r2   r4   rI   rm   search_viewer_sockets_in_tree)r'   treerp   noderT   s        r!   rw   7NODE_OT_connect_to_output.search_viewer_sockets_in_tree{   s{     JJDt[))>>)!44T^^DF///V5L!((0 E 11$..)L r#   c                     U R                   nUR                  U5        [        UR                  [	        UR
                  5      S-
  5      Ul        g )Nr   )r.   removeminactive_indexrG   r/   )rx   rT   r.   s      r!   remove_socket'NODE_OT_connect_to_output.remove_socket   s?    NN	 !$Y%;%;SAUAU=VYZ=Z![	r#   c                 `    [        UR                  5      nU=(       a    U R                  U5      $ )zGReturn True if link leads to a socket that is already used in this node)r   	to_socketis_socket_used_active_tree)r   rX   rT   s      r!   link_leads_to_used_socket3NODE_OT_connect_to_output.link_leads_to_used_socket   s&    $T^^4A$99&AAr#   c                 T   [        U S5      (       d  / U l        [        R                  R                  R
                  nSnUR                  S:X  a  [        U5      nO#UR                  S:X  a  [        X R                  S9nUb  U R                  X0R                  5        XR                  ;   $ )zLEnsure used sockets in active node tree is calculated and check given socketused_viewer_sockets_active_matNGEOMETRYSHADERoutput_node_idname)
rk   r   r=   r(   
space_datar2   typer
   r   rn   )r   rT   r2   ro   s       r!   r   4NODE_OT_connect_to_output.is_socket_used_active_tree   s    t=>>24D/..88IK~~+3I>8+3IRkRkl&44[BeBef<<<<r#   c                 (   [        U S5      (       Gds  / U l        UR                  S:X  a  [        R                  R
                   H  nUR                   H|  nUR                  S:w  d2  UR                  [        R                  R                  R                  :X  a  MG  [        UR                  5      nUc  Ma  U R                  X@R                  5        M~     M     O[        R                  R                   H  nUR                  [        R                  R                  R                  :X  d  [        UR                  S5      (       d  MR  [        UR                  U R                  S9nUc  Mu  U R                  X@R                  5        M     XR                  ;   $ )zOList the other users for this socket (other materials or geometry nodes groups)other_viewer_sockets_usersNodeSocketGeometryNODESr]   r   )rk   r   rF   r=   r>   objects	modifiersr   
node_groupr(   r   r2   r
   rn   	materialsr   )r   rT   objmodro   mats         r!   rJ   0NODE_OT_connect_to_output.has_socket_other_users   s.   t9::.0D+!!%99 88++C"}}88w.#..CKKDZDZDdDd2d$&;CNN&K&2 @@NmNmn  - , 88--C}}(>(>(H(HHPWX[XeXegnPoPo "7+/+D+D#K #.<<[JiJij . 8888r#   c                    Sn/ n[        UR                  5       HV  u  pU	R                  (       a  Us  $ [        U	5      (       d  M,  U(       a  U	R                  U:X  d  ME  UR                  U5        MX     U(       a  US   n[        U5       Hv  u  pUR                  U
   R                   HT  n[        X5      (       d  M  U(       d  U R                  U5      (       d  M4  U[        U5      S-
  :  a	  XxS-      nMO  US   nMV     Mx     U$ )z7Get the next available output socket in the active nodeNr   r   )
rH   outputsrb   r   r   rm   rL   r   r   rG   )r   ry   ro   is_base_node_treerF   
check_typeout_ivalid_outputsrS   outvalid_iout_links               r!   get_output_index*NODE_OT_connect_to_output.get_output_index   s    -FAzz %%zSXX=T$$Q'	 .
 !!$E#M2JA LL177!(88(D,J,J8,T,Ts=1A55$1a%$8E$1!$4E 8 3 r#   c                 6  ^ SSK Jn  [        [        U5      5      n[	        USS 5       H  u  pgUR
                  mU R                  TUUS:X  a  UR                  U   OSS9nXR                  ;   a  U R                  R                  U5        UR                  U   n	U R                  T5      R                  UR                     n
U" X5        XS-      R
                  n[        U4S jUR                   5       5      nUmUR                  nM     UR                  U   $ )z1Create links at each step in the node group path.r   )connect_socketsNrP   r   c              3   p   >#    U  H+  nUR                   S :X  d  M  UR                  T:X  d  M'  Uv   M-     g7f)GROUPN)r   r2   ).0nrx   s     r!   	<genexpr>9NODE_OT_connect_to_output.create_links.<locals>.<genexpr>   s6      *a66W$ t# ?s   66	6)bpy_extras.node_utilsr   listreversedrH   r2   rY   r   delete_socketsr|   rf   rK   rl   nextr]   )r   pathry   active_node_socket_idrF   r   
path_indexpath_elementrQ   
link_startlink_end	next_treerx   s               @r!   create_links&NODE_OT_connect_to_output.create_links   s   9HTN#(1$s)(<$J))D 55k?  $||,AB(, 6 M
  3 33##**=9 &;<J//5<<]=U=UVHJ1 !^,66I $?? D
 D$1$<$<!1 )=2 ||122r#   c                     U R                    H8  nU R                  U5      (       a  M  UR                  nU R                  X!5        M:     g N)r   rJ   id_datar   )r   rT   rx   s      r!   cleanup!NODE_OT_connect_to_output.cleanup   s:    ))F..v66~~""40 *r#   c                    SSK JnJn  UR                  nU R                  UR
                  S:H  :w  a  S1$ UR                  nUR                  n[        R                  R                  R                  Xg4SSS9nSU;  a  S	1$ UR                  n	UR                  R                  n
UR                  R                  nU
R                  nUR                   nU(       d$  [#        S
 UR$                   5       5      (       d  S	1$ / U l        U R)                  XR&                  5        UR$                  (       d  U R+                  5         S	1$ UR
                  S:X  a  U R-                  U	5      nU R/                  XX:H  SSS9nUc  S	1$ S n[1        UR2                  5       H  u  nnUR4                  S:X  d  M  Un  O   UR$                  U   nU" U5      nUc  U R7                  U	US S9nOUR
                  S:X  a  U R9                  XUR:                  5        [=        U	U R>                  S9nU(       d<  U	R                  RA                  U R>                  5      n[C        U	5      Ul"        SUl        U R/                  XX:H  S5      nUc  S	1$ UR$                  U   nU" U5      nURF                  S:X  a  SnOSn[I        U5      S:  a  U RK                  XWW5      nWR2                  W   nU" WU5        U R+                  5         Xl        SUl        [M        U5        S1$ )Nr   )find_base_socket_typer   r&   PASS_THROUGHFT)r_   extendsocket_selectFINISHED	CANCELLEDc              3   8   #    U  H  n[        U5      v   M     g 7fr   )r   )r   r   s     r!   r   3NODE_OT_connect_to_output.invoke.<locals>.<genexpr>  s     !SNS"3C"8"8Ns   r   )r   r   r%   r   r   Volumer   )'r   r   r   r   r   	tree_typemouse_region_xmouse_region_yr=   opsry   rb   r2   	edit_treer   r]   activeanyr   r   rw   r   rf   r   rH   rK   r   rY   rC   rB   r
   r   r^   r   r_   r   rG   r   r   )r   r(   eventr   r   rA   mlocxmlocyselect_nodebase_node_treeactive_treer   r]   r   ro   active_node_socket_indexoutput_node_socket_indexrS   inpnode_outputrF   output_node_inputs                         r!   invoke NODE_OT_connect_to_output.invoke   s   	

 ""%%%//=O*OP"##$$$$ggll))E>%_c)d[(= ((22!!&&!!c!SFNN!SSS=  !**>;N;NO~~LLN=  ??00 22>BK'+'<'<^%BJ[_ (= ($ (/#}$ (,$#K$6$67388z)/0, 8
 !..)ABK/<K'/+/+D+D"K ,E ,(
 __ 00&&u.?.?@ 0#'#<#<K ,2266t7P7PQ':>'J$%*"'+'<'<^%BH($ (/#}$ ..)ABK/<K8++,(+,( t9q=++D:RT_`K'../GH 	%67W|r#   )r   r   r   r   r   )F)__name__
__module____qualname____firstlineno__	bl_idnamebl_labelbl_description
bl_optionsr   __annotations__r   classmethodr)   staticmethodr4   rC   rY   rf   rn   rw   r   r   r   rJ   r   r   r   r   __static_attributes____classcell__)r    s   @r!   r   r      s    (I"HUNf%J (+ 
' \ \
 K K@6   e e( 	M 	M \ \
B
= 98*3B1h hr#   r   )r=   	bpy.typesr   	bpy.propsr   bpy.app.translationsr   rO   node_editor.node_functionsr   r   r	   r
   r   r   r   r   r   r   classes r#   r!   <module>r      s?   
   " 7
 
 
L. L`
 r#   