versioning.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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.utilities import (prRed, prGreen, prPurple, prWhite,
  6. prOrange,
  7. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  8. wrapOrange,
  9. get_socket_maps, do_relink, )
  10. # CHECKLIST FOR DUMMIES:
  11. # Did you make sure to turn off and re-enable tree updates?
  12. # Did you restore connections to the node socket after changing things?
  13. # Did you test FUNCTIONALITY in your test rigs instead of just checking if the tree
  14. # would generate? CHANGED FUNCTIONALITY IS STILL A BUG!!
  15. # Did you make sure to toggle the is_updating flag for ANY node that is touched by
  16. # the edits you are doing to the current node (example: removing a socket causes
  17. # linked group and schema nodes to update)
  18. # (add more whenever a weird or difficult to debug problem occurs)
  19. # Did you check the update script on a tree that doesn't need it?
  20. # Did you use a >= when you should have used a > (or similar)?
  21. def version_upgrade_very_old(*args, **kwargs):
  22. node = kwargs['node']
  23. current_major_version = node.id_data.mantis_version[0]
  24. current_minor_version = node.id_data.mantis_version[1]
  25. if current_major_version > 0: return# major version must be 0
  26. if current_minor_version > 11: return# minor version must be 12 or less
  27. # this is the old node update, very inneficient and strange and badly organized
  28. NODES_REMOVED=["xFormRootNode"]
  29. # Node bl_idname, # Socket Name
  30. SOCKETS_REMOVED=[("UtilityDriverVariable", "Transform Channel"),
  31. ("xFormRootNode","World Out"),
  32. ("UtilitySwitch","xForm"),
  33. ("LinkDrivenParameter", "Enable")]
  34. # Node Class #Prior bl_idname # prior name # new bl_idname # new name, # Multi
  35. SOCKETS_RENAMED=[ ("LinkDrivenParameter", "DriverSocket", "Driver", "FloatSocket", "Value", False),
  36. ("DeformerHook", "IntSocket", "Index", "UnsignedIntSocket", "Point Index", False),
  37. ("SchemaConstOutput", "IntSocket", "Expose when N==", "UnsignedIntSocket", "Expose at Index", False),]
  38. # NODE CLASS NAME IN_OUT SOCKET TYPE SOCKET NAME INDEX MULTI DEFAULT
  39. SOCKETS_ADDED=[("DeformerMorphTargetDeform", 'INPUT', 'BooleanSocket', "Use Shape Key", 1, False, False),
  40. ("DeformerMorphTargetDeform", 'INPUT', 'BooleanSocket', "Use Offset", 2, False, True),
  41. ("UtilityFCurve", 'INPUT', "eFCrvExtrapolationMode", "Extrapolation Mode", 0, False, 'CONSTANT'),
  42. ("LinkCopyScale", 'INPUT', "BooleanSocket", "Additive", 3, False, False),
  43. ("DeformerHook", 'INPUT', "FloatFactorSocket", "Influence",3, False, 1.0),
  44. ("DeformerHook", 'INPUT', "UnsignedIntSocket", "Spline Index", 2, False, 0),
  45. ("DeformerHook", 'INPUT', "BooleanSocket", "Auto-Bezier", 5, False, True),
  46. ("UtilityCompare", 'INPUT', "EnumCompareOperation", "Comparison", 0, False, 'EQUAL'),
  47. ("UtilityMatrixFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  48. ("UtilityMatricesFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  49. ("UtilityPointFromCurve", 'INPUT', "UnsignedIntSocket", "Spline Index", 1, False, 0),
  50. ("LinkCopyScale", 'INPUT', "FloatFactorSocket", "Power", 5, False, 1.0),
  51. ]
  52. rename_jobs = []
  53. node_tree = kwargs['node_tree']
  54. try:
  55. if node.bl_idname in NODES_REMOVED:
  56. print(f"INFO: removing node {node.name} of type {node.bl_idname} because it has been deprecated.")
  57. node.inputs.remove(socket)
  58. return
  59. for i, socket in enumerate(node.inputs.values()):
  60. if (node.bl_idname, socket.identifier) in SOCKETS_REMOVED:
  61. print(f"INFO: removing socket {socket.identifier} of node {node.name} of type {node.bl_idname} because it has been deprecated.")
  62. node.inputs.remove(socket)
  63. for old_class, old_bl_idname, old_name, new_bl_idname, new_name, multi in SOCKETS_RENAMED:
  64. if (node.bl_idname == old_class and socket.bl_idname == old_bl_idname and socket.name == old_name):
  65. rename_jobs.append((socket, i, new_bl_idname, new_name, multi))
  66. for i, socket in enumerate(node.outputs.values()):
  67. if (node.bl_idname, socket.identifier) in SOCKETS_REMOVED:
  68. print(f"INFO: removing socket {socket.identifier} of node {node.name} of type {node.bl_idname} because it has been deprecated.")
  69. node.outputs.remove(socket)
  70. for old_class, old_bl_idname, old_name, new_bl_idname, new_name, multi in SOCKETS_RENAMED:
  71. if (node.bl_idname == old_class and socket.bl_idname == old_bl_idname and socket.name == old_name):
  72. rename_jobs.append((socket, i, new_bl_idname, new_name, multi))
  73. for bl_idname, in_out, socket_type, socket_name, index, use_multi_input, default_val in SOCKETS_ADDED:
  74. if node.bl_idname != bl_idname:
  75. continue
  76. if in_out == 'INPUT' and node.inputs.get(socket_name) is None:
  77. print(f"INFO: adding socket \"{socket_name}\" of type {socket_type} to node {node.name} of type {node.bl_idname}.")
  78. s = node.inputs.new(socket_type, socket_name, use_multi_input=use_multi_input)
  79. try:
  80. s.default_value = default_val
  81. except AttributeError:
  82. pass # the socket is read-only
  83. node.inputs.move(len(node.inputs)-1, index)
  84. socket_map = None
  85. if rename_jobs:
  86. socket_maps = get_socket_maps(node)
  87. for socket, socket_index, new_bl_idname, new_name, multi in rename_jobs:
  88. old_id = socket.identifier
  89. print (f"Renaming socket {socket.identifier} to {new_name} in node {node.name}")
  90. if socket.is_output:
  91. index = 1
  92. in_out = "OUTPUT"
  93. node.outputs.remove(socket)
  94. s = node.outputs.new(new_bl_idname, new_name, identifier=new_name, use_multi_input=multi)
  95. node.outputs.move(len(node.outputs)-1, socket_index)
  96. socket_map = socket_maps[1]
  97. else:
  98. index = 0
  99. in_out = "INPUT"
  100. node.inputs.remove(socket)
  101. s = node.inputs.new(new_bl_idname, new_name, identifier=new_name, use_multi_input=multi)
  102. node.inputs.move(len(node.inputs)-1, socket_index)
  103. socket_map = socket_maps[0]
  104. socket_map[new_name] = socket_map[old_id]
  105. if new_name != old_id: del socket_map[old_id] # sometimes rename just changes the socket type or multi
  106. do_relink(node, s, socket_map)
  107. except Exception as e:
  108. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  109. print(e)
  110. def version_upgrade_bone_0_12_0_from_older(*args, **kwargs):
  111. # we need to check if it has an array collection input and a color input
  112. # then we need to solve each task
  113. node = kwargs['node']
  114. live_updates = node.id_data.do_live_update
  115. node.id_data.do_live_update = False
  116. current_major_version = node.id_data.mantis_version[0]
  117. current_minor_version = node.id_data.mantis_version[1]
  118. if current_major_version > 0: # major version must be 0
  119. return
  120. if current_minor_version >= 12: # minor version must be 11 or less
  121. return
  122. # sub version doesn't matter since any subversion of 11 should trigger this task
  123. collection_input_is_array = False
  124. try:
  125. collection_input_is_array = node.inputs['Bone Collection'].is_multi_input
  126. except KeyError: # I suppose this happens in very, very old trees
  127. pass # but it is extremely unlikely
  128. toggle_updating = []
  129. was_updating = {}
  130. try:
  131. if not collection_input_is_array: # it must be made into an array!
  132. prPurple(f"Updating \"Bone Collection\" Socket in {node.name}")
  133. from .utilities.utilities import get_socket_maps
  134. socket_maps = get_socket_maps(node)
  135. socket_map = socket_maps[0]
  136. for i, socket in enumerate(node.inputs):
  137. if socket.name == 'Bone Collection': break
  138. # Always guard socket removal in case of connection to e.g. schema
  139. for socket_name, links in socket_map.items():
  140. if isinstance(links, list):
  141. for link in links:
  142. if hasattr(link, 'node') and hasattr(link.node, 'is_updating'):
  143. toggle_updating.append(link.node)
  144. was_updating[link.node.name] = link.node.is_updating
  145. link.node.is_updating = True
  146. node.inputs.remove(socket)
  147. s = node.inputs.new('BoneCollectionSocket', 'Bone Collection',
  148. identifier='Bone Collection', use_multi_input=True)
  149. node.inputs.move(len(node.inputs)-1, i)
  150. do_relink(node, s, socket_map, in_out='INPUT')
  151. if node.inputs.get('Color') is None:
  152. prPurple(f"Adding \"Color\" Socket to {node.name}")
  153. s = node.inputs.new('ColorSetSocket', 'Color',)
  154. node.inputs.move(len(node.inputs)-1, 22)
  155. except Exception as e:
  156. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  157. print(e)
  158. finally:
  159. # Now we have to clean up by removing the unneeded sockets and repositioning.
  160. for other_node in toggle_updating:
  161. other_node.is_updating = was_updating[other_node.name]
  162. node.id_data.do_live_update = live_updates
  163. def up_0_12_1_add_inherit_color(*args, **kwargs):
  164. # add an inherit color input.
  165. node = kwargs['node']
  166. current_major_version = node.id_data.mantis_version[0]
  167. current_minor_version = node.id_data.mantis_version[1]
  168. current_sub_version = node.id_data.mantis_version[2]
  169. if current_major_version > 0: return# major version must be 0
  170. if current_minor_version > 12: return# minor version must be 12 or less
  171. if current_minor_version == 12 and current_sub_version > 9: return # sub version must be 8 or less
  172. # I am having it do 8 or less because there was a bug in this function prior to 9
  173. # sub version doesn't matter since any subversion of 11 should trigger this task
  174. prPurple(f"Adding \"Inherit Color\" socket to {node.name}")
  175. try:
  176. inh_color = node.inputs.get('Inherit Color')
  177. if inh_color and inh_color.bl_idname != 'BooleanSocket':
  178. node.inputs.remove(inh_color)
  179. inh_color = None
  180. if inh_color is None:
  181. s = node.inputs.new('BooleanSocket', 'Inherit Color',)
  182. node.inputs.move(len(node.inputs)-1, 23)
  183. s.default_value=True
  184. except Exception as e:
  185. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  186. print(e)
  187. def up_0_12_13_add_widget_scale(*args, **kwargs):
  188. node = kwargs['node']
  189. current_major_version = node.id_data.mantis_version[0]
  190. current_minor_version = node.id_data.mantis_version[1]
  191. current_sub_version = node.id_data.mantis_version[2]
  192. if current_major_version > 0: return# major version must be 0
  193. if current_minor_version > 12: return# minor version must be 12 or less
  194. if current_minor_version == 12 and current_sub_version > 12: return
  195. # sub version must be 12 or less
  196. prPurple(f"Adding \"Scale\" socket to {node.name}")
  197. try:
  198. scale = node.inputs.get('Scale')
  199. if scale is None:
  200. s = node.inputs.new('VectorScaleSocket', 'Scale',)
  201. s.default_value=[1.0,1.0,1.0]
  202. node.inputs.move(len(node.inputs)-1, 1)
  203. except Exception as e:
  204. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  205. print(e)
  206. def up_0_12_25_replace_floor_offset_type(*args, **kwargs):
  207. node = kwargs['node']
  208. current_major_version = node.id_data.mantis_version[0]
  209. current_minor_version = node.id_data.mantis_version[1]
  210. current_sub_version = node.id_data.mantis_version[2]
  211. if current_major_version > 0: return# major version must be 0
  212. if current_minor_version > 12: return# minor version must be 12 or less
  213. if current_minor_version == 12 and current_sub_version > 24: return
  214. socket_maps = get_socket_maps(node)
  215. prPurple(f"Fixing \"Offset\" socket in {node.name}")
  216. try:
  217. offset = node.inputs["Offset"]
  218. node.inputs.remove(offset)
  219. s = node.inputs.new('FloatSocket', 'Offset',)
  220. node.inputs.move(len(node.inputs)-1, 2)
  221. do_relink(node, s, socket_maps[0])
  222. except Exception as e:
  223. prRed(f"Error updating version in node: {node.id_data.name}::{node.name}; see error:")
  224. print(e)
  225. def get_absolute_location(node): # using this for version compatibility
  226. location = node.location.copy() # FUTURE: deprecate old blenders and use absolute_location
  227. while(node.parent):
  228. node = node.parent
  229. location+= node.location
  230. return location
  231. def up_0_13_0_new_custom_prop_node(*args, **kwargs):
  232. tree, node = kwargs['node_tree'], kwargs['node']
  233. current_major_version = tree.mantis_version[0]
  234. if current_major_version > 0: return# major version must be 0
  235. current_minor_version = tree.mantis_version[1]
  236. if current_minor_version >= 13: return# minor version must be 12 or less
  237. remove_me, prop_nodes = [], []
  238. from .xForm_nodes_ui import xFormBoneNode
  239. socket_map_in, socket_map_out = get_socket_maps(node)
  240. if isinstance(node, xFormBoneNode):
  241. if node.inputs.get('Custom Properties'):
  242. return # already done here
  243. for custom_input in reversed(list(node.inputs)):
  244. if 'Parameter' not in custom_input.bl_idname:
  245. continue
  246. custom_prop_node = tree.nodes.new('UtilityCustomProperty')
  247. prop_type = None
  248. if custom_input.bl_idname == 'ParameterBoolSocket':
  249. prop_type = 'BOOL'
  250. custom_prop_node.inputs['Default (Bool)'].default_value = getattr(custom_input, "default_value")
  251. elif custom_input.bl_idname == 'ParameterIntSocket':
  252. prop_type = 'INT'
  253. custom_prop_node.inputs['Default (Int)'].default_value = int( getattr(custom_input, "default_value") )
  254. custom_prop_node.inputs['Min (Int)'].default_value = int( getattr(custom_input, "min") )
  255. custom_prop_node.inputs['Max (Int)'].default_value = int( getattr(custom_input, "max") )
  256. custom_prop_node.inputs['Soft Min (Int)'].default_value = int( getattr(custom_input, "soft_min") )
  257. custom_prop_node.inputs['Soft Max (Int)'].default_value = int( getattr(custom_input, "soft_max") )
  258. elif custom_input.bl_idname == 'ParameterFloatSocket':
  259. prop_type = 'FLOAT'
  260. custom_prop_node.inputs['Default (Float)'].default_value = getattr(custom_input, "default_value")
  261. custom_prop_node.inputs['Min (Float)'].default_value = getattr(custom_input, "min")
  262. custom_prop_node.inputs['Max (Float)'].default_value = getattr(custom_input, "max")
  263. custom_prop_node.inputs['Soft Min (Float)'].default_value = getattr(custom_input, "soft_min")
  264. custom_prop_node.inputs['Soft Max (Float)'].default_value = getattr(custom_input, "soft_max")
  265. elif custom_input.bl_idname == 'ParameterVectorSocket':
  266. prop_type = 'VECTOR'
  267. custom_prop_node.inputs['Default (Vector)'].default_value = getattr(custom_input, "default_value")
  268. try:
  269. custom_prop_node.inputs['Min (Float)'].default_value = getattr(custom_input, "min")
  270. custom_prop_node.inputs['Max (Float)'].default_value = getattr(custom_input, "max")
  271. custom_prop_node.inputs['Soft Min (Float)'].default_value = getattr(custom_input, "soft_min")
  272. custom_prop_node.inputs['Soft Max (Float)'].default_value = getattr(custom_input, "soft_max")
  273. except AttributeError: # I don't know if I just never implemented these or if it is version specific
  274. pass # anyways this isn't critical and should not trigger even a warning
  275. elif custom_input.bl_idname == 'ParameterStringSocket':
  276. prop_type = 'STRING'
  277. custom_prop_node.inputs['Default (String)'].default_value = getattr(custom_input, "default_value")
  278. custom_prop_node.inputs['Name'].default_value = custom_input.name
  279. custom_prop_node.inputs['Type'].default_value = prop_type
  280. prop_nodes.append(custom_prop_node)
  281. remove_me.append(custom_input)
  282. # now we need to relink things, because the custom property socket could have had an
  283. # input supplying the default value
  284. relink_value = socket_map_in.get(custom_input.identifier, None)
  285. if isinstance(relink_value, list): # this indicates it is a list of links
  286. # CRITICALLY, we need to force display update before doing this.
  287. custom_prop_node.display_update({}, None) # parsed_tree and context, not needed
  288. input_socket = None
  289. match prop_type:
  290. case 'BOOL':
  291. input_socket = "Default (Bool)"
  292. case 'INT':
  293. input_socket = "Default (Int)"
  294. case 'FLOAT':
  295. input_socket = "Default (Float)"
  296. case 'VECTOR':
  297. input_socket = "Default (Vector)"
  298. case 'STRING':
  299. input_socket = "Default (String)"
  300. case _: # Should never never happen
  301. raise RuntimeError("Unexpected Code Path Reached, this is a bug.")
  302. do_relink(custom_prop_node, custom_prop_node.inputs[input_socket],
  303. {input_socket:relink_value})
  304. while remove_me:
  305. custom_input = remove_me.pop()
  306. node.inputs.remove(custom_input)
  307. new_input = node.inputs.new("CustomPropSocket", "Custom Properties", use_multi_input=True)
  308. from mathutils import Vector
  309. for i, prop_node in enumerate(prop_nodes):
  310. tree.links.new(input=prop_node.outputs[0], output=new_input)
  311. prop_node.location = get_absolute_location(node)
  312. prop_node.location = prop_node.location - Vector((180, -260*i))
  313. prop_node.select = False # it is annoying to select them all by default
  314. else: # just need to add the socket.
  315. if node.inputs.get('Custom Properties'):
  316. return # already done here
  317. new_input = node.inputs.new("CustomPropSocket", "Custom Properties", use_multi_input=True)
  318. def schema_enable_custom_interface_types(*args, **kwargs):
  319. tree = kwargs['tree']
  320. current_major_version = tree.mantis_version[0]
  321. current_minor_version = tree.mantis_version[1]
  322. current_sub_version = tree.mantis_version[2]
  323. if current_major_version > 0: return# major version must be 0
  324. if current_minor_version >= 13: return# minor version must be 12 or less
  325. # we need to set the new interface values on the schema interface stuff
  326. prGreen(f"Updating Schema tree {tree.name} to support new, improved UI!")
  327. tree.do_live_update = False
  328. for n in tree.nodes:
  329. if hasattr(n, "is_updating"):
  330. n.is_updating = True
  331. try:
  332. for item in tree.interface.items_tree:
  333. if item.item_type == 'PANEL':
  334. continue
  335. parent_name = 'Constant'
  336. if item.parent:
  337. parent_name=item.parent.name
  338. if hasattr(item, "is_array"):
  339. # if is_array exists we're in the custom interface class
  340. # so we'll assume the other attributes exist
  341. if parent_name == 'Array':
  342. item.is_array = True
  343. if parent_name == 'Connection':
  344. item.is_array = False
  345. item.is_connection=True
  346. item.connected_to=item.name
  347. # since heretofore it has been a requirement that the names match
  348. except Exception as e:
  349. prRed(f"Error updating version in tree: {tree.name}; see error:")
  350. print(e)
  351. finally:
  352. for n in tree.nodes:
  353. if hasattr(n, "is_updating"):
  354. n.is_updating = False
  355. tree.do_live_update = True
  356. def update_fcurve_and_armature_node_to_multi_socket(*args, **kwargs):
  357. from .base_definitions import SchemaUINode
  358. node = kwargs['node']
  359. live_updates = node.id_data.do_live_update
  360. node.id_data.do_live_update = False
  361. # First we have to get the socket maps and re-init the sockets
  362. convert_keys = {'UtilityFCurve' : [ 'Keyframe', ],
  363. 'LinkArmature' : [ 'Target', 'Weight', ], }
  364. # first test if we need to do the versioning
  365. if (socket := node.inputs.get(convert_keys[node.bl_idname][0]+'s')):
  366. if socket.is_multi_input: return
  367. else: # this will never happen, doesn't hurt to handle it
  368. node.inputs.remove(socket) # must be an array
  369. my_convert_keys = convert_keys[node.bl_idname]
  370. sockets_in, _sockets_out = get_socket_maps(node,)
  371. # NOTE IMPORTANT BUG INFO
  372. # I was using node.inputs.clear() here before unguarded...
  373. # this caused connected Schema nodes to update and mutate the socket map
  374. # which, in turn, connected the new socket to the wildcard and extended the schema
  375. # resulting in trees that fail to run. This was particularly bad because it was
  376. # so hard to debug (due to spooky action at a distance mutation)
  377. # So we have to mark the connected nodes as "updating" before we modify the tree.
  378. # TODO: should I just mark all nodes as updating during versioning?
  379. toggle_updating = []
  380. was_updating = {}
  381. for socket_name, links in sockets_in.items():
  382. if isinstance(links, list):
  383. for link in links:
  384. if hasattr(link, 'node') and hasattr(link.node, 'is_updating'):
  385. toggle_updating.append(link.node)
  386. was_updating[link.node.name] = link.node.is_updating
  387. link.node.is_updating = True
  388. socket_templates_original = node.socket_templates
  389. node.inputs.clear() # this is VERY DANGEROUS without the above
  390. add_me_sockets = [st for st in node.socket_templates if st.is_input == True]
  391. node.init_sockets(add_me_sockets)
  392. node.socket_templates = socket_templates_original
  393. try:
  394. new_sockets_in = {}
  395. for convert_key in my_convert_keys:
  396. for socket_name, links in sockets_in.items():
  397. if links is None: continue
  398. if convert_key+'.' in socket_name:
  399. if new_sockets_in.get(convert_key+'s'):
  400. # has to be in this order to come back alright.
  401. # assuming, that is, that the keys are sorted. they should be.
  402. new_sockets_in[convert_key+'s'] = sockets_in[socket_name] + new_sockets_in[convert_key+'s']
  403. else:
  404. new_sockets_in[convert_key+'s'] = sockets_in[socket_name]
  405. if new_sockets_in:
  406. do_relink(node, node.inputs[convert_key+'s'], new_sockets_in, in_out="INPUT",)
  407. for socket_name, links in sockets_in.items():
  408. proceed = True
  409. for convert_key in my_convert_keys:
  410. if convert_key in socket_name:
  411. proceed = False
  412. if proceed:
  413. do_relink(node, node.inputs[socket_name], sockets_in, in_out='INPUT')
  414. finally:
  415. # Now we have to clean up by removing the unneeded sockets and repositioning.
  416. for other_node in toggle_updating:
  417. other_node.is_updating = was_updating[other_node.name]
  418. node.id_data.do_live_update = live_updates
  419. # These BBone Scale-In, Scale-Out properties had the wrong defaults
  420. # now that they are implemented properly,
  421. # since they were not implemented before, we'll do all of them at once.
  422. # Normally it is wrong to set things directly like this, but they didn't
  423. # do anything before. Oh well.
  424. def fix_bbone_default_scale_in_and_out(*args, **kwargs):
  425. tree = kwargs['tree']
  426. current_major_version = tree.mantis_version[0]
  427. current_minor_version = tree.mantis_version[1]
  428. current_sub_version = tree.mantis_version[2]
  429. if current_major_version > 0: return# major version must be 0
  430. if current_minor_version > 13: return# minor version must be 12 or less
  431. if current_minor_version == 13 and current_sub_version >= 5: return# sub version must be 4 or less
  432. prPurple(f"Fixing BBone Settings for {tree.name}")
  433. live_updates = tree.do_live_update
  434. tree.do_live_update = False
  435. bones = []
  436. for node in tree.nodes:
  437. if hasattr(node, 'is_updating'):
  438. node.is_updating = True
  439. if node.bl_idname == 'xFormBoneNode':
  440. bones.append(node)
  441. try:
  442. for bone_node in bones:
  443. bone_node.inputs['BBone Scale-In'].default_value = (1,1,1)
  444. bone_node.inputs['BBone Scale-Out'].default_value = (1,1,1)
  445. finally:
  446. for node in tree.nodes:
  447. if hasattr(node, 'is_updating'):
  448. node.is_updating = False
  449. tree.do_live_update = live_updates
  450. # New display features, Display Type, new Custom Object gizmo/pivot stuff
  451. def add_new_display_features_v0_13_5(*args, **kwargs):
  452. tree = kwargs['tree']
  453. current_major_version = tree.mantis_version[0]
  454. current_minor_version = tree.mantis_version[1]
  455. current_sub_version = tree.mantis_version[2]
  456. print(tree.mantis_version[:])
  457. if current_major_version > 0:return# major version must be 0
  458. if current_minor_version > 13: return# minor version must be 12 or less
  459. if current_minor_version == 13 and current_sub_version >= 5: return# sub version must be 4 or less
  460. prPurple(f"Adding new Display Features to {tree.name}")
  461. live_updates = tree.do_live_update
  462. tree.do_live_update = False
  463. live_updates = tree.do_live_update
  464. bones = []
  465. armatures = []
  466. for node in tree.nodes:
  467. if hasattr(node, 'is_updating'):
  468. node.is_updating = True
  469. if node.bl_idname == 'xFormBoneNode':
  470. bones.append(node)
  471. if node.bl_idname == 'xFormArmatureNode':
  472. armatures.append(node)
  473. try:
  474. for bone_node in bones:
  475. # we'll just try this...
  476. from .xForm_socket_templates import (BoneDisplayType,
  477. CustomObjectWireWidth,
  478. UseTransformAtCustomShape,
  479. UseTransformAroundCustomShape,
  480. xFormBoneSockets)
  481. input_templates = ( BoneDisplayType,
  482. CustomObjectWireWidth,
  483. UseTransformAroundCustomShape,
  484. UseTransformAtCustomShape, )
  485. bone_node.init_sockets(input_templates)
  486. bone_node.socket_templates = xFormBoneSockets
  487. for i, inp in enumerate(bone_node.inputs):
  488. if inp.name == 'Custom Object xForm Override':
  489. # As confusing as this is, it is the least terrible way to do this
  490. # We add the sockets to the end, then move them from the end to the right place
  491. bone_node.inputs.move(len(bone_node.inputs)-1, i+1)
  492. bone_node.inputs.move(len(bone_node.inputs)-1, i+2)
  493. bone_node.inputs.move(len(bone_node.inputs)-1, i+3)
  494. bone_node.inputs.move(len(bone_node.inputs)-1, i+4)
  495. break
  496. for armature_node in armatures:
  497. # we'll just try this...
  498. from .xForm_socket_templates import (ArmatureDisplayType,
  499. xFormArmatureSockets)
  500. input_templates = ( ArmatureDisplayType, )
  501. armature_node.init_sockets(input_templates)
  502. armature_node.socket_templates = xFormArmatureSockets
  503. for i, inp in enumerate(armature_node.inputs):
  504. if inp.name == 'Relationship':
  505. armature_node.inputs.move(len(armature_node.inputs)-1, i+1)
  506. break
  507. finally:
  508. for node in tree.nodes:
  509. if hasattr(node, 'is_updating'):
  510. node.is_updating = False
  511. tree.do_live_update = live_updates
  512. def up_0_13_5_modes_for_matrix_from_curve_and_compose_matrix(*args, **kwargs):
  513. node = kwargs['node']
  514. if 'Mode' in node.inputs:
  515. return # done already
  516. tree = node.id_data
  517. current_major_version = tree.mantis_version[0]
  518. current_minor_version = tree.mantis_version[1]
  519. current_sub_version = tree.mantis_version[2]
  520. if current_major_version > 0: return# major version must be 0
  521. if current_minor_version >= 13: return# minor version must be 12 or less
  522. if current_minor_version == 13 and current_sub_version >= 5: return# sub version must be 4 or less
  523. live_updates = tree.do_live_update
  524. tree.do_live_update = False
  525. for other_node in tree.nodes:
  526. if hasattr(other_node, 'is_updating'):
  527. other_node.is_updating = True
  528. try:
  529. socket_templates = node.socket_templates
  530. if node.bl_idname == 'UtilityMatrixFromCurve':
  531. from .misc_nodes_socket_templates import (MatrixFromCurveMode,
  532. MatrixFromCurveFactor,
  533. MatrixFromCurveTailFactor, )
  534. input_templates = ( MatrixFromCurveMode, MatrixFromCurveFactor,
  535. MatrixFromCurveTailFactor, )
  536. node.init_sockets(input_templates)
  537. node.inputs.move(len(node.inputs)-3, 0) # move Mode to first place
  538. elif node.bl_idname == 'UtilityMatrixCompose':
  539. from .misc_nodes_socket_templates import (ComposeMatrixBoneLength,
  540. MatrixComposeMode,
  541. ComposeMatrixRotation,)
  542. input_templates = ( ComposeMatrixBoneLength, MatrixComposeMode,
  543. ComposeMatrixRotation,)
  544. node.init_sockets(input_templates)
  545. node.inputs.move(len(node.inputs)-1, 0) # These go in the top, so make em in order
  546. node.inputs.move(len(node.inputs)-1, 0) # and move 'em one at a time in reverse order
  547. node.socket_templates = socket_templates
  548. finally:
  549. for other_node in tree.nodes:
  550. if hasattr(other_node, 'is_updating'):
  551. other_node.is_updating = False
  552. tree.do_live_update = live_updates
  553. # versioning tasks that involve Blender versions rather than Mantis versions:
  554. error_description_4_5_0_LTS = 'There is a bug in Blender 4.5.0 LTS, that is why these sockets are blue.'\
  555. ' This will be fixed in future Blender versions.'
  556. def cleanup_4_5_0_LTS_interface_workaround(*args, **kwargs):
  557. # this is a function for cleaning up the workaround up above
  558. tree = kwargs['tree']
  559. if bpy_version == (4,5,0): return
  560. if not hasattr(tree, "interface_helper"): return
  561. import json
  562. try:
  563. interface_helper = json.loads(tree.interface_helper)
  564. except:
  565. return # this should mean it was never a problem to begin with.
  566. prPurple(f"Restoring Tree Interface for {tree.name}.")
  567. for interface_item in tree.interface.items_tree:
  568. if interface_item.item_type == 'PANEL': continue
  569. # now we need to restore the socket types, which are in the interface helper
  570. # keep the interface stuff around, I'll use it for marking arrays in the future
  571. int_info = interface_helper[interface_item.identifier]
  572. socket_type = int_info['bl_socket_idname']
  573. interface_item.socket_type = socket_type
  574. if interface_item.description == error_description_4_5_0_LTS:
  575. interface_item.description = ''
  576. versioning_tasks = [
  577. # node bl_idname task required keyword arguments
  578. (['ALL'], version_upgrade_very_old, ['node_tree', 'node'],),
  579. (['xFormBoneNode'], version_upgrade_bone_0_12_0_from_older, ['node'],),
  580. (['xFormBoneNode'], up_0_12_1_add_inherit_color, ['node'],),
  581. (['MantisTree', 'SchemaTree'], cleanup_4_5_0_LTS_interface_workaround, ['tree']),
  582. (['InputWidget'], up_0_12_13_add_widget_scale, ['node']),
  583. (['LinkFloor'], up_0_12_25_replace_floor_offset_type, ['node']),
  584. (['XFORM'], up_0_13_0_new_custom_prop_node, ['node', 'node_tree']),
  585. (['SchemaTree'], schema_enable_custom_interface_types, ['tree']),
  586. (['UtilityFCurve', 'LinkArmature'],
  587. update_fcurve_and_armature_node_to_multi_socket, ['node']),
  588. (['MantisTree', 'SchemaTree'], fix_bbone_default_scale_in_and_out, ['tree']),
  589. (['MantisTree', 'SchemaTree'], add_new_display_features_v0_13_5, ['tree']),
  590. (['UtilityMatrixFromCurve', 'UtilityMatrixCompose'],
  591. up_0_13_5_modes_for_matrix_from_curve_and_compose_matrix,
  592. ['node',], ),
  593. ]
  594. # WORKAROUNDS for bugs should go here:
  595. # 4.5.0 LTS valid socket types bug:
  596. def workaround_4_5_0_interface_update(tree, name, in_out, sock_type, parent_name, do_parent=False):
  597. # TODO: dedupe this code from i_o.py
  598. prRed("There is a bug in Blender 4.5.0 regarding node-group interface sockets. Working around it.")
  599. sock = tree.interface.new_socket(name=name, in_out=in_out, socket_type="NodeSocketGeometry")
  600. import json
  601. interface_helper = {} # initialize it if it is empty
  602. if tree.interface_helper: # may be empty, check here
  603. interface_helper = json.loads(tree.interface_helper)
  604. error_message = error_description_4_5_0_LTS
  605. interface_helper[sock.identifier] = {
  606. 'name' : sock.name,
  607. 'identifier' : sock.identifier,
  608. 'in_out' : sock.in_out,
  609. 'socket_type' : sock_type,
  610. 'bl_socket_idname' : sock_type,
  611. 'mantis_socket_category' : parent_name,
  612. 'description' : error_message,
  613. }
  614. sock.description = error_message # this tells the user why the socket looks weird.
  615. tree.interface_helper = json.dumps(interface_helper)
  616. if do_parent and (parent := tree.interface.items_tree.get(parent_name)):
  617. tree.interface.move_to_parent(
  618. sock,
  619. parent,
  620. 0, # what to do here?
  621. )
  622. return sock
  623. def socket_add_workaround_for_4_5_0_LTS(item, socket_collection, multi):
  624. import json
  625. tree = item.id_data
  626. interface_helper = json.loads(tree.interface_helper)
  627. socket_info = interface_helper.get(item.identifier)
  628. if not socket_info: raise RuntimeError(f"There has been an error adding the socket {item.name}")
  629. s = socket_collection.new(
  630. type=socket_info['bl_socket_idname'],
  631. name=item.name, # in case the user has changed it
  632. identifier=item.identifier,
  633. use_multi_input=multi, )
  634. return s