versioning.py 33 KB

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