versioning.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 bpy.app import version as bpy_version
  5. from .utilities import prRed, prGreen, prPurple
  6. def version_upgrade_very_old(*args, **kwargs):
  7. node = kwargs['node']
  8. current_major_version = node.id_data.mantis_version[0]
  9. current_minor_version = node.id_data.mantis_version[1]
  10. if current_major_version > 0: return# major version must be 0
  11. if current_minor_version > 11: return# minor version must be 12 or less
  12. # this is the old node update, very inneficient and strange and badly organized
  13. NODES_REMOVED=["xFormRootNode"]
  14. # Node bl_idname, # Socket Name
  15. SOCKETS_REMOVED=[("UtilityDriverVariable", "Transform Channel"),
  16. ("xFormRootNode","World Out"),
  17. ("UtilitySwitch","xForm"),
  18. ("LinkDrivenParameter", "Enable")]
  19. # Node Class #Prior bl_idname # prior name # new bl_idname # new name, # Multi
  20. SOCKETS_RENAMED=[ ("LinkDrivenParameter", "DriverSocket", "Driver", "FloatSocket", "Value", False),
  21. ("DeformerHook", "IntSocket", "Index", "UnsignedIntSocket", "Point Index", False),
  22. ("SchemaConstOutput", "IntSocket", "Expose when N==", "UnsignedIntSocket", "Expose at Index", False),]
  23. # NODE CLASS NAME IN_OUT SOCKET TYPE SOCKET NAME INDEX MULTI DEFAULT
  24. SOCKETS_ADDED=[("DeformerMorphTargetDeform", 'INPUT', 'BooleanSocket', "Use Shape Key", 1, False, False),
  25. ("DeformerMorphTargetDeform", 'INPUT', 'BooleanSocket', "Use Offset", 2, False, True),
  26. ("UtilityFCurve", 'INPUT', "eFCrvExtrapolationMode", "Extrapolation Mode", 0, False, 'CONSTANT'),
  27. ("LinkCopyScale", 'INPUT', "BooleanSocket", "Additive", 3, False, False),
  28. ("DeformerHook", 'INPUT', "FloatFactorSocket", "Influence",3, False, 1.0),
  29. ("DeformerHook", 'INPUT', "UnsignedIntSocket", "Spline Index", 2, False, 0),
  30. ("DeformerHook", 'INPUT', "BooleanSocket", "Auto-Bezier", 5, False, True),
  31. ("UtilityCompare", 'INPUT', "EnumCompareOperation", "Comparison", 0, False, 'EQUAL'),
  32. ("UtilityMatrixFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  33. ("UtilityMatricesFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  34. ("UtilityPointFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  35. ("LinkCopyScale", 'INPUT', "FloatFactorSocket", "Power", 5, False, 1.0),
  36. ]
  37. rename_jobs = []
  38. node_tree = kwargs['node_tree']
  39. try:
  40. if node.bl_idname in NODES_REMOVED:
  41. print(f"INFO: removing node {node.name} of type {node.bl_idname} because it has been deprecated.")
  42. node.inputs.remove(socket)
  43. return
  44. for i, socket in enumerate(node.inputs.values()):
  45. if (node.bl_idname, socket.identifier) in SOCKETS_REMOVED:
  46. print(f"INFO: removing socket {socket.identifier} of node {node.name} of type {node.bl_idname} because it has been deprecated.")
  47. node.inputs.remove(socket)
  48. for old_class, old_bl_idname, old_name, new_bl_idname, new_name, multi in SOCKETS_RENAMED:
  49. if (node.bl_idname == old_class and socket.bl_idname == old_bl_idname and socket.name == old_name):
  50. rename_jobs.append((socket, i, new_bl_idname, new_name, multi))
  51. for i, socket in enumerate(node.outputs.values()):
  52. if (node.bl_idname, socket.identifier) in SOCKETS_REMOVED:
  53. print(f"INFO: removing socket {socket.identifier} of node {node.name} of type {node.bl_idname} because it has been deprecated.")
  54. node.outputs.remove(socket)
  55. for old_class, old_bl_idname, old_name, new_bl_idname, new_name, multi in SOCKETS_RENAMED:
  56. if (node.bl_idname == old_class and socket.bl_idname == old_bl_idname and socket.name == old_name):
  57. rename_jobs.append((socket, i, new_bl_idname, new_name, multi))
  58. for bl_idname, in_out, socket_type, socket_name, index, use_multi_input, default_val in SOCKETS_ADDED:
  59. if node.bl_idname != bl_idname:
  60. continue
  61. if in_out == 'INPUT' and node.inputs.get(socket_name) is None:
  62. print(f"INFO: adding socket \"{socket_name}\" of type {socket_type} to node {node.name} of type {node.bl_idname}.")
  63. s = node.inputs.new(socket_type, socket_name, use_multi_input=use_multi_input)
  64. try:
  65. s.default_value = default_val
  66. except AttributeError:
  67. pass # the socket is read-only
  68. node.inputs.move(len(node.inputs)-1, index)
  69. socket_map = None
  70. if rename_jobs:
  71. from .utilities import get_socket_maps
  72. socket_maps = get_socket_maps(node)
  73. for socket, socket_index, new_bl_idname, new_name, multi in rename_jobs:
  74. old_id = socket.identifier
  75. print (f"Renaming socket {socket.identifier} to {new_name} in node {node.name}")
  76. from .utilities import do_relink
  77. if socket.is_output:
  78. index = 1
  79. in_out = "OUTPUT"
  80. node.outputs.remove(socket)
  81. s = node.outputs.new(new_bl_idname, new_name, identifier=new_name, use_multi_input=multi)
  82. node.outputs.move(len(node.outputs)-1, socket_index)
  83. socket_map = socket_maps[1]
  84. else:
  85. index = 0
  86. in_out = "INPUT"
  87. node.inputs.remove(socket)
  88. s = node.inputs.new(new_bl_idname, new_name, identifier=new_name, use_multi_input=multi)
  89. node.inputs.move(len(node.inputs)-1, socket_index)
  90. socket_map = socket_maps[0]
  91. socket_map[new_name] = socket_map[old_id]
  92. if new_name != old_id: del socket_map[old_id] # sometimes rename just changes the socket type or multi
  93. do_relink(node, s, socket_map)
  94. except Exception as e:
  95. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  96. print(e)
  97. def version_upgrade_bone_0_12_0_from_older(*args, **kwargs):
  98. # we need to check if it has an array collection input and a color input
  99. # then we need to solve each task
  100. node = kwargs['node']
  101. current_major_version = node.id_data.mantis_version[0]
  102. current_minor_version = node.id_data.mantis_version[1]
  103. if current_major_version > 0: # major version must be 0
  104. return
  105. if current_minor_version >= 12: # minor version must be 11 or less
  106. return
  107. # sub version doesn't matter since any subversion of 11 should trigger this task
  108. try:
  109. collection_input_is_array = node.inputs['Bone Collection'].is_multi_input
  110. if not collection_input_is_array: # it must be made into an array!
  111. prPurple(f"Updating \"Bone Collection\" Socket in {node.name}")
  112. from .utilities import get_socket_maps
  113. socket_maps = get_socket_maps(node)
  114. socket_map = socket_maps[0]
  115. for i, socket in enumerate(node.inputs):
  116. if socket.name == 'Bone Collection': break
  117. old_id = socket.identifier
  118. # it is an input
  119. node.inputs.remove(socket)
  120. s = node.inputs.new('BoneCollectionSocket', 'Bone Collection',
  121. identifier='Bone Collection', use_multi_input=True)
  122. node.inputs.move(len(node.inputs)-1, i)
  123. socket_map_from_old_socket = socket_map[old_id]
  124. # there seems to be an error in do_relink
  125. # gonna do it directly instead
  126. if isinstance(socket_map_from_old_socket, list):
  127. for map_info in socket_map_from_old_socket:
  128. if isinstance(map_info, Node ):
  129. l = node.id_data.links.new(input=map_info.outputs[0], output=s)
  130. elif isinstance(map_info, NodeSocket):
  131. l = node.id_data.links.new(input=map_info, output=s)
  132. else:
  133. s.default_value = socket_map_from_old_socket
  134. if node.inputs.get('Color') is None:
  135. prPurple(f"Adding \"Color\" Socket to {node.name}")
  136. s = node.inputs.new('ColorSetSocket', 'Color',)
  137. node.inputs.move(len(node.inputs)-1, 22)
  138. except Exception as e:
  139. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  140. print(e)
  141. def up_0_12_1_add_inherit_color(*args, **kwargs):
  142. # add an inherit color input.
  143. node = kwargs['node']
  144. current_major_version = node.id_data.mantis_version[0]
  145. current_minor_version = node.id_data.mantis_version[1]
  146. current_sub_version = node.id_data.mantis_version[2]
  147. if current_major_version > 0: return# major version must be 0
  148. if current_minor_version > 12: return# minor version must be 12 or less
  149. if current_minor_version == 12 and current_sub_version > 9: return # sub version must be 8 or less
  150. # I am having it do 8 or less because there was a bug in this function prior to 9
  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 and 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. def up_0_12_13_add_widget_scale(*args, **kwargs):
  166. # add an inherit color input.
  167. node = kwargs['node']
  168. current_major_version = node.id_data.mantis_version[0]
  169. current_minor_version = node.id_data.mantis_version[1]
  170. current_sub_version = node.id_data.mantis_version[2]
  171. if current_major_version > 0: return# major version must be 0
  172. if current_minor_version > 12: return# minor version must be 12 or less
  173. if current_minor_version == 12 and current_sub_version > 12: return
  174. # sub version must be 12 or less
  175. prPurple(f"Adding \"Scale\" socket to {node.name}")
  176. try:
  177. scale = node.inputs.get('Scale')
  178. if scale is None:
  179. s = node.inputs.new('VectorScaleSocket', 'Scale',)
  180. s.default_value=[1.0,1.0,1.0]
  181. node.inputs.move(len(node.inputs)-1, 1)
  182. except Exception as e:
  183. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  184. print(e)
  185. # versioning tasks that involve Blender versions rather than Mantis versions:
  186. error_description_4_5_0_LTS = 'There is a bug in Blender 4.5.0 LTS, that is why these sockets are blue.'\
  187. ' This will be fixed in future Blender versions.'
  188. def cleanup_4_5_0_LTS_interface_workaround(*args, **kwargs):
  189. # this is a function for cleaning up the workaround up above
  190. tree = kwargs['tree']
  191. if bpy_version == (4,5,0): return
  192. if not hasattr(tree, "interface_helper"):
  193. return
  194. import json
  195. try:
  196. interface_helper = json.loads(tree.interface_helper)
  197. except:
  198. return # this should mean it was never a problem to begin with.
  199. prPurple(f"Restoring Tree Interface for {tree.name}.")
  200. for interface_item in tree.interface.items_tree:
  201. if interface_item.item_type == 'PANEL': continue
  202. # now we need to restore the socket types, which are in the interface helper
  203. # keep the interface stuff around, I'll use it for marking arrays in the future
  204. int_info = interface_helper[interface_item.identifier]
  205. socket_type = int_info['bl_socket_idname']
  206. interface_item.socket_type = socket_type
  207. if interface_item.description == error_description_4_5_0_LTS:
  208. interface_item.description = ''
  209. # that should be enough!
  210. def up_0_12_25_replace_floor_offset_type(*args, **kwargs):
  211. # add an inherit color input.
  212. node = kwargs['node']
  213. current_major_version = node.id_data.mantis_version[0]
  214. current_minor_version = node.id_data.mantis_version[1]
  215. current_sub_version = node.id_data.mantis_version[2]
  216. if current_major_version > 0: return# major version must be 0
  217. if current_minor_version > 12: return# minor version must be 12 or less
  218. if current_minor_version == 12 and current_sub_version > 24: return
  219. from .utilities import get_socket_maps, do_relink
  220. socket_maps = get_socket_maps(node)
  221. prPurple(f"Fixing \"Offset\" socket in {node.name}")
  222. try:
  223. offset = node.inputs["Offset"]
  224. node.inputs.remove(offset)
  225. s = node.inputs.new('FloatSocket', 'Offset',)
  226. node.inputs.move(len(node.inputs)-1, 2)
  227. do_relink(node, s, socket_maps[0])
  228. except Exception as e:
  229. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  230. print(e)
  231. versioning_tasks = [
  232. # node bl_idname task required keyword arguments
  233. (['ALL'], version_upgrade_very_old, ['node_tree', 'node'],),
  234. (['xFormBoneNode'], version_upgrade_bone_0_12_0_from_older, ['node'],),
  235. (['xFormBoneNode'], up_0_12_1_add_inherit_color, ['node'],),
  236. (['MantisTree', 'SchemaTree'], cleanup_4_5_0_LTS_interface_workaround, ['tree']),
  237. (['InputWidget'], up_0_12_13_add_widget_scale, ['node']),
  238. (['LinkFloor'], up_0_12_25_replace_floor_offset_type, ['node']),
  239. ]
  240. # WORKAROUNDS for bugs should go here:
  241. # 4.5.0 LTS valid socket types bug:
  242. def workaround_4_5_0_interface_update(tree, name, in_out, sock_type, parent_name, do_parent=False):
  243. # TODO: dedupe this code from i_o.py
  244. prRed("There is a bug in Blender 4.5.0 regarding node-group interface sockets. Working around it.")
  245. sock = tree.interface.new_socket(name=name, in_out=in_out, socket_type="NodeSocketGeometry")
  246. import json
  247. interface_helper = {} # initialize it if it is empty
  248. if tree.interface_helper: # may be empty, check here
  249. interface_helper = json.loads(tree.interface_helper)
  250. error_message = error_description_4_5_0_LTS
  251. interface_helper[sock.identifier] = {
  252. 'name' : sock.name,
  253. 'identifier' : sock.identifier,
  254. 'in_out' : sock.in_out,
  255. 'socket_type' : sock_type,
  256. 'bl_socket_idname' : sock_type,
  257. 'mantis_socket_category' : parent_name,
  258. 'description' : error_message,
  259. }
  260. sock.description = error_message # this tells the user why the socket looks weird.
  261. tree.interface_helper = json.dumps(interface_helper)
  262. if do_parent and (parent := tree.interface.items_tree.get(parent_name)):
  263. tree.interface.move_to_parent(
  264. sock,
  265. parent,
  266. 0, # what to do here?
  267. )
  268. return sock
  269. def socket_add_workaround_for_4_5_0_LTS(item, socket_collection, multi):
  270. import json
  271. tree = item.id_data
  272. interface_helper = json.loads(tree.interface_helper)
  273. socket_info = interface_helper.get(item.identifier)
  274. if not socket_info: raise RuntimeError(f"There has been an error adding the socket {item.name}")
  275. s = socket_collection.new(
  276. type=socket_info['bl_socket_idname'],
  277. name=item.name, # in case the user has changed it
  278. identifier=item.identifier,
  279. use_multi_input=multi, )
  280. return s