versioning.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #Versioning Tasks
  2. # this will be the new versioning system, and will deprecate the old SOCKETS_ADDED and such
  3. from bpy.types import Node, NodeSocket
  4. from .utilities import prRed, prGreen, prPurple
  5. def version_upgrade_very_old(*args, **kwargs):
  6. node = kwargs['node']
  7. current_major_version = node.id_data.mantis_version[0]
  8. current_minor_version = node.id_data.mantis_version[1]
  9. if current_major_version > 0: return# major version must be 0
  10. if current_minor_version > 11: return# minor version must be 12 or less
  11. # this is the old node update, very inneficient and strange and badly organized
  12. NODES_REMOVED=["xFormRootNode"]
  13. # Node bl_idname, # Socket Name
  14. SOCKETS_REMOVED=[("UtilityDriverVariable", "Transform Channel"),
  15. ("xFormRootNode","World Out"),
  16. ("UtilitySwitch","xForm"),
  17. ("LinkDrivenParameter", "Enable")]
  18. # Node Class #Prior bl_idname # prior name # new bl_idname # new name, # Multi
  19. SOCKETS_RENAMED=[ ("LinkDrivenParameter", "DriverSocket", "Driver", "FloatSocket", "Value", False),
  20. ("DeformerHook", "IntSocket", "Index", "UnsignedIntSocket", "Point Index", False),
  21. ("SchemaConstOutput", "IntSocket", "Expose when N==", "UnsignedIntSocket", "Expose at Index", False),]
  22. # NODE CLASS NAME IN_OUT SOCKET TYPE SOCKET NAME INDEX MULTI DEFAULT
  23. SOCKETS_ADDED=[("DeformerMorphTargetDeform", 'INPUT', 'BooleanSocket', "Use Shape Key", 1, False, False),
  24. ("DeformerMorphTargetDeform", 'INPUT', 'BooleanSocket', "Use Offset", 2, False, True),
  25. ("UtilityFCurve", 'INPUT', "eFCrvExtrapolationMode", "Extrapolation Mode", 0, False, 'CONSTANT'),
  26. ("LinkCopyScale", 'INPUT', "BooleanSocket", "Additive", 3, False, False),
  27. ("DeformerHook", 'INPUT', "FloatFactorSocket", "Influence",3, False, 1.0),
  28. ("DeformerHook", 'INPUT', "UnsignedIntSocket", "Spline Index", 2, False, 0),
  29. ("DeformerHook", 'INPUT', "BooleanSocket", "Auto-Bezier", 5, False, True),
  30. ("UtilityCompare", 'INPUT', "EnumCompareOperation", "Comparison", 0, False, 'EQUAL'),
  31. ("UtilityMatrixFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  32. ("UtilityMatricesFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  33. ("UtilityPointFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  34. ("LinkCopyScale", 'INPUT', "FloatFactorSocket", "Power", 5, False, 1.0),
  35. ]
  36. rename_jobs = []
  37. node_tree = kwargs['node_tree']
  38. try:
  39. if node.bl_idname in NODES_REMOVED:
  40. print(f"INFO: removing node {node.name} of type {node.bl_idname} because it has been deprecated.")
  41. node.inputs.remove(socket)
  42. return
  43. for i, socket in enumerate(node.inputs.values()):
  44. if (node.bl_idname, socket.identifier) in SOCKETS_REMOVED:
  45. print(f"INFO: removing socket {socket.identifier} of node {node.name} of type {node.bl_idname} because it has been deprecated.")
  46. node.inputs.remove(socket)
  47. for old_class, old_bl_idname, old_name, new_bl_idname, new_name, multi in SOCKETS_RENAMED:
  48. if (node.bl_idname == old_class and socket.bl_idname == old_bl_idname and socket.name == old_name):
  49. rename_jobs.append((socket, i, new_bl_idname, new_name, multi))
  50. for i, socket in enumerate(node.outputs.values()):
  51. if (node.bl_idname, socket.identifier) in SOCKETS_REMOVED:
  52. print(f"INFO: removing socket {socket.identifier} of node {node.name} of type {node.bl_idname} because it has been deprecated.")
  53. node.outputs.remove(socket)
  54. for old_class, old_bl_idname, old_name, new_bl_idname, new_name, multi in SOCKETS_RENAMED:
  55. if (node.bl_idname == old_class and socket.bl_idname == old_bl_idname and socket.name == old_name):
  56. rename_jobs.append((socket, i, new_bl_idname, new_name, multi))
  57. for bl_idname, in_out, socket_type, socket_name, index, use_multi_input, default_val in SOCKETS_ADDED:
  58. if node.bl_idname != bl_idname:
  59. continue
  60. if in_out == 'INPUT' and node.inputs.get(socket_name) is None:
  61. print(f"INFO: adding socket \"{socket_name}\" of type {socket_type} to node {node.name} of type {node.bl_idname}.")
  62. s = node.inputs.new(socket_type, socket_name, use_multi_input=use_multi_input)
  63. try:
  64. s.default_value = default_val
  65. except AttributeError:
  66. pass # the socket is read-only
  67. node.inputs.move(len(node.inputs)-1, index)
  68. socket_map = None
  69. if rename_jobs:
  70. from .utilities import get_socket_maps
  71. socket_maps = get_socket_maps(node)
  72. for socket, socket_index, new_bl_idname, new_name, multi in rename_jobs:
  73. old_id = socket.identifier
  74. print (f"Renaming socket {socket.identifier} to {new_name} in node {node.name}")
  75. from .utilities import do_relink
  76. if socket.is_output:
  77. index = 1
  78. in_out = "OUTPUT"
  79. node.outputs.remove(socket)
  80. s = node.outputs.new(new_bl_idname, new_name, identifier=new_name, use_multi_input=multi)
  81. node.outputs.move(len(node.outputs)-1, socket_index)
  82. socket_map = socket_maps[1]
  83. else:
  84. index = 0
  85. in_out = "INPUT"
  86. node.inputs.remove(socket)
  87. s = node.inputs.new(new_bl_idname, new_name, identifier=new_name, use_multi_input=multi)
  88. node.inputs.move(len(node.inputs)-1, socket_index)
  89. socket_map = socket_maps[0]
  90. socket_map[new_name] = socket_map[old_id]
  91. if new_name != old_id: del socket_map[old_id] # sometimes rename just changes the socket type or multi
  92. do_relink(node, s, socket_map)
  93. for bl_idname, task in versioning_node_tasks:
  94. if node.bl_idname in bl_idname: task(node)
  95. except Exception as e:
  96. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  97. print(e)
  98. def version_upgrade_bone_0_12_0_from_older(*args, **kwargs):
  99. # we need to check if it has an array collection input and a color input
  100. # then we need to solve each task
  101. node = kwargs['node']
  102. current_major_version = node.id_data.mantis_version[0]
  103. current_minor_version = node.id_data.mantis_version[1]
  104. if current_major_version > 0: # major version must be 0
  105. return
  106. if current_minor_version >= 12: # minor version must be 11 or less
  107. return
  108. # sub version doesn't matter since any subversion of 11 should trigger this task
  109. try:
  110. collection_input_is_array = node.inputs['Bone Collection'].is_multi_input
  111. if not collection_input_is_array: # it must be made into an array!
  112. prPurple(f"Updating \"Bone Collection\" Socket in {node.name}")
  113. from .utilities import get_socket_maps
  114. socket_maps = get_socket_maps(node)
  115. socket_map = socket_maps[0]
  116. for i, socket in enumerate(node.inputs):
  117. if socket.name == 'Bone Collection': break
  118. old_id = socket.identifier
  119. # it is an input
  120. node.inputs.remove(socket)
  121. s = node.inputs.new('BoneCollectionSocket', 'Bone Collection',
  122. identifier='Bone Collection', use_multi_input=True)
  123. node.inputs.move(len(node.inputs)-1, i)
  124. socket_map_from_old_socket = socket_map[old_id]
  125. # there seems to be an error in do_relink
  126. # gonna do it directly instead
  127. if isinstance(socket_map_from_old_socket, list):
  128. for map_info in socket_map_from_old_socket:
  129. if isinstance(map_info, Node ):
  130. l = node.id_data.links.new(input=map_info.outputs[0], output=s)
  131. elif isinstance(map_info, NodeSocket):
  132. l = node.id_data.links.new(input=map_info, output=s)
  133. else:
  134. s.default_value = socket_map_from_old_socket
  135. if node.inputs.get('Color') is None:
  136. prPurple(f"Adding \"Color\" Socket to {node.name}")
  137. s = node.inputs.new('ColorSetSocket', 'Color',)
  138. node.inputs.move(len(node.inputs)-1, 22)
  139. except Exception as e:
  140. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  141. print(e)
  142. def up_0_12_1_add_inherit_color(*args, **kwargs):
  143. # add an inherit color input.
  144. node = kwargs['node']
  145. current_major_version = node.id_data.mantis_version[0]
  146. current_minor_version = node.id_data.mantis_version[1]
  147. current_sub_version = node.id_data.mantis_version[2]
  148. if current_major_version > 0: return# major version must be 0
  149. if current_minor_version > 12: return# minor version must be 12 or less
  150. if current_minor_version == 12 and current_sub_version < 1: return # sub version must be 0
  151. # sub version doesn't matter since any subversion of 11 should trigger this task
  152. prPurple(f"Adding \"Inherit Color\" socket to {node.name}")
  153. try:
  154. inh_color = node.inputs.get('Inherit Color')
  155. if inh_color.bl_idname != 'BooleanSocket':
  156. node.inputs.remove(inh_color)
  157. inh_color = None
  158. if inh_color is None:
  159. s = node.inputs.new('BooleanSocket', 'Inherit Color',)
  160. node.inputs.move(len(node.inputs)-1, 23)
  161. s.default_value=True
  162. except Exception as e:
  163. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  164. print(e)
  165. versioning_tasks = [
  166. # node bl_idname task required keyword arguments
  167. (['ALL'], version_upgrade_very_old, ['node_tree', 'node'],),
  168. (['xFormBoneNode'], version_upgrade_bone_0_12_0_from_older, ['node'],),
  169. (['xFormBoneNode'], up_0_12_1_add_inherit_color, ['node'],),
  170. ]