__init__.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. from . import ( ops_nodegroup,
  2. ops_ui,
  3. base_definitions,
  4. socket_definitions,
  5. link_nodes_ui,
  6. xForm_nodes_ui,
  7. misc_nodes_ui,
  8. primitives_nodes_ui,
  9. deformer_nodes_ui,
  10. math_nodes_ui,
  11. i_o,
  12. schema_nodes_ui,
  13. menu_classes,
  14. )
  15. from .ops_generate_tree import GenerateMantisTree
  16. from .utilities import prRed
  17. from .base_definitions import (MANTIS_VERSION_MAJOR,
  18. MANTIS_VERSION_MINOR,
  19. MANTIS_VERSION_SUB)
  20. classLists = [module.TellClasses() for module in [
  21. link_nodes_ui,
  22. xForm_nodes_ui,
  23. misc_nodes_ui,
  24. socket_definitions,
  25. ops_nodegroup,
  26. ops_ui,
  27. primitives_nodes_ui,
  28. deformer_nodes_ui,
  29. math_nodes_ui,
  30. i_o,
  31. schema_nodes_ui,
  32. base_definitions,
  33. menu_classes,
  34. ]]
  35. classLists.append( [GenerateMantisTree] )
  36. #
  37. classes = []
  38. while (classLists):
  39. classes.extend(classLists.pop())
  40. interface_classes = []
  41. from .preferences import MantisPreferences
  42. classes.append(MantisPreferences)
  43. from os import environ
  44. if environ.get("ENABLEVIS"):
  45. from .visualize import MantisVisualizeNode, MantisVisualizeOutput, MantisVisualizeTree
  46. classes.extend([MantisVisualizeTree, MantisVisualizeNode, MantisVisualizeOutput, ])
  47. import nodeitems_utils
  48. from nodeitems_utils import NodeCategory, NodeItem
  49. class MantisNodeCategory(NodeCategory):
  50. @classmethod
  51. def poll(cls, context):
  52. return (context.space_data.tree_type in ['MantisTree', 'SchemaTree'])
  53. class SchemaNodeCategory(NodeCategory):
  54. @classmethod
  55. def poll(cls, context):
  56. return (context.space_data.path[len(context.space_data.path)-1].node_tree.bl_idname == 'SchemaTree')
  57. class MantisGroupCategory(NodeCategory):
  58. @classmethod
  59. def poll(cls, context):
  60. return (context.space_data.path[len(context.space_data.path)-1].node_tree.bl_idname in ['MantisTree'] and len(context.space_data.path)>1)
  61. input_category=[
  62. NodeItem("InputFloatNode"),
  63. NodeItem("InputVectorNode"),
  64. NodeItem("InputBooleanNode"),
  65. NodeItem("InputBooleanThreeTupleNode"),
  66. NodeItem("InputStringNode"),
  67. NodeItem("InputIntNode"),
  68. NodeItem("InputMatrixNode"),
  69. NodeItem("InputWidget"),
  70. NodeItem("InputExistingGeometryObject"),
  71. NodeItem("xFormGetBone"),
  72. NodeItem("InputExistingGeometryData"),
  73. NodeItem("UtilityDeclareCollections"),
  74. NodeItem("InputThemeBoneColorSets"),
  75. NodeItem("InputColorSetPallete"),
  76. ]
  77. link_transform_category = [
  78. NodeItem("LinkCopyLocation"),
  79. NodeItem("LinkCopyRotation"),
  80. NodeItem("LinkCopyScale"),
  81. NodeItem("LinkCopyTransforms"),
  82. NodeItem("LinkLimitLocation"),
  83. NodeItem("LinkLimitScale"),
  84. NodeItem("LinkLimitRotation"),
  85. NodeItem("LinkLimitDistance"),
  86. NodeItem("LinkTransformation"),
  87. NodeItem("LinkGeometryAttribute"),
  88. NodeItem("LinkFloor"),
  89. ]
  90. link_tracking_category = [
  91. NodeItem("LinkInverseKinematics"),
  92. NodeItem("LinkSplineIK"),
  93. NodeItem("LinkStretchTo"),
  94. NodeItem("LinkDampedTrack"),
  95. NodeItem("LinkLockedTrack"),
  96. NodeItem("LinkTrackTo"),
  97. NodeItem("LinkShrinkWrap"),
  98. ]
  99. link_relationship_category = [
  100. NodeItem("linkInherit"),
  101. NodeItem("LinkInheritConstraint"),
  102. NodeItem("LinkArmature"),
  103. ]
  104. deformer_category=[NodeItem(cls.bl_idname) for cls in deformer_nodes_ui.TellClasses()]
  105. xForm_category = [
  106. NodeItem("xFormGeometryObject"),
  107. NodeItem("xFormBoneNode"),
  108. NodeItem("xFormArmatureNode"),
  109. NodeItem("xFormObjectInstance"),
  110. NodeItem("xFormCurvePin"),
  111. ]
  112. driver_category = [
  113. NodeItem("UtilityCustomProperty"),
  114. NodeItem("LinkDrivenParameter"),
  115. NodeItem("UtilityFCurve"),
  116. NodeItem("UtilityBoneProperties"),
  117. NodeItem("UtilityDriverVariable"),
  118. NodeItem("UtilitySwitch"),
  119. NodeItem("UtilityDriver"),
  120. NodeItem("UtilityKeyframe"),
  121. ]
  122. geometry_category = [
  123. NodeItem("GeometryCirclePrimitive"),
  124. NodeItem("GeometryLattice"),
  125. ]
  126. utility_category = [
  127. NodeItem("MathStaticInt"),
  128. NodeItem("MathStaticFloat"),
  129. NodeItem("MathStaticVector"),
  130. NodeItem("UtilityCatStrings"),
  131. NodeItem("UtilityCollectionJoin"),
  132. NodeItem("UtilityCollectionHierarchy"),
  133. NodeItem("UtilityGeometryOfXForm"),
  134. NodeItem("UtilityNameOfXForm"),
  135. NodeItem("UtilityCombineThreeBool"),
  136. NodeItem("UtilityCombineVector"),
  137. NodeItem("UtilityIntToString"),
  138. NodeItem("UtilityArrayGet"),
  139. NodeItem("UtilityArrayLength"),
  140. NodeItem("UtilityChoose"),
  141. NodeItem("UtilityCompare"),
  142. NodeItem("UtilityPrint"),
  143. NodeItem("UtilitySeparateVector"),
  144. NodeItem("UtilityGetNearestFactorOnCurve"),
  145. NodeItem("UtilityKDChoosePoint"),
  146. NodeItem("UtilityKDChooseXForm"),
  147. ]
  148. matrix_category = [
  149. NodeItem("UtilityMetaRig"),
  150. NodeItem("UtilityMatrixFromCurve"),
  151. NodeItem("UtilityMatricesFromCurve"),
  152. NodeItem("UtilityNumberOfCurveSegments"),
  153. NodeItem("UtilityMatrixFromCurveSegment"),
  154. NodeItem("UtilityPointFromCurve"),
  155. NodeItem("UtilityGetCurvePoint"),
  156. NodeItem("UtilityNumberOfSplines"),
  157. NodeItem("UtilityPointFromBoneMatrix"),
  158. NodeItem("UtilitySetBoneLength"),
  159. NodeItem("UtilityGetBoneLength"),
  160. NodeItem("UtilityBoneMatrixHeadTailFlip"),
  161. NodeItem("UtilityMatrixSetLocation"),
  162. NodeItem("UtilityMatrixFromXForm"),
  163. NodeItem("UtilityAxesFromMatrix"),
  164. NodeItem("UtilityMatrixTransform"),
  165. NodeItem("UtilityMatrixInvert"),
  166. NodeItem("UtilityMatrixCompose"),
  167. NodeItem("UtilityMatrixAlignRoll"),
  168. NodeItem("UtilityTransformationMatrix"),
  169. NodeItem("UtilitySetBoneMatrixTail"),
  170. ]
  171. groups_category = [
  172. NodeItem("MantisNodeGroup"),
  173. NodeItem("MantisSchemaGroup"),
  174. ]
  175. group_interface_category = [
  176. NodeItem("NodeGroupInput"),
  177. NodeItem("NodeGroupOutput"),
  178. ]
  179. node_categories = [
  180. # identifier, label, items list
  181. MantisNodeCategory('INPUT', "Input", items=input_category),
  182. MantisNodeCategory('LINK_TRANSFORM', "Link (Transform)", items=link_transform_category),
  183. MantisNodeCategory('LINK_TRACKING', "Link (Tracking)", items=link_tracking_category),
  184. MantisNodeCategory('LINK_RELATIONSHIP', "Link (Inheritance)", items=link_relationship_category),
  185. MantisNodeCategory('DEFORMER', "Deformer", items=deformer_category),
  186. MantisNodeCategory('XFORM', "Transform", items=xForm_category),
  187. MantisNodeCategory('DRIVER', "Driver", items=driver_category),
  188. MantisNodeCategory('GEOMETRY', "Geometry", items =geometry_category),
  189. MantisNodeCategory('UTILITIES', "Utility", items=utility_category),
  190. MantisNodeCategory('MATRIX', "Matrix", items=matrix_category),
  191. MantisNodeCategory('GROUPS', "Groups", items=groups_category),
  192. MantisGroupCategory('GROUP_INTERFACE', "Group In/Out", items=group_interface_category),
  193. ]
  194. schema_category=[NodeItem(cls.bl_idname) for cls in schema_nodes_ui.TellClasses()]
  195. schema_categories = [
  196. SchemaNodeCategory('SCHEMA_SCHEMA', "Schema", items=schema_category),
  197. ]
  198. import bpy
  199. def init_keymaps():
  200. kc = bpy.context.window_manager.keyconfigs.addon
  201. km = kc.keymaps.new(name="Node Generic", space_type='NODE_EDITOR')
  202. kmi = [
  203. # Normal operation
  204. km.keymap_items.new("mantis.group_nodes", 'G', 'PRESS', ctrl=True),
  205. km.keymap_items.new("mantis.edit_group", 'TAB', 'PRESS'),
  206. km.keymap_items.new("mantis.execute_node_tree", 'E', 'PRESS'),
  207. km.keymap_items.new("mantis.mute_node", 'M', 'PRESS'),
  208. km.keymap_items.new("mantis.nodes_cleanup", "C", 'PRESS', shift=True,),
  209. # Testing
  210. km.keymap_items.new("mantis.query_sockets", 'Q', 'PRESS'),
  211. km.keymap_items.new("mantis.test_operator", 'T', 'PRESS'),
  212. km.keymap_items.new("mantis.visualize_output", 'V', 'PRESS'),
  213. # Saving, Loading, Reloading, etc.
  214. km.keymap_items.new("mantis.export_save_choose", "S", 'PRESS', alt=True,),
  215. km.keymap_items.new("mantis.export_save_as", "S", 'PRESS', alt=True, shift=True),
  216. km.keymap_items.new("mantis.reload_tree", "R", 'PRESS', alt=True,),
  217. km.keymap_items.new("mantis.import_tree", "O", 'PRESS', ctrl=True,),
  218. ]
  219. return km, kmi
  220. addon_keymaps = []
  221. # handlers! these have to be persistent
  222. from bpy.app.handlers import persistent
  223. from .base_definitions import hash_tree
  224. @persistent
  225. def update_handler(scene):
  226. # return
  227. context=bpy.context
  228. if context.space_data:
  229. if not hasattr(context.space_data, "path"):
  230. return
  231. trees = [p.node_tree for p in context.space_data.path]
  232. if not trees: return
  233. if (node_tree := trees[0]).bl_idname in ['MantisTree']:
  234. if node_tree.is_exporting:
  235. return
  236. if node_tree.prevent_next_exec : pass
  237. elif node_tree.do_live_update and not (node_tree.is_executing):
  238. node_tree.update_tree(context)
  239. @persistent
  240. def execute_handler(scene):
  241. context = bpy.context
  242. if context.space_data:
  243. if not hasattr(context.space_data, "path"):
  244. return
  245. trees = [p.node_tree for p in context.space_data.path]
  246. if not trees: return
  247. if (node_tree := trees[0]).bl_idname in ['MantisTree']:
  248. # check here instead of in execute_tree because these values can be
  249. # modified at weird times and checking from the handler is more consistent
  250. if ( node_tree.tree_valid) and ( node_tree.do_live_update ):
  251. node_tree.execute_tree(context)
  252. node_tree.tree_valid=False
  253. from .versioning import versioning_tasks
  254. def node_version_update(node, do_once):
  255. from .xForm_nodes_ui import xFormNode
  256. for bl_idname, task, required_kwargs in versioning_tasks:
  257. arg_map = {}
  258. if 'node' in required_kwargs:
  259. arg_map['node']=node
  260. if 'node_tree' in required_kwargs:
  261. arg_map['node_tree']=node.id_data
  262. # we'll match all, or categories, or specific bl_idname as needed.
  263. if ('ALL' in bl_idname) or \
  264. ('XFORM' in bl_idname) and isinstance(node, xFormNode) or\
  265. node.bl_idname in bl_idname:
  266. if do_once:
  267. print (f"Updating tree {node.id_data.name} to "
  268. f"{MANTIS_VERSION_MAJOR}.{MANTIS_VERSION_MINOR}.{MANTIS_VERSION_SUB}")
  269. task(**arg_map)
  270. def do_version_update(node_tree):
  271. # set updating status for dynamic nodes to prevent bugs in socket remapping
  272. do_once = True
  273. for node in node_tree.nodes:
  274. if hasattr(node, 'is_updating'):
  275. node.is_updating = True
  276. # start by doing tree versioning tasks
  277. for affected_bl_idnames, task, arguments_needed in versioning_tasks:
  278. if node_tree.bl_idname not in affected_bl_idnames: continue # this is a node task.
  279. arguments = {}
  280. if 'tree' in arguments_needed:
  281. arguments['tree']=node_tree
  282. task(**arguments)
  283. # run the updates that have no prerequisites
  284. for node in node_tree.nodes:
  285. node_version_update(node, do_once)
  286. do_once = False
  287. # NOTE: if future versoning tasks have prerequisites, resolve them here and update again
  288. # reset the updating status for dynamic nodes
  289. for node in node_tree.nodes:
  290. if hasattr(node, 'is_updating'):
  291. node.is_updating = False
  292. # increment the version at the end
  293. node_tree.mantis_version[0] = MANTIS_VERSION_MAJOR
  294. node_tree.mantis_version[1] = MANTIS_VERSION_MINOR
  295. node_tree.mantis_version[2] = MANTIS_VERSION_SUB
  296. @persistent
  297. def version_update_handler(filename):
  298. for node_tree in bpy.data.node_groups: # ensure it can update again after file load.
  299. if node_tree.bl_idname in ["MantisTree", "SchemaTree"]:
  300. node_tree.is_exporting=False; node_tree.is_executing=False
  301. for node_tree in bpy.data.node_groups:
  302. if node_tree.bl_idname in ["MantisTree", "SchemaTree"]:
  303. if (node_tree.mantis_version[0] < MANTIS_VERSION_MAJOR) or \
  304. (node_tree.mantis_version[1] < MANTIS_VERSION_MINOR) or \
  305. (node_tree.mantis_version[2] < MANTIS_VERSION_SUB):
  306. do_version_update(node_tree)
  307. @persistent
  308. def autoload_components(filename):
  309. # this should not be blocking or slow!
  310. print("Auto-loading components")
  311. from os import path as os_path
  312. from .utilities import get_component_library_items, get_default_collection
  313. from .i_o import do_import
  314. from .preferences import get_bl_addon_object
  315. import json
  316. from bpy import context, data
  317. bl_addon_object = get_bl_addon_object()
  318. unlink_curves = True
  319. unlink_armatures = True
  320. if data.collections.get(bl_addon_object.preferences.CurveDefaultCollection):
  321. unlink_curves=False
  322. if data.collections.get(bl_addon_object.preferences.MetaArmatureDefaultCollection):
  323. unlink_armatures=False
  324. base_path = bl_addon_object.preferences.ComponentsAutoLoadFolder
  325. components = get_component_library_items(path='AUTOLOAD')
  326. for autoload_component in components:
  327. path = os_path.join(base_path, autoload_component[0])
  328. with open(path, 'r', encoding='utf-8') as f:
  329. json_data = json.load(f)
  330. do_import(json_data, context,
  331. search_multi_files=True, filepath=path,
  332. skip_existing=True)
  333. # let's get the node trees and assign a fake user
  334. for tree_name in json_data.keys():
  335. tree = data.node_groups.get(tree_name)
  336. tree.use_fake_user = True
  337. # now we need to unlink the collections, and add fake users to them
  338. curves_collection = get_default_collection(collection_type="CURVE")
  339. armature_collection = get_default_collection(collection_type="ARMATURE")
  340. if unlink_curves and (curves_collection := data.collections.get(
  341. bl_addon_object.preferences.CurveDefaultCollection)):
  342. context.scene.collection.children.unlink(curves_collection)
  343. if unlink_armatures and (armature_collection := data.collections.get(
  344. bl_addon_object.preferences.MetaArmatureDefaultCollection)):
  345. context.scene.collection.children.unlink(armature_collection)
  346. # I'll need to do some fiddling here when it comes time to try
  347. # and make rig definitions animatable.
  348. @persistent
  349. def on_animation_playback_pre_handler(scene,depsgraph):
  350. for t in bpy.data.node_groups:
  351. if t.bl_idname in ['MantisTree', 'SchemaTree']:
  352. t.is_executing = True
  353. @persistent
  354. def on_animation_playback_post_handler(scene,depsgraph):
  355. for t in bpy.data.node_groups:
  356. if t.bl_idname in ['MantisTree', 'SchemaTree']:
  357. t.is_executing = False
  358. @persistent
  359. def on_undo_post_handler(scene): # the undo will trigger a depsgraph update
  360. for t in bpy.data.node_groups: # so we enable prevent_next_exec.
  361. if t.bl_idname in ['MantisTree', 'SchemaTree']:
  362. t.prevent_next_exec = True
  363. t.hash=""
  364. # set the tree to invalid to trigger a tree update
  365. # since the context data is wiped by an undo.
  366. from .menu_classes import (node_context_menu_draw, node_add_menu_draw,
  367. armature_add_menu_draw, import_menu_draw)
  368. from .socket_definitions import generate_custom_interface_types
  369. generated_classes = generate_custom_interface_types()
  370. classes.extend(generated_classes)
  371. def register():
  372. from bpy.utils import register_class
  373. for cls in classes:
  374. try:
  375. register_class(cls)
  376. except RuntimeError as e:
  377. prRed(f"Registration error for class: {cls.__name__}")
  378. raise e
  379. nodeitems_utils.register_node_categories('MantisNodeCategories', node_categories)
  380. nodeitems_utils.register_node_categories('SchemaNodeCategories', schema_categories)
  381. bpy.types.NODE_MT_context_menu.append(node_context_menu_draw)
  382. bpy.types.NODE_MT_add.append(node_add_menu_draw)
  383. bpy.types.VIEW3D_MT_armature_add.append(armature_add_menu_draw)
  384. bpy.types.TOPBAR_MT_file_import.append(import_menu_draw)
  385. km, kmi = init_keymaps()
  386. for k in kmi:
  387. k.active = True
  388. addon_keymaps.append((km, k))
  389. # add the handlers
  390. bpy.app.handlers.depsgraph_update_pre.insert(0, update_handler)
  391. bpy.app.handlers.depsgraph_update_post.insert(0, execute_handler)
  392. bpy.app.handlers.load_post.insert(0, autoload_components)
  393. bpy.app.handlers.load_post.insert(0, version_update_handler)
  394. bpy.app.handlers.animation_playback_pre.insert(0, on_animation_playback_pre_handler)
  395. bpy.app.handlers.animation_playback_post.insert(0, on_animation_playback_post_handler)
  396. bpy.app.handlers.undo_post.insert(0, on_undo_post_handler)
  397. # I'm adding mine in first to ensure other addons don't mess up mine
  398. # but I am a good citizen! so my addon won't mess up yours! probably...
  399. def unregister():
  400. bpy.types.NODE_MT_context_menu.remove(node_context_menu_draw)
  401. for tree in bpy.data.node_groups: # ensure it doesn't try to update while quitting.
  402. if tree.bl_idname in ['MantisTree, SchemaTree']:
  403. tree.is_exporting=True; tree.is_executing=True
  404. nodeitems_utils.unregister_node_categories('MantisNodeCategories')
  405. nodeitems_utils.unregister_node_categories('SchemaNodeCategories')
  406. from bpy.utils import unregister_class
  407. for cls in reversed(classes):
  408. unregister_class(cls)
  409. for km, kmi in addon_keymaps:
  410. km.keymap_items.remove(kmi)
  411. addon_keymaps.clear()