__init__.py 10 KB

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