| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- from . import ( ops_nodegroup,
- base_definitions,
- socket_definitions,
- link_definitions,
- xForm_definitions,
- nodes_generic,
- primitives_definitions,
- deformer_definitions,
- math_definitions,
- i_o,
- schema_definitions,
- )
- from .ops_generate_tree import GenerateMantisTree
- from .utilities import prRed
- MANTIS_VERSION_MAJOR=0
- MANTIS_VERSION_MINOR=9
- MANTIS_VERSION_SUB=15
- classLists = [module.TellClasses() for module in [
- link_definitions,
- xForm_definitions,
- base_definitions,
- nodes_generic,
- socket_definitions,
- ops_nodegroup,
- primitives_definitions,
- deformer_definitions,
- math_definitions,
- i_o,
- schema_definitions,
- ]]
- classLists.append( [GenerateMantisTree] )
- #
- classes = []
- while (classLists):
- classes.extend(classLists.pop())
- interface_classes = []
- import nodeitems_utils
- from nodeitems_utils import NodeCategory, NodeItem
- class MantisNodeCategory(NodeCategory):
- @classmethod
- def poll(cls, context):
- return (context.space_data.tree_type in ['MantisTree', 'SchemaTree'])
- class SchemaNodeCategory(NodeCategory):
- @classmethod
- def poll(cls, context):
- return (context.space_data.path[len(context.space_data.path)-1].node_tree.bl_idname == 'SchemaTree')
- class MantisGroupCategory(NodeCategory):
- @classmethod
- def poll(cls, context):
- return (context.space_data.path[len(context.space_data.path)-1].node_tree.bl_idname in ['MantisTree'] and len(context.space_data.path)>1)
- input_category=[
- NodeItem("InputFloatNode"),
- NodeItem("InputVectorNode"),
- NodeItem("InputBooleanNode"),
- NodeItem("InputStringNode"),
- NodeItem("InputIntNode"),
- NodeItem("InputMatrixNode"),
- NodeItem("InputExistingGeometryObject"),
- NodeItem("InputExistingGeometryData"),
- ]
- link_transform_category = [
- NodeItem("LinkCopyLocation"),
- NodeItem("LinkCopyRotation"),
- NodeItem("LinkCopyScale"),
- NodeItem("LinkCopyTransforms"),
- NodeItem("LinkLimitLocation"),
- NodeItem("LinkLimitScale"),
- NodeItem("LinkLimitRotation"),
- NodeItem("LinkLimitDistance"),
- NodeItem("LinkTransformation"),
- ]
- link_tracking_category = [
- NodeItem("LinkInverseKinematics"),
- NodeItem("LinkSplineIK"),
- NodeItem("LinkStretchTo"),
- NodeItem("LinkDampedTrack"),
- NodeItem("LinkLockedTrack"),
- NodeItem("LinkTrackTo"),
- ]
- link_relationship_category = [
- NodeItem("linkInherit"),
- NodeItem("LinkInheritConstraint"),
- NodeItem("LinkArmature"),
- ]
- deformer_category=[NodeItem(cls.bl_idname) for cls in deformer_definitions.TellClasses()]
- xForm_category = [
- NodeItem("xFormGeometryObject"),
- NodeItem("xFormBoneNode"),
- NodeItem("xFormArmatureNode"),
- NodeItem("xFormObjectInstance"),
- ]
- driver_category = [
- NodeItem("LinkDrivenParameter"),
- NodeItem("UtilityFCurve"),
- NodeItem("UtilityBoneProperties"),
- NodeItem("UtilityDriverVariable"),
- NodeItem("UtilitySwitch"),
- NodeItem("UtilityDriver"),
- NodeItem("UtilityKeyframe"),
- ]
- geometry_category = [
- NodeItem("GeometryCirclePrimitive"),
- ]
- utility_category = [
- NodeItem("MathStaticInt"),
- NodeItem("MathStaticFloat"),
- NodeItem("MathStaticVector"),
- NodeItem("UtilityCatStrings"),
- NodeItem("UtilityGeometryOfXForm"),
- NodeItem("UtilityNameOfXForm"),
- NodeItem("UtilityCombineThreeBool"),
- NodeItem("UtilityCombineVector"),
- NodeItem("UtilityIntToString"),
- NodeItem("UtilityArrayGet"),
- NodeItem("UtilityChoose"),
- NodeItem("UtilityCompare"),
- NodeItem("UtilityPrint"),
- NodeItem("UtilitySeparateVector"),
- ]
- matrix_category = [
- NodeItem("UtilityMetaRig"),
- NodeItem("UtilityMatrixFromCurve"),
- NodeItem("UtilityMatricesFromCurve"),
- NodeItem("UtilityPointFromCurve"),
- NodeItem("UtilityPointFromBoneMatrix"),
- NodeItem("UtilitySetBoneLength"),
- NodeItem("UtilityGetBoneLength"),
- NodeItem("UtilityBoneMatrixHeadTailFlip"),
- NodeItem("UtilityMatrixSetLocation"),
- NodeItem("UtilityMatrixFromXForm"),
- NodeItem("UtilityAxesFromMatrix"),
- NodeItem("UtilityMatrixTransform"),
- NodeItem("UtilityTransformationMatrix"),
- NodeItem("UtilitySetBoneMatrixTail"),
- ]
- groups_category = [
- NodeItem("MantisNodeGroup"),
- NodeItem("MantisSchemaGroup"),
- ]
- group_interface_category = [
- NodeItem("NodeGroupInput"),
- NodeItem("NodeGroupOutput"),
- ]
- node_categories = [
- # identifier, label, items list
- MantisNodeCategory('INPUT', "Input", items=input_category),
- MantisNodeCategory('LINK_TRANSFORM', "Link (Transform)", items=link_transform_category),
- MantisNodeCategory('LINK_TRACKING', "Link (Tracking)", items=link_tracking_category),
- MantisNodeCategory('LINK_RELATIONSHIP', "Link (Inheritance)", items=link_relationship_category),
- MantisNodeCategory('DEFORMER', "Deformer", items=deformer_category),
- MantisNodeCategory('XFORM', "Transform", items=xForm_category),
- MantisNodeCategory('DRIVER', "Driver", items=driver_category),
- MantisNodeCategory('GEOMETRY', "Geometry", items =geometry_category),
- MantisNodeCategory('UTILITIES', "Utility", items=utility_category),
- MantisNodeCategory('MATRIX', "Matrix", items=matrix_category),
- MantisNodeCategory('GROUPS', "Groups", items=groups_category),
- MantisGroupCategory('GROUP_INTERFACE', "Group In/Out", items=group_interface_category),
- ]
- schema_category=[NodeItem(cls.bl_idname) for cls in schema_definitions.TellClasses()]
- schema_categories = [
- SchemaNodeCategory('SCHEMA_SCHEMA', "Schema", items=schema_category),
- ]
- import bpy
- def init_keymaps():
- kc = bpy.context.window_manager.keyconfigs.addon
- km = kc.keymaps.new(name="Node Generic", space_type='NODE_EDITOR')
- kmi = [
- # Normal operation
- km.keymap_items.new("mantis.group_nodes", 'G', 'PRESS', ctrl=True),
- km.keymap_items.new("mantis.edit_group", 'TAB', 'PRESS'),
- km.keymap_items.new("mantis.execute_node_tree", 'E', 'PRESS'),
- km.keymap_items.new("mantis.mute_node", 'M', 'PRESS'),
- km.keymap_items.new("mantis.nodes_cleanup", "C", 'PRESS', shift=True,),
- # Testing
- km.keymap_items.new("mantis.query_sockets", 'Q', 'PRESS'),
- km.keymap_items.new("mantis.test_operator", 'T', 'PRESS'),
- km.keymap_items.new("mantis.visualize_output", 'V', 'PRESS'),
- # Saving, Loading, Reloading, etc.
- km.keymap_items.new("mantis.export_save_choose", "S", 'PRESS', alt=True,),
- km.keymap_items.new("mantis.export_save_as", "S", 'PRESS', alt=True, shift=True),
- km.keymap_items.new("mantis.reload_tree", "R", 'PRESS', alt=True,),
- km.keymap_items.new("mantis.import_tree", "O", 'PRESS', ctrl=True,),
- ]
- return km, kmi
- addon_keymaps = []
- # handlers! these have to be persistent
- from bpy.app.handlers import persistent
- @persistent
- def update_handler(scene):
- context=bpy.context
- if context.space_data:
- if not hasattr(context.space_data, "path"):
- return
- trees = [p.node_tree for p in context.space_data.path]
- if not trees: return
- if (node_tree := trees[0]).bl_idname in ['MantisTree']:
- if node_tree.prevent_next_exec : pass
- elif node_tree.do_live_update and not (node_tree.is_executing or node_tree.is_exporting):
- prev_links = node_tree.num_links
- node_tree.num_links = len(node_tree.links)
- if (prev_links == -1):
- return
- if prev_links != node_tree.num_links:
- node_tree.tree_valid = False
- if node_tree.tree_valid == False:
- scene.render.use_lock_interface = True
- node_tree.update_tree(context)
- scene.render.use_lock_interface = False
- @persistent
- def execute_handler(scene):
- context = bpy.context
- if context.space_data:
- if not hasattr(context.space_data, "path"):
- return
- trees = [p.node_tree for p in context.space_data.path]
- if not trees: return
- if (node_tree := trees[0]).bl_idname in ['MantisTree']:
- if node_tree.prevent_next_exec : node_tree.prevent_next_exec = False
- elif node_tree.tree_valid and node_tree.do_live_update and not (node_tree.is_executing or node_tree.is_exporting):
- scene.render.use_lock_interface = True
- node_tree.execute_tree(context)
- scene.render.use_lock_interface = False
- node_tree.tree_valid = False
- versioning_node_tasks = [
- #relevant bl_idnames # task
- #(['LinkTransformation'], transformation_constraint_radians_to_degrees)
- ]
- def do_version_update(node_tree):
- from .base_definitions import NODES_REMOVED, SOCKETS_REMOVED, SOCKETS_RENAMED, SOCKETS_ADDED
- for n in node_tree.nodes:
- rename_jobs = []
- if n.bl_idname in NODES_REMOVED:
- print(f"INFO: removing node {n.name} of type {n.bl_idname} because it has been deprecated.")
- n.inputs.remove(socket)
- continue
- for i, socket in enumerate(n.inputs.values()):
- if (n.bl_idname, socket.identifier) in SOCKETS_REMOVED:
- print(f"INFO: removing socket {socket.identifier} of node {n.name} of type {n.bl_idname} because it has been deprecated.")
- n.inputs.remove(socket)
- for old_class, old_bl_idname, old_name, new_bl_idname, new_name, multi in SOCKETS_RENAMED:
- if (n.bl_idname == old_class and socket.bl_idname == old_bl_idname and socket.name == old_name):
- rename_jobs.append((socket, i, new_bl_idname, new_name, multi))
- for i, socket in enumerate(n.outputs.values()):
- if (n.bl_idname, socket.identifier) in SOCKETS_REMOVED:
- print(f"INFO: removing socket {socket.identifier} of node {n.name} of type {n.bl_idname} because it has been deprecated.")
- n.outputs.remove(socket)
- for old_class, old_bl_idname, old_name, new_bl_idname, new_name, multi in SOCKETS_RENAMED:
- if (n.bl_idname == old_class and socket.bl_idname == old_bl_idname and socket.name == old_name):
- rename_jobs.append((socket, i, new_bl_idname, new_name, multi))
- for bl_idname, in_out, socket_type, socket_name, index, use_multi_input, default_val in SOCKETS_ADDED:
- if n.bl_idname != bl_idname:
- continue
- if in_out == 'INPUT' and n.inputs.get(socket_name) is None:
- print(f"INFO: adding socket \"{socket_name}\" of type {socket_type} to node {n.name} of type {n.bl_idname}.")
- s = n.inputs.new(socket_type, socket_name, use_multi_input=use_multi_input)
- s.default_value = default_val
- n.inputs.move(len(n.inputs)-1, index)
- socket_map = None
- if rename_jobs:
- from .utilities import get_socket_maps
- socket_maps = get_socket_maps(n)
- for socket, socket_index, new_bl_idname, new_name, multi in rename_jobs:
- old_id = socket.identifier
- print (f"Renaming socket {socket.identifier} to {new_name} in node {n.name}")
- from .utilities import do_relink
- if socket.is_output:
- index = 1
- in_out = "OUTPUT"
- n.outputs.remove(socket)
- s = n.outputs.new(new_bl_idname, new_name, identifier=new_name, use_multi_input=multi)
- n.outputs.move(len(n.outputs)-1, socket_index)
- socket_map = socket_maps[1]
- else:
- index = 0
- in_out = "INPUT"
- n.inputs.remove(socket)
- s = n.inputs.new(new_bl_idname, new_name, identifier=new_name, use_multi_input=multi)
- n.inputs.move(len(n.inputs)-1, socket_index)
- socket_map = socket_maps[0]
- socket_map[new_name] = socket_map[old_id]; del socket_map[old_id]
- do_relink(n, s, socket_map)
- for bl_idname, task in versioning_node_tasks:
- if n.bl_idname in bl_idname: task(n)
- # increment the version at the end
- node_tree.mantis_version[0] = MANTIS_VERSION_MAJOR
- node_tree.mantis_version[1] = MANTIS_VERSION_MINOR
- node_tree.mantis_version[2] = MANTIS_VERSION_SUB
- @persistent
- def version_update_handler(filename):
- for node_tree in bpy.data.node_groups:
- if node_tree.bl_idname in ["MantisTree", "SchemaTree"]:
- if (node_tree.mantis_version[0] < MANTIS_VERSION_MAJOR) or \
- (node_tree.mantis_version[1] < MANTIS_VERSION_MINOR) or \
- (node_tree.mantis_version[2] < MANTIS_VERSION_SUB):
- print (f"Updating tree {node_tree.name} to {MANTIS_VERSION_MAJOR}.{MANTIS_VERSION_MINOR}.{MANTIS_VERSION_SUB}")
- do_version_update(node_tree)
-
- # I'll need to do some fiddling here when it comes time to try
- # and make rig definitions animatable.
- @persistent
- def on_animation_playback_pre_handler(scene,depsgraph):
- for t in bpy.data.node_groups:
- if t.bl_idname in ['MantisTree', 'SchemaTree']:
- t.is_executing = True
- @persistent
- def on_animation_playback_post_handler(scene,depsgraph):
- for t in bpy.data.node_groups:
- if t.bl_idname in ['MantisTree', 'SchemaTree']:
- t.is_executing = False
- @persistent
- def on_undo_pre_handler(scene): # the undo will trigger a depsgraph update
- for t in bpy.data.node_groups: # so we enable prevent_next_exec.
- if t.bl_idname in ['MantisTree', 'SchemaTree']:
- t.prevent_next_exec = True
- @persistent
- def on_undo_post_handler(scene): # the undo will trigger a depsgraph update
- for t in bpy.data.node_groups: # so we enable prevent_next_exec.
- if t.bl_idname in ['MantisTree', 'SchemaTree']:
- t.prevent_next_exec = True
- @persistent
- def on_save_pre_handler(scene): # save-as will trigger a depsgraph update
- for t in bpy.data.node_groups: # so we enable prevent_next_exec.
- if t.bl_idname in ['MantisTree', 'SchemaTree']:
- t.prevent_next_exec = True
- # annoyingly, regular save does not trigger a DG update so we also need this:
- @persistent
- def on_save_post_handler(scene): # The DG has already updated and we can disable this.
- for t in bpy.data.node_groups:
- if t.bl_idname in ['MantisTree', 'SchemaTree']:
- t.prevent_next_exec = False
- def register():
- from bpy.utils import register_class
-
- for cls in classes:
- try:
- register_class(cls)
- except RuntimeError as e:
- prRed(cls.__name__)
- raise e
- nodeitems_utils.register_node_categories('MantisNodeCategories', node_categories)
- nodeitems_utils.register_node_categories('SchemaNodeCategories', schema_categories)
- km, kmi = init_keymaps()
- for k in kmi:
- k.active = True
- addon_keymaps.append((km, k))
- # add the handlers
- bpy.app.handlers.depsgraph_update_pre.append(update_handler)
- bpy.app.handlers.depsgraph_update_post.append(execute_handler)
- bpy.app.handlers.save_pre.append(on_save_pre_handler)
- bpy.app.handlers.save_post.append(on_save_post_handler)
- bpy.app.handlers.load_post.append(version_update_handler)
- bpy.app.handlers.animation_playback_pre.append(on_animation_playback_pre_handler)
- bpy.app.handlers.animation_playback_post.append(on_animation_playback_post_handler)
- bpy.app.handlers.undo_pre.append(on_undo_pre_handler)
-
- def unregister():
- nodeitems_utils.unregister_node_categories('MantisNodeCategories')
- nodeitems_utils.unregister_node_categories('SchemaNodeCategories')
- from bpy.utils import unregister_class
- for cls in reversed(classes):
- unregister_class(cls)
-
- for km, kmi in addon_keymaps:
- km.keymap_items.remove(kmi)
- addon_keymaps.clear()
|