timers.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. try:
  53. if not ENABLED: return None
  54. root_tree = None
  55. tree_path = None
  56. parsed_tree = None
  57. spaces = get_visible_space_data()
  58. if not spaces:
  59. return SELECTION_POLL_RATE
  60. selection_state_start = cached_selection_state.copy()
  61. selection_state = cached_selection_state.copy()
  62. current_tree_selected_total = 0
  63. # we can't get UI context in this function so we have to checc all of them
  64. #maybe the tree itself can load the timer when it is opened in the node editor?
  65. for space in spaces:
  66. # if we are in the node editor, manage node selection
  67. if space.type == "NODE_EDITOR":
  68. if not space.path:
  69. return SELECTION_POLL_RATE # empty tree
  70. root_tree = space.path[0].node_tree
  71. parsed_tree = root_tree.parsed_tree
  72. tree_path = [path_item.node_tree for path_item in space.path]
  73. if root_tree is None:
  74. return SELECTION_POLL_RATE
  75. if root_tree.bl_idname != 'MantisTree':
  76. return SELECTION_POLL_RATE
  77. active_ui_signature = (get_signature_from_space(tree_path[-1].nodes.active, space))
  78. for n in tree_path[-1].nodes:
  79. if n.bl_idname not in WATCH_FOR_SELECTION: continue
  80. selection_state[ui_signature := get_signature_from_space(n, space)] = n.select
  81. current_tree_selected_total += int(n.select)
  82. if parsed_tree is None:
  83. return SELECTION_POLL_RATE
  84. cached_selection_state.update(selection_state)
  85. # We only fire the update if things have changed.
  86. if selection_state != selection_state_start:
  87. from bpy import context
  88. view_layer = context.view_layer
  89. deselect = not bool(current_tree_selected_total)
  90. try:
  91. update_scene_selection_from_node_graph_selection_sync(
  92. active_ui_signature, view_layer,
  93. selection_state, deselect)
  94. except Exception as e:
  95. prRed (e)
  96. return SELECTION_POLL_RATE
  97. except Exception as e:
  98. print ("Error in Selection Sync")
  99. print (e)
  100. return SELECTION_POLL_RATE
  101. def set_bone_selection(pose_bone, selection_state):
  102. # in some older blender versions, select was a bone property
  103. if hasattr(pose_bone, 'select'):
  104. pose_bone.select = selection_state
  105. elif hasattr(pose_bone.bone, 'select'):
  106. pose_bone.bone.select = selection_state
  107. def update_scene_selection_from_node_graph_selection_sync(
  108. active_ui_signature, view_layer,
  109. selection_state, deselect ):
  110. from .base_definitions import selection_sync_node_to_object
  111. # prWhite("Updating Scene Selection from Node Graph Selection")
  112. deselect_do_once = []
  113. for ui_signature, selected in selection_state.items():
  114. this_one_is_active = False
  115. if ui_signature == active_ui_signature: this_one_is_active = True
  116. for object_key in selection_sync_node_to_object.get(ui_signature, []):
  117. id_data = bpy.data.objects.get(object_key[0])
  118. if object_key[1]: # we have a sub_value, it's a bone
  119. if deselect:
  120. if object_key[0] not in deselect_do_once:
  121. for pb in id_data.pose.bones:
  122. set_bone_selection(pb, False)
  123. deselect_do_once.append(object_key[0])
  124. else:
  125. pb = id_data.pose.bones[object_key[1]]
  126. set_bone_selection(pb, selected)
  127. if this_one_is_active: # set the active bone
  128. id_data.data.bones.active = pb.bone
  129. # now do its parent object...
  130. if selected:
  131. id_data.select_set(True, view_layer=view_layer)
  132. if this_one_is_active:
  133. view_layer.objects.active = id_data
  134. else: # or the sub_value is None, this is an object
  135. ob = id_data
  136. if deselect: selected = False
  137. ob.select_set(selected, view_layer=view_layer)
  138. if this_one_is_active and not deselect:
  139. view_layer.objects.active = ob