__init__.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from . import ( ops_nodegroup,
  2. base_definitions,
  3. socket_definitions,
  4. link_definitions,
  5. xForm_definitions,
  6. nodes_generic,
  7. primitives_definitions,
  8. deformer_definitions,
  9. math_definitions,
  10. i_o,
  11. schema_definitions,
  12. )
  13. from .ops_generate_tree import GenerateMantisTree
  14. from bpy.types import NodeSocket
  15. from .utilities import prRed
  16. classLists = [module.TellClasses() for module in [
  17. link_definitions,
  18. xForm_definitions,
  19. base_definitions,
  20. nodes_generic,
  21. socket_definitions,
  22. ops_nodegroup,
  23. primitives_definitions,
  24. deformer_definitions,
  25. math_definitions,
  26. i_o,
  27. schema_definitions,
  28. ]]
  29. classLists.append( [GenerateMantisTree] )
  30. #
  31. classes = []
  32. while (classLists):
  33. classes.extend(classLists.pop())
  34. interface_classes = []
  35. from bpy import app
  36. import nodeitems_utils
  37. from nodeitems_utils import NodeCategory, NodeItem
  38. class MantisNodeCategory(NodeCategory):
  39. @classmethod
  40. def poll(cls, context):
  41. return (context.space_data.tree_type in ['MantisTree', 'SchemaTree'])
  42. class SchemaNodeCategory(NodeCategory):
  43. @classmethod
  44. def poll(cls, context):
  45. return (context.space_data.path[len(context.space_data.path)-1].node_tree.bl_idname == 'SchemaTree')
  46. input_category=[
  47. NodeItem("InputFloatNode"),
  48. NodeItem("InputVectorNode"),
  49. NodeItem("InputBooleanNode"),
  50. NodeItem("InputStringNode"),
  51. NodeItem("InputIntNode"),
  52. NodeItem("InputMatrixNode"),
  53. NodeItem("InputExistingGeometryObject"),
  54. NodeItem("InputExistingGeometryData"),
  55. ]
  56. link_transform_category = [
  57. NodeItem("LinkCopyLocation"),
  58. NodeItem("LinkCopyRotation"),
  59. NodeItem("LinkCopyScale"),
  60. NodeItem("LinkCopyTransforms"),
  61. NodeItem("LinkLimitLocation"),
  62. NodeItem("LinkLimitScale"),
  63. NodeItem("LinkLimitRotation"),
  64. NodeItem("LinkLimitDistance"),
  65. NodeItem("LinkTransformation"),
  66. ]
  67. link_tracking_category = [
  68. NodeItem("LinkInverseKinematics"),
  69. NodeItem("LinkSplineIK"),
  70. NodeItem("LinkStretchTo"),
  71. NodeItem("LinkDampedTrack"),
  72. NodeItem("LinkLockedTrack"),
  73. NodeItem("LinkTrackTo"),
  74. ]
  75. link_relationship_category = [
  76. NodeItem("linkInherit"),
  77. NodeItem("LinkInheritConstraint"),
  78. NodeItem("LinkArmature"),
  79. ]
  80. deformer_category=[NodeItem(cls.bl_idname) for cls in deformer_definitions.TellClasses()]
  81. xForm_category = [
  82. NodeItem("xFormGeometryObject"),
  83. NodeItem("xFormBoneNode"),
  84. NodeItem("xFormArmatureNode"),
  85. ]
  86. driver_category = [
  87. NodeItem("LinkDrivenParameter"),
  88. NodeItem("UtilityFCurve"),
  89. NodeItem("UtilityBoneProperties"),
  90. NodeItem("UtilityDriverVariable"),
  91. NodeItem("UtilitySwitch"),
  92. NodeItem("UtilityDriver"),
  93. NodeItem("UtilityKeyframe"),
  94. ]
  95. geometry_category = [
  96. NodeItem("GeometryCirclePrimitive"),
  97. ]
  98. utility_category = [
  99. NodeItem("MathStaticInt"),
  100. NodeItem("MathStaticFloat"),
  101. NodeItem("MathStaticVector"),
  102. NodeItem("UtilityCatStrings"),
  103. NodeItem("UtilityCombineThreeBool"),
  104. NodeItem("UtilityCombineVector"),
  105. NodeItem("UtilityIntToString"),
  106. NodeItem("UtilityArrayGet"),
  107. NodeItem("UtilityChoose"),
  108. NodeItem("UtilityCompare"),
  109. NodeItem("UtilityPrint"),
  110. ]
  111. matrix_category = [
  112. NodeItem("UtilityMetaRig"),
  113. NodeItem("UtilityMatrixFromCurve"),
  114. NodeItem("UtilityMatricesFromCurve"),
  115. NodeItem("UtilityPointFromCurve"),
  116. NodeItem("UtilityPointFromBoneMatrix"),
  117. NodeItem("UtilitySetBoneLength"),
  118. NodeItem("UtilityGetBoneLength"),
  119. NodeItem("UtilityBoneMatrixHeadTailFlip"),
  120. NodeItem("UtilityMatrixSetLocation"),
  121. NodeItem("UtilityMatrixFromXForm"),
  122. NodeItem("UtilityAxesFromMatrix"),
  123. NodeItem("UtilityMatrixTransform"),
  124. NodeItem("UtilityTransformationMatrix"),
  125. NodeItem("UtilitySetBoneMatrixTail"),
  126. ]
  127. groups_category = [
  128. NodeItem("MantisNodeGroup"),
  129. NodeItem("MantisSchemaGroup"),
  130. ]
  131. node_categories = [
  132. # identifier, label, items list
  133. MantisNodeCategory('INPUT', "Input", items=input_category),
  134. MantisNodeCategory('LINK_TRANSFORM', "Link (Transform)", items=link_transform_category),
  135. MantisNodeCategory('LINK_TRACKING', "Link (Tracking)", items=link_tracking_category),
  136. MantisNodeCategory('LINK_RELATIONSHIP', "Link (Inheritance)", items=link_relationship_category),
  137. MantisNodeCategory('DEFORMER', "Deformer", items=deformer_category),
  138. MantisNodeCategory('XFORM', "Transform", items=xForm_category),
  139. MantisNodeCategory('DRIVER', "Driver", items=driver_category),
  140. MantisNodeCategory('GEOMETRY', "Geometry", items =geometry_category),
  141. MantisNodeCategory('UTILITIES', "Utility", items=utility_category),
  142. MantisNodeCategory('MATRIX', "Matrix", items=matrix_category),
  143. MantisNodeCategory('GROUPS', "Groups", items=groups_category),
  144. ]
  145. schema_category=[NodeItem(cls.bl_idname) for cls in schema_definitions.TellClasses()]
  146. schema_categories = [
  147. SchemaNodeCategory('SCHEMA_SCHEMA', "Schema", items=schema_category),
  148. ]
  149. import bpy
  150. def init_keymaps():
  151. kc = bpy.context.window_manager.keyconfigs.addon
  152. km = kc.keymaps.new(name="Node Generic", space_type='NODE_EDITOR')
  153. kmi = [
  154. # Normal operation
  155. km.keymap_items.new("mantis.group_nodes", 'G', 'PRESS', ctrl=True),
  156. km.keymap_items.new("mantis.edit_group", 'TAB', 'PRESS'),
  157. km.keymap_items.new("mantis.execute_node_tree", 'E', 'PRESS'),
  158. km.keymap_items.new("mantis.mute_node", 'M', 'PRESS'),
  159. km.keymap_items.new("mantis.nodes_cleanup", "C", 'PRESS', shift=True,),
  160. # Testing
  161. km.keymap_items.new("mantis.query_sockets", 'Q', 'PRESS'),
  162. km.keymap_items.new("mantis.test_operator", 'T', 'PRESS'),
  163. km.keymap_items.new("mantis.visualize_output", 'V', 'PRESS'),
  164. # Saving, Loading, Reloading, etc.
  165. km.keymap_items.new("mantis.export_save_choose", "S", 'PRESS', alt=True,),
  166. km.keymap_items.new("mantis.export_save_as", "S", 'PRESS', alt=True, shift=True),
  167. km.keymap_items.new("mantis.reload_tree", "R", 'PRESS', alt=True,),
  168. km.keymap_items.new("mantis.import_tree", "O", 'PRESS', ctrl=True,),
  169. ]
  170. return km, kmi
  171. addon_keymaps = []
  172. # handlers! these have to be persistent
  173. from bpy.app.handlers import persistent
  174. @persistent
  175. def update_handler(scene):
  176. context=bpy.context
  177. if context.space_data:
  178. if not hasattr(context.space_data, "path"):
  179. return
  180. trees = [p.node_tree for p in context.space_data.path]
  181. if not trees: return
  182. if (node_tree := trees[0]).bl_idname in ['MantisTree']:
  183. if node_tree.do_live_update and not (node_tree.is_executing or node_tree.is_exporting):
  184. prev_links = node_tree.num_links
  185. node_tree.num_links = len(node_tree.links)
  186. if (prev_links == -1):
  187. return
  188. if prev_links != node_tree.num_links:
  189. node_tree.tree_valid = False
  190. if node_tree.tree_valid == False:
  191. scene.render.use_lock_interface = True
  192. node_tree.update_tree(context)
  193. scene.render.use_lock_interface = False
  194. @persistent
  195. def execute_handler(scene):
  196. context = bpy.context
  197. if context.space_data:
  198. if not hasattr(context.space_data, "path"):
  199. return
  200. trees = [p.node_tree for p in context.space_data.path]
  201. if not trees: return
  202. if (node_tree := trees[0]).bl_idname in ['MantisTree']:
  203. if node_tree.tree_valid and node_tree.do_live_update and not (node_tree.is_executing or node_tree.is_exporting):
  204. scene.render.use_lock_interface = True
  205. node_tree.execute_tree(context)
  206. scene.render.use_lock_interface = False
  207. node_tree.tree_valid = False
  208. def register():
  209. if bpy.app.version >= (4, 4):
  210. raise NotImplementedError("Blender 4.4 is not supported at this time.")
  211. from bpy.utils import register_class
  212. for cls in classes:
  213. try:
  214. register_class(cls)
  215. except RuntimeError as e:
  216. prRed(cls.__name__)
  217. raise e
  218. nodeitems_utils.register_node_categories('MantisNodeCategories', node_categories)
  219. nodeitems_utils.register_node_categories('SchemaNodeCategories', schema_categories)
  220. km, kmi = init_keymaps()
  221. for k in kmi:
  222. k.active = True
  223. addon_keymaps.append((km, k))
  224. # add the handlers
  225. bpy.app.handlers.depsgraph_update_pre.append(update_handler)
  226. bpy.app.handlers.depsgraph_update_post.append(execute_handler)
  227. def unregister():
  228. nodeitems_utils.unregister_node_categories('MantisNodeCategories')
  229. nodeitems_utils.unregister_node_categories('SchemaNodeCategories')
  230. from bpy.utils import unregister_class
  231. for cls in reversed(classes):
  232. unregister_class(cls)
  233. for km, kmi in addon_keymaps:
  234. km.keymap_items.remove(kmi)
  235. addon_keymaps.clear()