timers.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from .utilities.utilities import (prRed, prGreen, prPurple, prWhite,
  2. prOrange,
  3. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  4. wrapOrange,)
  5. # the first step is to get selection syncing working
  6. import bpy
  7. from bpy import context
  8. from .base_definitions import get_signature_from_edited_tree
  9. from bpy.types import Bone, PoseBone
  10. # just to prevent confusing with Python object:
  11. from bpy.types import Object as bpy_object
  12. SELECTION_POLL_RATE = 0.01
  13. ENABLED = True
  14. cached_selection_state = {}
  15. import bpy
  16. # The following node types represent objects that can be selected.
  17. WATCH_FOR_SELECTION = [ 'xFormBoneNode', 'xFormArmatureNode',
  18. 'xFormGeometryObject', 'xFormObjectInstance', 'xFormCurvePin',
  19. 'xFormGetBone', 'InputExistingGeometryObject']
  20. def get_signature_from_space(node, space):
  21. sig_path=[None,]
  22. for item in space.path[:-1]:
  23. sig_path.append(item.node_tree.nodes.active.name)
  24. return tuple(sig_path+[node.name])
  25. def get_visible_space_data(space_types={'NODE_EDITOR'}):
  26. visible_spaces = []
  27. for wm in bpy.data.window_managers:
  28. for window in wm.windows:
  29. for area in window.screen.areas:
  30. if area.type in space_types:
  31. if area.spaces.active:
  32. visible_spaces.append(area.spaces.active)
  33. return visible_spaces
  34. # Example usage: Get all active 3D Viewport space_data objects
  35. spaces = get_visible_space_data('VIEW_3D')
  36. for space in spaces:
  37. print(f"Found 3D Viewport: shading mode = {space.shading.type}")
  38. # TODO: now that I have this working...
  39. # - Selection State should be tracked and handled on a per-object basis
  40. # so Mantis should update selection state one item at a time
  41. # - finally, I want to extend this selection sync to show active constraints and modifiers
  42. # and to link up with viewport gizmo tools.
  43. # - Serialize this so that it sticks around when saving/loading file
  44. # Eventually:
  45. # - sync scene/object selection to node graph. I have the structure in place
  46. # but I still have to write the selection tracking code.
  47. # Potential small BUG?
  48. # - I am not ensuring the base/root tree is the same for the space and the nodes
  49. # - because I straight up do not store that in the selection_sync dicts
  50. # - easy to fix, will do it when clearing TODO's here.
  51. def timer_update_selection_sync():
  52. if not ENABLED: return None
  53. root_tree = None
  54. tree_path = None
  55. parsed_tree = None
  56. spaces = get_visible_space_data()
  57. if not spaces:
  58. return SELECTION_POLL_RATE
  59. selection_state_start = cached_selection_state.copy()
  60. selection_state = cached_selection_state.copy()
  61. current_tree_selected_total = 0
  62. # we can't get UI context in this function so we have to checc all of them
  63. #maybe the tree itself can load the timer when it is opened in the node editor?
  64. for space in spaces:
  65. # if we are in the node editor, manage node selection
  66. if space.type == "NODE_EDITOR":
  67. root_tree = space.path[0].node_tree
  68. parsed_tree = root_tree.parsed_tree
  69. tree_path = [path_item.node_tree for path_item in space.path]
  70. if root_tree is None:
  71. return SELECTION_POLL_RATE
  72. if root_tree.bl_idname != 'MantisTree':
  73. return SELECTION_POLL_RATE
  74. active_ui_signature = (get_signature_from_space(tree_path[-1].nodes.active, space))
  75. for n in tree_path[-1].nodes:
  76. if n.bl_idname not in WATCH_FOR_SELECTION: continue
  77. selection_state[ui_signature := get_signature_from_space(n, space)] = n.select
  78. current_tree_selected_total += int(n.select)
  79. if parsed_tree is None:
  80. return SELECTION_POLL_RATE
  81. cached_selection_state.update(selection_state)
  82. # We only fire the update if things have changed.
  83. if selection_state != selection_state_start:
  84. from bpy import context
  85. view_layer = context.view_layer
  86. deselect = not bool(current_tree_selected_total)
  87. try:
  88. update_scene_selection_from_node_graph_selection_sync(
  89. active_ui_signature, view_layer,
  90. selection_state, deselect)
  91. except Exception as e:
  92. prRed (e)
  93. return SELECTION_POLL_RATE
  94. return SELECTION_POLL_RATE
  95. def update_scene_selection_from_node_graph_selection_sync(
  96. active_ui_signature, view_layer,
  97. selection_state, deselect ):
  98. from .base_definitions import ( selection_sync_node_to_object,
  99. selection_sync_object_to_node, )
  100. prWhite("Updating Scene Selection from Node Graph Selection")
  101. deselect_do_once = []
  102. for ui_signature, selected in selection_state.items():
  103. this_one_is_active = False
  104. if ui_signature == active_ui_signature: this_one_is_active = True
  105. for object_key in selection_sync_node_to_object.get(ui_signature, []):
  106. id_data = bpy.data.objects.get(object_key[0])
  107. if object_key[1]: # we have a sub_value, it's a bone
  108. if deselect and object_key[0] not in deselect_do_once:
  109. for pb in id_data.pose.bones:
  110. pb.select = False
  111. deselect_do_once.append(object_key[0])
  112. else:
  113. ob = id_data.pose.bones[object_key[1]]
  114. ob.select = selected
  115. if this_one_is_active: # set the active bone
  116. id_data.data.bones.active = ob.bone
  117. # now do its parent object...
  118. if selected:
  119. id_data.select_set(True, view_layer=view_layer)
  120. if this_one_is_active:
  121. view_layer.objects.active = id_data
  122. else: # or the sub_value is None, this is an object
  123. ob = id_data
  124. if deselect: selected = False
  125. ob.select_set(selected, view_layer=view_layer)
  126. if this_one_is_active and not deselect:
  127. view_layer.objects.active = ob