__init__.py 9.6 KB

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