timers.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if item.node_tree.nodes.active:
  24. sig_path.append(item.node_tree.nodes.active.name)
  25. else:
  26. return () # can't be determined, should not happen in normal use.
  27. return tuple(sig_path+[node.name])
  28. def get_visible_space_data(space_types={'NODE_EDITOR'}):
  29. visible_spaces = []
  30. for wm in bpy.data.window_managers:
  31. for window in wm.windows:
  32. for area in window.screen.areas:
  33. if area.type in space_types:
  34. if area.spaces.active:
  35. visible_spaces.append(area.spaces.active)
  36. return visible_spaces
  37. # Example usage: Get all active 3D Viewport space_data objects
  38. spaces = get_visible_space_data('VIEW_3D')
  39. for space in spaces:
  40. print(f"Found 3D Viewport: shading mode = {space.shading.type}")
  41. # TODO: now that I have this working...
  42. # - Selection State should be tracked and handled on a per-object basis
  43. # so Mantis should update selection state one item at a time
  44. # - finally, I want to extend this selection sync to show active constraints and modifiers
  45. # and to link up with viewport gizmo tools.
  46. # - Serialize this so that it sticks around when saving/loading file
  47. # Eventually:
  48. # - sync scene/object selection to node graph. I have the structure in place
  49. # but I still have to write the selection tracking code.
  50. # Potential small BUG?
  51. # - I am not ensuring the base/root tree is the same for the space and the nodes
  52. # - because I straight up do not store that in the selection_sync dicts
  53. # - easy to fix, will do it when clearing TODO's here.
  54. def timer_update_selection_sync():
  55. try:
  56. if not ENABLED: return None
  57. root_tree = None
  58. tree_path = None
  59. parsed_tree = None
  60. spaces = get_visible_space_data()
  61. if not spaces:
  62. return SELECTION_POLL_RATE
  63. selection_state_start = cached_selection_state.copy()
  64. selection_state = cached_selection_state.copy()
  65. current_tree_selected_total = 0
  66. # we can't get UI context in this function so we have to checc all of them
  67. #maybe the tree itself can load the timer when it is opened in the node editor?
  68. for space in spaces:
  69. # if we are in the node editor, manage node selection
  70. if space.type == "NODE_EDITOR":
  71. if not space.path:
  72. return SELECTION_POLL_RATE # empty tree
  73. root_tree = space.path[0].node_tree
  74. if root_tree is None:
  75. return SELECTION_POLL_RATE
  76. if root_tree.bl_idname != 'MantisTree':
  77. return SELECTION_POLL_RATE
  78. tree_path = [path_item.node_tree for path_item in space.path]
  79. if len(tree_path[-1].nodes) == 0:
  80. return SELECTION_POLL_RATE # empty graph
  81. parsed_tree = root_tree.parsed_tree
  82. active_ui_signature = (get_signature_from_space(tree_path[-1].nodes.active, space))
  83. for n in tree_path[-1].nodes:
  84. if n.bl_idname not in WATCH_FOR_SELECTION: continue
  85. selection_state[ui_signature := get_signature_from_space(n, space)] = n.select
  86. current_tree_selected_total += int(n.select)
  87. if parsed_tree is None:
  88. return SELECTION_POLL_RATE
  89. cached_selection_state.update(selection_state)
  90. # We only fire the update if things have changed.
  91. if selection_state != selection_state_start:
  92. from bpy import context
  93. view_layer = context.view_layer
  94. deselect = not bool(current_tree_selected_total)
  95. try:
  96. update_scene_selection_from_node_graph_selection_sync(
  97. active_ui_signature, view_layer,
  98. selection_state, deselect)
  99. except Exception as e:
  100. prRed (e)
  101. return SELECTION_POLL_RATE
  102. except Exception as e:
  103. print ("Error in Selection Sync")
  104. print (e)
  105. return SELECTION_POLL_RATE
  106. def set_bone_selection(pose_bone, selection_state):
  107. # in some older blender versions, select was a bone property
  108. if hasattr(pose_bone, 'select'):
  109. pose_bone.select = selection_state
  110. elif hasattr(pose_bone.bone, 'select'):
  111. pose_bone.bone.select = selection_state
  112. def update_scene_selection_from_node_graph_selection_sync(
  113. active_ui_signature, view_layer,
  114. selection_state, deselect ):
  115. from .base_definitions import selection_sync_node_to_object
  116. # prWhite("Updating Scene Selection from Node Graph Selection")
  117. deselect_do_once = []
  118. for ui_signature, selected in selection_state.items():
  119. this_one_is_active = False
  120. if ui_signature == active_ui_signature: this_one_is_active = True
  121. for object_key in selection_sync_node_to_object.get(ui_signature, []):
  122. id_data = bpy.data.objects.get(object_key[0])
  123. if object_key[1]: # we have a sub_value, it's a bone
  124. if deselect:
  125. if object_key[0] not in deselect_do_once:
  126. for pb in id_data.pose.bones:
  127. set_bone_selection(pb, False)
  128. deselect_do_once.append(object_key[0])
  129. else:
  130. pb = id_data.pose.bones[object_key[1]]
  131. set_bone_selection(pb, selected)
  132. if this_one_is_active: # set the active bone
  133. id_data.data.bones.active = pb.bone
  134. # now do its parent object...
  135. if selected:
  136. id_data.select_set(True, view_layer=view_layer)
  137. if this_one_is_active:
  138. view_layer.objects.active = id_data
  139. else: # or the sub_value is None, this is an object
  140. ob = id_data
  141. if deselect: selected = False
  142. ob.select_set(selected, view_layer=view_layer)
  143. if this_one_is_active and not deselect:
  144. view_layer.objects.active = ob