__init__.py 12 KB

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