i_o.py 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. # this is the I/O part of mantis. I eventually intend to make this a markup language. not right now tho lol
  2. from .utilities import (prRed, prGreen, prPurple, prWhite,
  3. prOrange,
  4. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  5. wrapOrange,)
  6. from mathutils import Vector, Matrix
  7. NODES_REMOVED=["xFormRootNode"]
  8. # Node bl_idname, # Socket Name
  9. SOCKETS_REMOVED=[("UtilityDriverVariable", "Transform Channel"),
  10. ("xFormRootNode","World Out"),
  11. ("UtilitySwitch","xForm"),
  12. ("LinkDrivenParameter", "Enable")]
  13. # Node Class #Prior bl_idname # prior name # new bl_idname # new name, # Multi
  14. # Debugging values.
  15. print_read_only_warning = False
  16. print_link_failure = False
  17. # ignore these because they are either unrelated python stuff or useless or borked
  18. prop_ignore = [ "__dict__", "__doc__", "__module__", "__weakref__",# "name",
  19. "bl_height_default", "bl_height_max", "bl_height_min",
  20. "bl_icon", "bl_rna", "bl_static_type", "bl_description",
  21. "bl_width_default", "bl_width_max", "bl_width_min",
  22. "__annotations__", "original", "rna_type", "view_center",
  23. "links", "nodes", "internal_links", "inputs", "outputs",
  24. "__slots__", "dimensions", "type", "interface",
  25. "library_weak_reference", "parsed_tree", "node_tree_updater",
  26. "asset_data", "preview", # blender asset stuff
  27. "object_reference", # this one is here to hold on to widgets when appending
  28. "color_tag" , # added in blender 4.4, not used by Mantis, readonly.
  29. # more blender properties...
  30. "bl_use_group_interface", "default_group_node_width", "id_type",
  31. # blender runtime stuff
  32. "animation_data", "description", "grease_pencil", "is_editable",
  33. "is_embedded_data", "is_evaluated", "is_library_indirect", "is_missing",
  34. "is_runtime_data", "library", "name_full", "override_library",
  35. "session_uid", "tag", "use_extra_user", "use_fake_user", "users",
  36. # some Mantis stuff I don't need to save
  37. "do_live_update", "is_executing", "is_exporting", "hash", "filepath",
  38. "prevent_next_exec", "execution_id", "num_links", "tree_valid",
  39. "interface_helper",
  40. # node stuff
  41. "mantis_node_class_name", "color", "height", "initialized", "select",
  42. "show_options", "show_preview", "show_texture", "use_custom_color",
  43. "warning_propagation",
  44. # these are in Bone
  45. "socket_count", "display_bb_settings", "display_def_settings",
  46. "display_ik_settings", "display_vp_settings",
  47. ]
  48. # don't ignore: "bl_idname", "bl_label",
  49. # ignore the name, it's the dict - key for the node props
  50. # no that's stupid don't ignore the name good grief
  51. # I am doing this because these are interactions with other addons that cause problems and probably don't exist for any given user
  52. prop_ignore.extend(['keymesh'])
  53. # trees
  54. prop_ignore_tree = prop_ignore.copy()
  55. prop_ignore_tree.extend(["bl_label", "name"])
  56. prop_ignore_interface = prop_ignore.copy()
  57. # Geometry Nodes stuff that Mantis doesn't use
  58. prop_ignore_interface.extend( [ "attribute_domain",
  59. "default_attribute_name",
  60. "default_input",
  61. "force_non_field",
  62. "hide_in_modifier",
  63. "hide_value",
  64. # no idea what this is, also don't care
  65. "is_inspect_output",
  66. "is_panel_toggle",
  67. "layer_selection_field",
  68. "structure_type", ] )
  69. from bpy.app import version
  70. if version >= (4,5,0):
  71. SOCKETS_REMOVED.append( ("LinkSplineIK", "Use Original Scale"))
  72. add_inputs_bl_idnames = [
  73. "UtilityDriver", "UtilityFCurve", "DeformerMorphTargetDeform",
  74. "LinkArmature",
  75. "xFormBoneNode"
  76. # for custom properties, right?
  77. # For a long time this wasn't in here and I guess there weren't problems
  78. # I really don't know if adding it here is right...
  79. ]
  80. # this works but it is really ugly and probably quite inneficient
  81. # TODO: make hotkeys for export and import and reload from file
  82. # we need to give the tree a filepath attribute and update it on saving
  83. # then we need to use the filepath attribute to load from
  84. # finally we need to use a few operators to choose whether to open a menu or not
  85. # and we need a message to display on save/load so that the user knows it is happening
  86. # TODO:
  87. # Additionally export MetaRig and Curve and other referenced data
  88. # Meshes can be exported as .obj and imported via GN
  89. def TellClasses():
  90. return [ MantisExportNodeTreeSaveAs,
  91. MantisExportNodeTreeSave,
  92. MantisExportNodeTree,
  93. MantisImportNodeTree,
  94. MantisImportNodeTreeNoMenu,
  95. MantisReloadNodeTree]
  96. # https://stackoverflow.com/questions/42033142/is-there-an-easy-way-to-check-if-an-object-is-json-serializable-in-python - thanks!
  97. def is_jsonable(x):
  98. import json
  99. try:
  100. json.dumps(x)
  101. return True
  102. except (TypeError, OverflowError):
  103. return False
  104. # https://stackoverflow.com/questions/295135/turn-a-stritree-into-a-valid-filename - thank you user "Sophie Gage"
  105. def remove_special_characters(name):
  106. import re; return re.sub('[^\w_.)( -]', '', name)# re = regular expressions
  107. def fix_custom_parameter(n, property_definition, ):
  108. if n.bl_idname in ['xFormNullNode', 'xFormBoneNode', 'xFormArmatureNode', 'xFormGeometryObjectNode',]:
  109. prop_name = property_definition["name"]
  110. prop_type = property_definition["bl_idname"]
  111. if prop_type in ['ParameterBoolSocket', 'ParameterIntSocket', 'ParameterFloatSocket', 'ParameterVectorSocket' ]:
  112. # is it good to make both of them?
  113. input = n.inputs.new( prop_type, prop_name)
  114. output = n.outputs.new( prop_type, prop_name)
  115. if property_definition["is_output"] == True:
  116. return output
  117. return input
  118. elif n.bl_idname in ['LinkArmature']:
  119. prop_name = property_definition["name"]
  120. prop_type = property_definition["bl_idname"]
  121. input = n.inputs.new( prop_type, prop_name)
  122. return input
  123. return None
  124. # def scan_tree_for_objects(base_tree, current_tree):
  125. # # goal: find all referenced armature and curve objects
  126. # # return [set(armatures), set(curves)]
  127. # armatures = set()
  128. # curves = set()
  129. # for node in base_tree.parsed_tree.values():
  130. # from .utilities import get_node_prototype
  131. # if node.ui_signature is None:
  132. # continue
  133. # ui_node = get_node_prototype(node.ui_signature, node.base_tree)
  134. # if ui_node is None or ui_node.id_data != current_tree:
  135. # continue
  136. # if hasattr(node, "bGetObject"):
  137. # ob = node.bGetObject()
  138. # print(node, ob)
  139. # if ob is None:
  140. # continue
  141. # if not hasattr(node, "type"):
  142. # continue
  143. # if ob.type == 'ARMATURE':
  144. # armatures.add(ob)
  145. # if ob.type == 'CURVE':
  146. # curves.add(ob)
  147. # return (armatures, curves)
  148. # Currently this isn't very robust and doesn't seek backwards
  149. # to see if a dependency is created by node connections.
  150. # TODO it remains to be seen if that is even a desirable behaviour.
  151. def scan_tree_for_objects(base_tree, current_tree):
  152. # this should work
  153. armatures, curves = set(), set()
  154. if current_tree == base_tree:
  155. scan_tree_dependencies(base_tree, curves, armatures,)
  156. return (curves, armatures )
  157. def scan_tree_dependencies(base_tree, curves:set, armatures:set, ):
  158. from .utilities import check_and_add_root
  159. from collections import deque
  160. from bpy import context, data
  161. xForm_pass = deque()
  162. if base_tree.tree_valid:
  163. nodes = base_tree.parsed_tree
  164. else:
  165. base_tree.update_tree(context=context)
  166. nodes = base_tree.parsed_tree
  167. for nc in nodes.values():
  168. nc.reset_execution()
  169. check_and_add_root(nc, xForm_pass)
  170. from .readtree import sort_execution
  171. from .utilities import get_node_prototype
  172. sorted_nodes, execution_failed = sort_execution(nodes, xForm_pass)
  173. if execution_failed:
  174. prRed("Error reading dependencies from tree, skipping")
  175. return curves, armatures
  176. for n in sorted_nodes:
  177. if n.ui_signature is None:
  178. continue # doesn't matter
  179. ui_node = get_node_prototype(n.ui_signature, n.base_tree)
  180. if not ui_node:
  181. continue
  182. # we need to see if it is receiving a Curve
  183. # so check the ui_node if it is a socket that takes an object
  184. for s in ui_node.inputs:
  185. if s.bl_idname in 'EnumCurveSocket':
  186. curve_name = n.evaluate_input(s.name)
  187. if curve := data.objects.get(curve_name):
  188. curves.add(curve)
  189. else:
  190. raise NotImplementedError(curve_name)
  191. match ui_node.bl_idname:
  192. case "UtilityMetaRig":
  193. armature_name = n.evaluate_input("Meta-Armature")
  194. if armature := data.objects.get(armature_name):
  195. armatures.add(armature)
  196. case "InputExistingGeometryObjectNode":
  197. object_name = n.evaluate_input("Name")
  198. if object := data.objects.get(object_name):
  199. if object.type == "ARMATURE":
  200. armatures.add(object)
  201. elif object.type == "CURVE":
  202. curves.add(object)
  203. # Usually we don't want an object that is generated by the tree
  204. # case "xFormArmatureNode":
  205. # armature_name = n.evaluate_input("Name")
  206. # prWhite(armature_name)
  207. # if armature := data.objects.get(armature_name):
  208. # armatures.add(armature)
  209. case "xFormGeometryObjectNode":
  210. object_name = n.evaluate_input("Name")
  211. if object := data.objects.get(object_name):
  212. if object.type == "ARMATURE":
  213. armatures.add(object)
  214. elif object.type == "CURVE":
  215. curves.add(object)
  216. return (curves, armatures )
  217. # TODO move these dataclasses into a new file
  218. from dataclasses import dataclass, field, asdict
  219. # some basic classes to define curve point types
  220. @dataclass
  221. class crv_pnt_data():
  222. co : tuple[float, float, float] = field(default=(0,0,0,))
  223. handle_left : tuple[float, float, float] = field(default=(0,0,0,))
  224. handle_right : tuple[float, float, float] = field(default=(0,0,0,))
  225. handle_left_type : str = field(default="ALIGNED")
  226. handle_right_type : str = field(default="ALIGNED")
  227. radius : float = field(default=0.0)
  228. tilt : float = field(default=0.0)
  229. w : float = field(default=0.0)
  230. @dataclass
  231. class spline_data():
  232. type : str = field(default='POLY')
  233. points : list[dict] = field(default_factory=[])
  234. order_u : int = field(default=4)
  235. radius_interpolation : str = field(default="LINEAR")
  236. tilt_interpolation : str = field(default="LINEAR")
  237. resolution_u : int = field(default=12)
  238. use_bezier_u : bool = field(default=False)
  239. use_cyclic_u : bool = field(default=False)
  240. use_endpoint_u : bool = field(default=False)
  241. index : int = field(default=0)
  242. object_name : str = field(default='Curve')
  243. def get_curve_for_pack(object):
  244. splines = []
  245. for i, spline in enumerate(object.data.splines):
  246. points = []
  247. if spline.type == 'BEZIER':
  248. for point in spline.bezier_points:
  249. export_pnt = crv_pnt_data(
  250. co = tuple(point.co),
  251. radius = point.radius,
  252. tilt = point.tilt,
  253. handle_left = tuple(point.handle_left),
  254. handle_right = tuple(point.handle_right),
  255. handle_left_type = point.handle_left_type,
  256. handle_right_type = point.handle_right_type,
  257. )
  258. points.append(asdict(export_pnt))
  259. else:
  260. for point in spline.points:
  261. export_pnt = crv_pnt_data(
  262. co = point.co[:3], # exclude the w value
  263. radius = point.radius,
  264. tilt = point.tilt,
  265. w = point.co[3],
  266. )
  267. points.append(asdict(export_pnt))
  268. export_spl = spline_data(
  269. type = spline.type,
  270. points = points,
  271. order_u = spline.order_u,
  272. radius_interpolation = spline.radius_interpolation,
  273. tilt_interpolation = spline.tilt_interpolation,
  274. resolution_u = spline.resolution_u,
  275. use_bezier_u = spline.use_bezier_u,
  276. use_cyclic_u = spline.use_cyclic_u,
  277. use_endpoint_u = spline.use_endpoint_u,
  278. index = i,
  279. object_name = object.name,)
  280. splines.append(asdict(export_spl))
  281. return splines
  282. def matrix_as_tuple(matrix):
  283. return ( matrix[0][0], matrix[0][1], matrix[0][2], matrix[0][3],
  284. matrix[1][0], matrix[1][1], matrix[1][2], matrix[1][3],
  285. matrix[2][0], matrix[2][1], matrix[2][2], matrix[2][3],
  286. matrix[3][0], matrix[3][1], matrix[3][2], matrix[3][3], )
  287. @dataclass
  288. class metabone_data:
  289. object_name : str = field(default='')
  290. name : str = field(default=''),
  291. type : str = field(default='BONE'),
  292. matrix : tuple[float] = field(default=()),
  293. parent : str = field(default=''),
  294. length : float = field(default=-1.0),
  295. children : list[str] = field(default_factory=[]),
  296. # keep it really simple for now. I'll add BBone and envelope later on
  297. # when I make them accessible from the meta-rig
  298. def get_armature_for_pack(object):
  299. metarig_data = {}
  300. armature_children = []
  301. for bone in object.data.bones:
  302. parent_name = ''
  303. if bone.parent is None:
  304. armature_children.append(bone.name)
  305. else:
  306. parent_name=bone.parent.name
  307. children=[]
  308. for c in bone.children:
  309. children.append(c.name)
  310. bone_data = metabone_data( object_name = object.name,
  311. name=bone.name, type='BONE',
  312. matrix=matrix_as_tuple(bone.matrix_local),
  313. parent=parent_name, length = bone.length, children = children,
  314. )
  315. metarig_data[bone.name]=asdict(bone_data)
  316. armature_data = metabone_data( object_name = object.name,
  317. name=object.name, type='ARMATURE',
  318. matrix=matrix_as_tuple(object.matrix_world),
  319. parent="", # NOTE that this is not always a fair assumption!
  320. length = -1.0, children = armature_children,)
  321. metarig_data[object.name] = asdict(armature_data)
  322. metarig_data["MANTIS_RESERVED"] = asdict(armature_data) # just in case a bone is named the same as the armature
  323. return metarig_data
  324. def get_socket_data(socket, ignore_if_default=False):
  325. # TODO: don't get stuff in the socket templates
  326. # PROBLEM: I don't have easy access to this from the ui class (or mantis class)
  327. socket_data = {}
  328. socket_data["name"] = socket.name
  329. socket_data["bl_idname"] = socket.bl_idname
  330. socket_data["is_output"] = socket.is_output
  331. socket_data["is_multi_input"] = socket.is_multi_input
  332. # here is where we'll handle a socket_data'socket special data
  333. if socket.bl_idname == "EnumMetaBoneSocket":
  334. socket_data["bone"] = socket.bone
  335. if socket.bl_idname in ["EnumMetaBoneSocket", "EnumMetaRigSocket", "EnumCurveSocket"]:
  336. if sp := socket.get("search_prop"): # may be None
  337. socket_data["search_prop"] = sp.name # this is an object.
  338. #
  339. if hasattr(socket, "default_value"):
  340. value = socket.default_value
  341. else:
  342. value = None
  343. return socket_data # we don't need to store any more.
  344. if not is_jsonable(value): # FIRST try and make a tuple out of it because JSON doesn't like mutables
  345. value = tuple(value)
  346. if not is_jsonable(value): # now see if it worked and crash out if it didn't
  347. raise RuntimeError(f"Error serializing data in {socket.node.name}::{socket.name} for value of type {type(value)}")
  348. socket_data["default_value"] = value
  349. # TODO TODO implement "ignore if default" feature here
  350. # at this point we can get the custom parameter ui hints if we want
  351. if not socket.is_output:
  352. # try and get this data
  353. if value := getattr(socket,'min', None):
  354. socket_data["min"] = value
  355. if value := getattr(socket,'max', None):
  356. socket_data["max"] = value
  357. if value := getattr(socket,'soft_min', None):
  358. socket_data["soft_min"] = value
  359. if value := getattr(socket,'soft_max', None):
  360. socket_data["soft_max"] = value
  361. if value := getattr(socket,'description', None):
  362. socket_data["description"] = value
  363. return socket_data
  364. #
  365. def get_node_data(ui_node):
  366. # if this is a node-group, force it to update its interface, because it may be messed up.
  367. # can remove this HACK when I have stronger guarentees about node-group's keeping the interface
  368. from .base_definitions import node_group_update
  369. if hasattr(ui_node, "node_tree"):
  370. ui_node.is_updating = True
  371. try: # HERE BE DANGER
  372. node_group_update(ui_node, force=True)
  373. finally: # ensure this line is run even if there is an error
  374. ui_node.is_updating = False
  375. node_props, inputs, outputs = {}, {}, {}
  376. node_props["inputs"], node_props['outputs'] = {}, {} # just good to have a default
  377. for propname in dir(ui_node):
  378. value = getattr(ui_node, propname)
  379. if propname in ['fake_fcurve_ob']:
  380. value=value.name
  381. if (propname in prop_ignore) or ( callable(value) ):
  382. continue
  383. if value.__class__.__name__ in ["Vector", "Color"]:
  384. value = tuple(value)
  385. if isinstance(value, bpy.types.NodeTree):
  386. value = value.name
  387. if isinstance(value, bpy.types.bpy_prop_array):
  388. value = tuple(value)
  389. if propname == "parent" and value:
  390. value = value.name
  391. if not is_jsonable(value):
  392. raise RuntimeError(f"Could not export... {ui_node.name}, {propname}, {type(value)}")
  393. if value is None:
  394. continue
  395. node_props[propname] = value
  396. # so we have to accumulate the parent location because the location is not absolute
  397. if propname == "location" and ui_node.parent is not None:
  398. location_acc = Vector((0,0))
  399. parent = ui_node.parent
  400. while (parent):
  401. location_acc += parent.location
  402. parent = parent.parent
  403. location_acc += getattr(ui_node, propname)
  404. node_props[propname] = tuple(location_acc)
  405. # this works!
  406. if ui_node.bl_idname in ['RerouteNode']:
  407. return node_props # we don't need to get the socket information.
  408. for i, ui_socket in enumerate(ui_node.inputs):
  409. socket = get_socket_data(ui_socket)
  410. socket["index"]=i
  411. inputs[ui_socket.identifier] = socket
  412. for i, ui_socket in enumerate(ui_node.outputs):
  413. socket = get_socket_data(ui_socket)
  414. socket["index"]=i
  415. outputs[ui_socket.identifier] = socket
  416. node_props["inputs"] = inputs
  417. node_props["outputs"] = outputs
  418. return node_props
  419. def get_tree_data(tree):
  420. tree_info = {}
  421. for propname in dir(tree):
  422. # if getattr(tree, propname):
  423. # pass
  424. if (propname in prop_ignore_tree) or ( callable(getattr(tree, propname)) ):
  425. continue
  426. v = getattr(tree, propname)
  427. if isinstance(getattr(tree, propname), bpy.types.bpy_prop_array):
  428. v = tuple(getattr(tree, propname))
  429. if not is_jsonable( v ):
  430. raise RuntimeError(f"Not JSON-able: {propname}, type: {type(v)}")
  431. tree_info[propname] = v
  432. tree_info["name"]=tree.name
  433. return tree_info
  434. def get_interface_data(tree, tree_in_out):
  435. for sock in tree.interface.items_tree:
  436. sock_data={}
  437. if sock.item_type == 'PANEL':
  438. sock_data["name"] = sock.name
  439. sock_data["item_type"] = sock.item_type
  440. sock_data["description"] = sock.description
  441. sock_data["default_closed"] = sock.default_closed
  442. tree_in_out[sock.name] = sock_data
  443. # if it is a socket....
  444. else:
  445. # we need to get the socket class from the bl_idname
  446. bl_socket_idname = sock.bl_socket_idname
  447. # try and import it
  448. from . import socket_definitions
  449. # some mistakes in versioning makes it possible for older trees to have
  450. # standard Blender types instead of Mantis types
  451. if bl_socket_idname == 'NodeSocketFloat':
  452. bl_socket_idname = 'FloatSocket'
  453. elif bl_socket_idname == 'NodeSocketVector':
  454. bl_socket_idname = 'VectorSocket'
  455. elif bl_socket_idname == 'NodeSocketInt':
  456. bl_socket_idname = 'IntSocket'
  457. try:
  458. socket_class = getattr(socket_definitions, bl_socket_idname)
  459. except AttributeError: # sometimes the class doesn't work.
  460. # I think this happens because of an oversight in versioning. Sorry.
  461. socket_class = getattr(socket_definitions, "FloatSocket")
  462. prRed(f"Cannot export interface socket of type {bl_socket_idname}.\n"
  463. f"See interface socket: {sock.name} in tree {tree.name}.\n"
  464. f"Try to change the socket type to a Mantis type.\n"
  465. f"Exporting a FloatSocket instead. This will not alter Mantis' "
  466. f"usability or behavior.")
  467. sock_parent = None
  468. if sock.parent:
  469. sock_parent = sock.parent.name
  470. for propname in dir(sock):
  471. if propname in prop_ignore_interface:
  472. continue
  473. if (propname == "parent"):
  474. sock_data[propname] = sock_parent
  475. continue
  476. v = getattr(sock, propname)
  477. if (propname in prop_ignore) or ( callable(v) ):
  478. continue
  479. if isinstance(getattr(sock, propname), bpy.types.bpy_prop_array):
  480. v = tuple(getattr(sock, propname))
  481. if not is_jsonable( v ):
  482. raise RuntimeError(f"{propname}, {type(v)}")
  483. sock_data[propname] = v
  484. # this is a property. pain.
  485. from bpy.types import NodeSocketColor
  486. if hasattr(sock, "interface_type"):
  487. sock_data["socket_type"] = socket_class.interface_type.fget(socket_class)
  488. else:
  489. sock_data["socket_type"] = sock.bl_socket_idname
  490. tree_in_out[sock.identifier] = sock_data
  491. def export_to_json(trees, base_tree=None, path="", write_file=True, only_selected=False, ):
  492. export_data = {}
  493. for tree in trees:
  494. current_tree_is_base_tree = False
  495. if tree is trees[-1]:
  496. current_tree_is_base_tree = True
  497. tree_info, tree_in_out = {}, {}
  498. tree_info = get_tree_data(tree)
  499. curves, metarig_data = {}, {}
  500. embed_metarigs=True
  501. if base_tree and embed_metarigs:
  502. curves_in_tree, metarigs_in_tree = scan_tree_for_objects(base_tree, tree)
  503. for crv in curves_in_tree:
  504. curves[crv.name] = get_curve_for_pack(crv)
  505. for mr in metarigs_in_tree:
  506. metarig_data[mr.name] = get_armature_for_pack(mr)
  507. # if only_selected:
  508. # # all in/out links, relative to the selection, should be marked and used to initialize tree properties
  509. if not only_selected: # we'll handle this later with the links
  510. for sock in tree.interface.items_tree:
  511. get_interface_data(tree, tree_in_out) # it concerns me that this one modifies
  512. # the collection instead of getting the data and returning it. TODO refactor this
  513. nodes = {}
  514. for node in tree.nodes:
  515. if only_selected and node.select == False:
  516. continue
  517. nodes[node.name] = get_node_data(node)
  518. links = []
  519. in_sockets, out_sockets = {}, {}
  520. unique_sockets_from, unique_sockets_to = {}, {}
  521. # TODO BUG HACK BAD UGLY WRONG this code should NOT be isolated from the node generation code!
  522. # in the future, try to use dataclasses to enforce defaults and such. then as_dict() or whatever
  523. in_node = {"name":"MANTIS_AUTOGEN_GROUP_INPUT", "bl_idname":"NodeGroupInput", "inputs":in_sockets, "outputs":{}}
  524. out_node = {"name":"MANTIS_AUTOGEN_GROUP_OUTPUT", "bl_idname":"NodeGroupOutput", "inputs":{}, "outputs":out_sockets}
  525. add_input_node, add_output_node = False, False
  526. for link in tree.links:
  527. from_node_name, from_socket_id = link.from_node.name, link.from_socket.identifier
  528. to_node_name, to_socket_id = link.to_node.name, link.to_socket.identifier
  529. from_socket_name, to_socket_name = link.from_socket.name, link.to_socket.name
  530. # get the indices of the sockets to be absolutely sure
  531. for from_output_index, outp in enumerate(link.from_node.outputs):
  532. # for some reason, 'is' does not return True no matter what...
  533. # so we are gonn compare the memory address directly, this is stupid
  534. if (outp.as_pointer() == link.from_socket.as_pointer()): break
  535. else:
  536. problem=link.from_node.name + "::" + link.from_socket.name
  537. raise RuntimeError(wrapRed(f"Error saving index of socket: {problem}"))
  538. for to_input_index, inp in enumerate(link.to_node.inputs):
  539. if (inp.as_pointer() == link.to_socket.as_pointer()): break
  540. else:
  541. problem = link.to_node.name + "::" + link.to_socket.name
  542. raise RuntimeError(wrapRed(f"Error saving index of socket: {problem}"))
  543. if current_tree_is_base_tree:
  544. if (only_selected and link.from_node.select) and (not link.to_node.select):
  545. # handle an output in the tree
  546. add_output_node=True
  547. if not (sock_name := unique_sockets_to.get(link.from_socket.node.name+link.from_socket.identifier)):
  548. sock_name = link.to_socket.name; name_stub = sock_name
  549. used_names = list(tree_in_out.keys()); i=0
  550. while sock_name in used_names:
  551. sock_name=name_stub+'.'+str(i).zfill(3); i+=1
  552. unique_sockets_to[link.from_socket.node.name+link.from_socket.identifier]=sock_name
  553. out_sock = out_sockets.get(sock_name)
  554. if not out_sock:
  555. out_sock = {}; out_sockets[sock_name] = out_sock
  556. out_sock["index"]=len(out_sockets) # zero indexed, so zero length makes zero the first index and so on, this works
  557. # what in the bad word is happening here?
  558. # why?
  559. # why no de-duplication?
  560. # what was I thinking?
  561. # TODO REFACTOR THIS SOON
  562. out_sock["name"] = sock_name
  563. out_sock["identifier"] = sock_name
  564. out_sock["bl_idname"] = link.to_socket.bl_idname
  565. out_sock["is_output"] = False
  566. out_sock["source"]=[link.to_socket.node.name,link.to_socket.identifier]
  567. out_sock["is_multi_input"] = False # this is not something I can even set on tree interface items, and this code is not intended for making Schema
  568. sock_data={}
  569. sock_data["name"] = sock_name
  570. sock_data["item_type"] = "SOCKET"
  571. sock_data["default_closed"] = False
  572. # record the actual bl_idname and the proper interface type.
  573. if hasattr(link.from_socket, "interface_type"):
  574. sock_data["socket_type"] = link.from_socket.interface_type
  575. else:
  576. sock_data["socket_type"] = link.from_socket.bl_idname
  577. sock_data["bl_socket_idname"] = link.from_socket.bl_idname
  578. sock_data["identifier"] = sock_name
  579. sock_data["in_out"]="OUTPUT"
  580. sock_data["index"]=out_sock["index"]
  581. tree_in_out[sock_name] = sock_data
  582. to_node_name=out_node["name"]
  583. to_socket_id=out_sock["identifier"]
  584. to_input_index=out_sock["index"]
  585. to_socket_name=out_sock["name"]
  586. elif (only_selected and (not link.from_node.select)) and link.to_node.select:
  587. add_input_node=True
  588. # we need to get a unique name for this
  589. # use the Tree IN/Out because we are dealing with Group in/out
  590. if not (sock_name := unique_sockets_from.get(link.from_socket.node.name+link.from_socket.identifier)):
  591. sock_name = link.from_socket.name; name_stub = sock_name
  592. used_names = list(tree_in_out.keys()); i=0
  593. while sock_name in used_names:
  594. sock_name=name_stub+'.'+str(i).zfill(3); i+=1
  595. unique_sockets_from[link.from_socket.node.name+link.from_socket.identifier]=sock_name
  596. in_sock = in_sockets.get(sock_name)
  597. if not in_sock:
  598. in_sock = {}; in_sockets[sock_name] = in_sock
  599. in_sock["index"]=len(in_sockets) # zero indexed, so zero length makes zero the first index and so on, this works
  600. #
  601. in_sock["name"] = sock_name
  602. in_sock["identifier"] = sock_name
  603. in_sock["bl_idname"] = link.from_socket.bl_idname
  604. in_sock["is_output"] = True
  605. in_sock["is_multi_input"] = False # this is not something I can even set on tree interface items, and this code is not intended for making Schema
  606. in_sock["source"] = [link.from_socket.node.name,link.from_socket.identifier]
  607. sock_data={}
  608. sock_data["name"] = sock_name
  609. sock_data["item_type"] = "SOCKET"
  610. sock_data["default_closed"] = False
  611. # record the actual bl_idname and the proper interface type.
  612. if hasattr(link.from_socket, "interface_type"):
  613. sock_data["socket_type"] = link.from_socket.interface_type
  614. else:
  615. sock_data["socket_type"] = link.from_socket.bl_idname
  616. sock_data["bl_socket_idname"] = link.from_socket.bl_idname
  617. sock_data["identifier"] = sock_name
  618. sock_data["in_out"]="INPUT"
  619. sock_data["index"]=in_sock["index"]
  620. tree_in_out[sock_name] = sock_data
  621. from_node_name=in_node.get("name")
  622. from_socket_id=in_sock["identifier"]
  623. from_output_index=in_sock["index"]
  624. from_socket_name=in_node.get("name")
  625. # parentheses matter here...
  626. elif (only_selected and not (link.from_node.select and link.to_node.select)):
  627. continue
  628. elif only_selected and not (link.from_node.select and link.to_node.select):
  629. continue # pass if both links are not selected
  630. links.append( (from_node_name,
  631. from_socket_id,
  632. to_node_name,
  633. to_socket_id,
  634. from_output_index,
  635. to_input_index,
  636. from_socket_name,
  637. to_socket_name) ) # it's a tuple
  638. if add_input_node or add_output_node:
  639. all_nodes_bounding_box=[Vector((float("inf"),float("inf"))), Vector((-float("inf"),-float("inf")))]
  640. for n in nodes.values():
  641. if n["location"][0] < all_nodes_bounding_box[0].x:
  642. all_nodes_bounding_box[0].x = n["location"][0]
  643. if n["location"][1] < all_nodes_bounding_box[0].y:
  644. all_nodes_bounding_box[0].y = n["location"][1]
  645. #
  646. if n["location"][0] > all_nodes_bounding_box[1].x:
  647. all_nodes_bounding_box[1].x = n["location"][0]
  648. if n["location"][1] > all_nodes_bounding_box[1].y:
  649. all_nodes_bounding_box[1].y = n["location"][1]
  650. if add_input_node:
  651. in_node["location"] = Vector((all_nodes_bounding_box[0].x-400, all_nodes_bounding_box[0].lerp(all_nodes_bounding_box[1], 0.5).y))
  652. nodes["MANTIS_AUTOGEN_GROUP_INPUT"]=in_node
  653. if add_output_node:
  654. out_node["location"] = Vector((all_nodes_bounding_box[1].x+400, all_nodes_bounding_box[0].lerp(all_nodes_bounding_box[1], 0.5).y))
  655. nodes["MANTIS_AUTOGEN_GROUP_OUTPUT"]=out_node
  656. export_data[tree.name] = (tree_info, tree_in_out, nodes, links, curves, metarig_data,) # f_curves)
  657. return export_data
  658. def write_json_data(data, path):
  659. import json
  660. with open(path, "w") as file:
  661. print(wrapWhite("Writing mantis tree data to: "), wrapGreen(file.name))
  662. file.write( json.dumps(data, indent = 4) )
  663. def get_link_sockets(link, tree, tree_socket_id_map):
  664. from_node_name = link[0]
  665. from_socket_id = link[1]
  666. to_node_name = link[2]
  667. to_socket_id = link[3]
  668. from_output_index = link[4]
  669. to_input_index = link[5]
  670. from_socket_name = link[6]
  671. to_socket_name = link[7]
  672. # TODO: make this a loop and swap out the in/out stuff
  673. # this is OK but I want to avoid code-duplication, which this almost is.
  674. from_node = tree.nodes.get(from_node_name)
  675. # first try and get by name. we'll use this if the ID and the name do not match.
  676. # from_sock = from_node.outputs.get(from_socket_name)
  677. id1 = from_socket_id
  678. if hasattr(from_node, "node_tree") or \
  679. from_node.bl_idname in ["SchemaArrayInput",
  680. "SchemaArrayInputGet",
  681. "SchemaArrayInputAll",
  682. "SchemaConstInput",
  683. "SchemaIncomingConnection", ]: # now we have to map by something else
  684. try:
  685. id1 = from_node.outputs[from_socket_name].identifier
  686. except KeyError: # we'll try index if nothing else works
  687. try:
  688. id1 = from_node.outputs[from_output_index].identifier
  689. except IndexError as e:
  690. prRed("failed to create link: "
  691. f"{from_node_name}:{from_socket_id} --> {to_node_name}:{to_socket_id}")
  692. return (None, None)
  693. elif from_node.bl_idname in ["NodeGroupInput"]:
  694. id1 = tree_socket_id_map.get(from_socket_id)
  695. for from_sock in from_node.outputs:
  696. if from_sock.identifier == id1: break
  697. else:
  698. from_sock = None
  699. id2 = to_socket_id
  700. to_node = tree.nodes[to_node_name]
  701. if hasattr(to_node, "node_tree") or \
  702. to_node.bl_idname in ["SchemaArrayOutput",
  703. "SchemaConstOutput",
  704. "SchemaOutgoingConnection", ]: # now we have to map by something else
  705. try:
  706. id2 = to_node.inputs[to_socket_name].identifier
  707. except KeyError: # we'll try index if nothing else works
  708. try: # nesting try/except is ugly but it is right...
  709. id2 = to_node.inputs[to_input_index].identifier
  710. except IndexError as e:
  711. prRed("failed to create link: "
  712. f"{from_node_name}:{from_socket_id} --> {to_node_name}:{to_socket_id}")
  713. return (from_sock, None)
  714. elif to_node.bl_idname in ["NodeGroupOutput"]:
  715. id2 = tree_socket_id_map.get(to_socket_id)
  716. for to_sock in to_node.inputs:
  717. if to_sock.identifier == id2: break
  718. else:
  719. to_sock = None
  720. return from_sock, to_sock
  721. def setup_sockets(node, propslist, in_out="inputs"):
  722. sockets_removed = []
  723. socket = None
  724. for i, (s_id, s_val) in enumerate(propslist[in_out].items()):
  725. if node.bl_idname in ['NodeReroute']:
  726. break # Reroute Nodes do not have anything I can set or modify.
  727. for socket_removed in SOCKETS_REMOVED:
  728. if node.bl_idname == socket_removed[0] and s_id == socket_removed[1]:
  729. prWhite(f"INFO: Ignoring import of socket {s_id}; it has been removed.")
  730. sockets_removed.append(s_val["index"])
  731. sockets_removed.sort()
  732. continue
  733. if s_val["is_output"]:
  734. if node.bl_idname in "MantisSchemaGroup":
  735. node.is_updating = True
  736. try:
  737. socket = node.outputs.new(s_val["bl_idname"], s_val["name"], identifier=s_id)
  738. finally:
  739. node.is_updating=False
  740. elif s_val["index"] >= len(node.outputs):
  741. if node.bl_idname in add_inputs_bl_idnames:
  742. socket = node.outputs.new(s_val["bl_idname"], s_val["name"], identifier=s_id, )
  743. else: # first try to get by ID AND name. ID's switch around a bit so we need both to match.
  744. for socket in node.outputs:
  745. if socket.identifier == s_id and socket.name == s_val['name']:
  746. break
  747. # this often fails for group outputs and such
  748. # because the socket ID may not be the same when it is re-generated
  749. else: # otherwise try to get the index
  750. # IT IS NOT CLEAR but this is what throws the index error below BAD
  751. # try to get by name
  752. socket = node.outputs.get(s_val['name'])
  753. if not socket:
  754. try:
  755. socket = node.outputs[int(s_val["index"])]
  756. except IndexError as e:
  757. print (node.id_data.name)
  758. print (propslist['name'])
  759. print (s_id, s_val['name'], s_val['index'])
  760. raise e
  761. if socket.name != s_val["name"]:
  762. right_name = s_val['name']
  763. prRed( "There has been an error getting a socket while importing data."
  764. f"found name: {socket.name}; should have found: {right_name}.")
  765. else:
  766. for removed_index in sockets_removed:
  767. if s_val["index"] > removed_index:
  768. s_val["index"]-=1
  769. if s_val["index"] >= len(node.inputs):
  770. if node.bl_idname in add_inputs_bl_idnames:
  771. socket = node.inputs.new(s_val["bl_idname"], s_val["name"], identifier=s_id, use_multi_input=s_val["is_multi_input"])
  772. elif node.bl_idname in ["MantisSchemaGroup"]:
  773. node.is_updating = True
  774. try:
  775. socket = node.inputs.new(s_val["bl_idname"], s_val["name"], identifier=s_id, use_multi_input=s_val["is_multi_input"])
  776. finally:
  777. node.is_updating=False
  778. elif node.bl_idname in ["NodeGroupOutput"]:
  779. pass # this is dealt with separately
  780. else:
  781. prWhite("Not found: ", propslist['name'], s_val["name"], s_id)
  782. prRed("Index: ", s_val["index"], "Number of inputs", len(node.inputs))
  783. for thing1, thing2 in zip(propslist[in_out].keys(), getattr(node, in_out).keys()):
  784. print (thing1, thing2)
  785. raise NotImplementedError(wrapRed(f"{node.bl_idname} in {node.id_data.name} needs to be handled in JSON load."))
  786. else: # first try to get by ID AND name. ID's switch around a bit so we need both to match.
  787. for socket in node.inputs:
  788. if socket.identifier == s_id and socket.name == s_val['name']:
  789. break
  790. # failing to find the socket by ID is less common for inputs than outputs.
  791. # it usually isn't a problem.
  792. else: # otherwise try to get the index
  793. # IT IS NOT CLEAR but this is what throws the index error below BAD
  794. socket = node.inputs.get(s_val["name"])
  795. if not socket:
  796. socket = node.inputs[int(s_val["index"])]
  797. # finally we need to check that the name matches.
  798. if socket.name != s_val["name"]:
  799. right_name = s_val['name']
  800. prRed( "There has been an error getting a socket while importing data."
  801. f"found name: {socket.name}; should have found: {right_name}.")
  802. if socket is None:
  803. # Error?
  804. return
  805. # set the value
  806. for s_p, s_v in s_val.items():
  807. if s_p not in ["default_value"]:
  808. if s_p == "search_prop" and node.bl_idname == 'UtilityMetaRig':
  809. socket.node.armature= s_v
  810. socket.search_prop=bpy.data.objects.get(s_v)
  811. if s_p == "search_prop" and node.bl_idname in ['UtilityMatrixFromCurve', 'UtilityMatricesFromCurve']:
  812. socket.search_prop=bpy.data.objects.get(s_v)
  813. elif s_p == "bone" and socket.bl_idname == 'EnumMetaBoneSocket':
  814. socket.bone = s_v
  815. socket.node.pose_bone = s_v
  816. continue # not editable and NOT SAFE
  817. #
  818. if socket.bl_idname in ["BooleanThreeTupleSocket"]:
  819. value = bool(s_v[0]), bool(s_v[1]), bool(s_v[2]),
  820. s_v = value
  821. try:
  822. setattr(socket, s_p , s_v)
  823. except TypeError as e:
  824. prRed("Can't set socket due to type mismatch: ", node.name, socket.name, s_p, s_v)
  825. # raise e
  826. except ValueError as e:
  827. prRed("Can't set socket due to type mismatch: ", node.name, socket.name, s_p, s_v)
  828. # raise e
  829. except AttributeError as e:
  830. if print_read_only_warning == True:
  831. prWhite("Tried to write a read-only property, ignoring...")
  832. prWhite(f"{socket.node.name}[{socket.name}].{s_p} is read only, cannot set value to {s_v}")
  833. def do_import_from_file(filepath, context):
  834. import json
  835. all_trees = [n_tree for n_tree in bpy.data.node_groups if n_tree.bl_idname in ["MantisTree", "SchemaTree"]]
  836. for tree in all_trees:
  837. tree.is_exporting = True
  838. tree.do_live_update = False
  839. def do_cleanup(tree):
  840. tree.is_exporting = False
  841. tree.do_live_update = True
  842. tree.prevent_next_exec = True
  843. with open(filepath, 'r', encoding='utf-8') as f:
  844. data = json.load(f)
  845. do_import(data,context, search_multi_files=True, filepath=filepath)
  846. for tree in all_trees:
  847. do_cleanup(tree)
  848. tree = bpy.data.node_groups[list(data.keys())[-1]]
  849. try:
  850. context.space_data.node_tree = tree
  851. except AttributeError: # not hovering over the Node Editor
  852. pass
  853. return {'FINISHED'}
  854. # otherwise:
  855. # repeat this because we left the with, this is bad and ugly but I don't care
  856. for tree in all_trees:
  857. do_cleanup(tree)
  858. return {'CANCELLED'}
  859. # TODO figure out the right way to dedupe this stuff (see above)
  860. # I need this function for recursing through multi-file components
  861. # but I am using the with statement in the above function...
  862. # it should be easy to refactor but I don't know 100% for sure
  863. # the behaviour will be identical or if that matters.
  864. def get_graph_data_from_json(filepath) -> dict:
  865. import json
  866. with open(filepath, 'r', encoding='utf-8') as f:
  867. data = json.load(f)
  868. return data
  869. def do_import(data, context, search_multi_files=False, filepath='', skip_existing=False):
  870. trees = []
  871. tree_sock_id_maps = {}
  872. skip_trees = set()
  873. # First: init the interface of the node graph
  874. for tree_name, tree_data in data.items():
  875. tree_info = tree_data[0]
  876. tree_in_out = tree_data[1]
  877. print (tree_info)
  878. # TODO: IMPORT THIS DATA HERE!!!
  879. try:
  880. curves = tree_data[4]
  881. armatures = tree_data[5]
  882. except IndexError: # shouldn't happen but maybe someone has an old file
  883. curves = {}
  884. armatures = {}
  885. for curve_name, curve_data in curves.items():
  886. from .utilities import import_curve_data_to_object, import_metarig_data
  887. import_curve_data_to_object(curve_name, curve_data)
  888. for armature_name, armature_data in armatures.items():
  889. import_metarig_data(armature_data)
  890. # need to make a new tree; first, try to get it:
  891. tree = bpy.data.node_groups.get(tree_info["name"])
  892. if tree and skip_existing:
  893. skip_trees.add(tree.name)
  894. continue # already done here because the tree already exists.
  895. if tree is None:
  896. tree = bpy.data.node_groups.new(tree_info["name"], tree_info["bl_idname"])
  897. tree.mantis_version = tree_info['mantis_version']
  898. tree.nodes.clear(); tree.links.clear(); tree.interface.clear()
  899. # this may be a bad bad thing to do without some kind of warning TODO TODO
  900. tree.is_executing = True
  901. tree.do_live_update = False
  902. trees.append(tree)
  903. tree_sock_id_map = {}
  904. tree_sock_id_maps[tree.name] = tree_sock_id_map
  905. interface_parent_me = {}
  906. # I need to guarantee that the interface items are in the right order.
  907. interface_sockets = [] # I'll just sort them afterwards so I hold them here.
  908. default_position=0 # We'll use this if the position attribute is not set when e.g. making groups.
  909. for s_name, s_props in tree_in_out.items():
  910. if s_props["item_type"] == 'SOCKET':
  911. if s_props["socket_type"] == "LayerMaskSocket":
  912. continue
  913. if (socket_type := s_props["socket_type"]) == "NodeSocketColor":
  914. socket_type = "ColorSetSocket"
  915. if bpy.app.version != (4,5,0):
  916. sock = tree.interface.new_socket(s_props["name"], in_out=s_props["in_out"], socket_type=socket_type)
  917. else: # blender 4.5.0 LTS, have to workaround a bug!
  918. from .versioning import workaround_4_5_0_interface_update
  919. sock = workaround_4_5_0_interface_update(tree=tree, name=s_props["name"], in_out=s_props["in_out"],
  920. sock_type=socket_type, parent_name=s_props.get("parent", ''))
  921. tree_sock_id_map[s_name] = sock.identifier
  922. if not (socket_position := s_props.get('position')):
  923. socket_position=default_position; default_position+=1
  924. interface_sockets.append( (sock, socket_position) )
  925. # TODO: set whatever properties are needed (default, etc)
  926. if panel := s_props.get("parent"): # this get is just to maintain compatibility with an older form of this script... and it is harmless
  927. interface_parent_me[sock] = (panel, s_props["position"])
  928. else: # it's a panel
  929. panel = tree.interface.new_panel(s_props["name"], description=s_props.get("description"), default_closed=s_props.get("default_closed"))
  930. for socket, (panel, index) in interface_parent_me.items():
  931. tree.interface.move_to_parent(
  932. socket,
  933. tree.interface.items_tree.get(panel),
  934. index,
  935. )
  936. # BUG this was screwing up the order of things
  937. # so I want to fix it and re-enable it
  938. if True:
  939. # Go BACK through and set the index/position now that all items exist.
  940. interface_sockets.sort(key=lambda a : a[1])
  941. for (socket, position) in interface_sockets:
  942. tree.interface.move(socket, position)
  943. # Now go and do nodes and links
  944. for tree_name, tree_data in data.items():
  945. if tree_name in skip_trees:
  946. continue
  947. print ("Importing sub-graph: %s with %s nodes" % (wrapGreen(tree_name), wrapPurple(len(tree_data[2]))) )
  948. tree_info = tree_data[0]
  949. nodes = tree_data[2]
  950. links = tree_data[3]
  951. parent_me = []
  952. tree = bpy.data.node_groups.get(tree_info["name"])
  953. tree.is_executing = True
  954. tree.do_live_update = False
  955. trees.append(tree)
  956. tree_sock_id_map=tree_sock_id_maps[tree.name]
  957. interface_parent_me = {}
  958. # from mantis.utilities import prRed, prWhite, prOrange, prGreen
  959. for name, propslist in nodes.items():
  960. bl_idname = propslist["bl_idname"]
  961. do_socket_setup = True
  962. if bl_idname in NODES_REMOVED:
  963. prWhite(f"INFO: Ignoring import of node {name} of type {bl_idname}; it has been removed.")
  964. continue
  965. n = tree.nodes.new(bl_idname)
  966. if bl_idname in ["DeformerMorphTargetDeform"]:
  967. n.inputs.remove(n.inputs[-1]) # get rid of the wildcard
  968. elif bl_idname in ['UtilityDeclareCollections']:
  969. n.collection_declarations = propslist['collection_declarations']
  970. n.update_interface()
  971. do_socket_setup = False
  972. elif bl_idname in ['InputColorSetPallete']:
  973. # because the user can add and remove sockets, we need to match the names.
  974. # since the user's sockets could be 000, 001, 003, 004 or something like
  975. socket_names = list(propslist['outputs'].keys())
  976. for i in range(len(propslist['outputs'])):
  977. socket = n.outputs.new("ColorSetSocket", socket_names[i] )
  978. color_values = propslist['outputs'][socket.name]['default_value']
  979. socket.active_color = color_values[:3]
  980. socket.normal_color = color_values[3:6]
  981. socket.selected_color = color_values[6:9]
  982. do_socket_setup = False
  983. elif bl_idname in [ "SchemaArrayInput",
  984. "SchemaArrayInputGet",
  985. "SchemaArrayInputAll",
  986. "SchemaArrayOutput",
  987. "SchemaConstInput",
  988. "SchemaConstOutput",
  989. "SchemaOutgoingConnection",
  990. "SchemaIncomingConnection",]:
  991. n.update()
  992. if sub_tree := propslist.get("node_tree"):
  993. # now that I am doing multi-file exports, this is tricky
  994. # we need to see if the tree exists and if not, recurse
  995. # and import that tree before continuing.
  996. grp_tree = bpy.data.node_groups.get(sub_tree)
  997. if grp_tree is None: # for multi-file component this is intentional
  998. if search_multi_files: # we'll get the filename and recurse
  999. from bpy.path import native_pathsep, clean_name
  1000. from os import path as os_path
  1001. native_filepath = native_pathsep(filepath)
  1002. directory = os_path.split(native_filepath)[0]
  1003. subtree_filepath = os_path.join(directory, clean_name(sub_tree)+'.rig')
  1004. subtree_data = get_graph_data_from_json(subtree_filepath)
  1005. do_import(subtree_data, context,
  1006. search_multi_files=True,
  1007. filepath=subtree_filepath)
  1008. #now get the grp_tree lol
  1009. grp_tree = bpy.data.node_groups[sub_tree]
  1010. else: # otherwise it is an error
  1011. raise RuntimeError(f"Tree {sub_tree} not available to import.")
  1012. n.node_tree = grp_tree
  1013. from .base_definitions import node_group_update
  1014. n.is_updating = True
  1015. try:
  1016. node_group_update(n, force = True)
  1017. finally:
  1018. n.is_updating=False
  1019. # set up sockets
  1020. if do_socket_setup:
  1021. setup_sockets(n, propslist, in_out="inputs")
  1022. setup_sockets(n, propslist, in_out="outputs")
  1023. for p, v in propslist.items():
  1024. if p in ["node_tree",
  1025. "sockets",
  1026. "inputs",
  1027. "outputs",
  1028. "warning_propagation",
  1029. "socket_idname"]:
  1030. continue
  1031. # will throw AttributeError if read-only
  1032. # will throw TypeError if wrong type...
  1033. if n.bl_idname == "NodeFrame" and p in ["width, height, location"]:
  1034. continue
  1035. if version < (4,4,0) and p == 'location_absolute':
  1036. continue
  1037. if p == "parent" and v is not None:
  1038. parent_me.append( (n.name, v) )
  1039. v = None # for now) #TODO
  1040. try:
  1041. setattr(n, p, v)
  1042. except Exception as e:
  1043. prRed (p)
  1044. raise e
  1045. for l in links:
  1046. from_socket_name = l[6]
  1047. to_socket_name = l[7]
  1048. name1=l[0]
  1049. name2=l[2]
  1050. from_sock, to_sock = get_link_sockets(l, tree, tree_sock_id_map)
  1051. try:
  1052. link = tree.links.new(from_sock, to_sock)
  1053. except TypeError:
  1054. prPurple (from_sock)
  1055. prOrange (to_sock)
  1056. if print_link_failure:
  1057. from_node_name = l[0]; from_socket_id = l[1]
  1058. to_node_name = l[2]; to_socket_id = l[3]
  1059. prWhite(f"looking for... {from_node_name}:{from_socket_id}, {to_node_name}:{to_socket_id}")
  1060. prRed (f"Failed: {l[0]}:{l[1]} --> {l[2]}:{l[3]}")
  1061. prRed (f" got node: {from_node_name}, {to_node_name}")
  1062. prRed (f" got socket: {from_sock}, {to_sock}")
  1063. prWhite(f"Failed to add link in {tree.name}: {name1}:{from_socket_name}, {name2}:{to_socket_name}")
  1064. raise RuntimeError
  1065. else:
  1066. prRed(f"Failed to add link in {tree.name}: {name1}:{from_socket_name}, {name2}:{to_socket_name}")
  1067. # if at this point it doesn't work... we need to fix
  1068. for name, p in parent_me:
  1069. if (n := tree.nodes.get(name)) and (p := tree.nodes.get(p)):
  1070. n.parent = p
  1071. # otherwise the frame node is missing because it was not included in the data e.g. when grouping nodes.
  1072. tree.is_executing = False
  1073. tree.do_live_update = True
  1074. def export_multi_file(trees : list, base_tree, filepath : str, base_name :str) -> None:
  1075. for t in trees:
  1076. # this should name them the name of the tree...
  1077. from bpy.path import native_pathsep, clean_name
  1078. from os import path as os_path
  1079. from os import mkdir
  1080. native_filepath = native_pathsep(filepath)
  1081. directory = os_path.split(native_filepath)[0]
  1082. export_data = export_to_json([t], base_tree, os_path.join(directory,
  1083. clean_name(t.name)+'.rig'))
  1084. write_json_data(export_data, os_path.join(directory,
  1085. clean_name(t.name)+'.rig'))
  1086. import bpy
  1087. from bpy_extras.io_utils import ImportHelper, ExportHelper
  1088. from bpy.props import StringProperty, BoolProperty, EnumProperty
  1089. from bpy.types import Operator
  1090. # Save As
  1091. class MantisExportNodeTreeSaveAs(Operator, ExportHelper):
  1092. """Export a Mantis Node Tree by filename."""
  1093. bl_idname = "mantis.export_save_as"
  1094. bl_label = "Export Mantis Tree as ...(.rig)"
  1095. # ExportHelper mix-in class uses this.
  1096. filename_ext = ".rig"
  1097. filter_glob: StringProperty(
  1098. default="*.rig",
  1099. options={'HIDDEN'},
  1100. maxlen=255, # Max internal buffer length, longer would be clamped.
  1101. )
  1102. export_trees_together : BoolProperty(
  1103. default=False,
  1104. name="Pack All Sub-Trees",
  1105. description="Pack all Sub-trees into one file?")
  1106. @classmethod
  1107. def poll(cls, context):
  1108. return hasattr(context.space_data, 'path')
  1109. def execute(self, context):
  1110. # we need to get the dependent trees from self.tree...
  1111. # there is no self.tree
  1112. # how do I choose a tree?
  1113. base_tree=context.space_data.path[-1].node_tree
  1114. from .utilities import all_trees_in_tree
  1115. trees = all_trees_in_tree(base_tree)[::-1]
  1116. prGreen("Exporting node graph with dependencies...")
  1117. for t in trees:
  1118. prGreen ("Node graph: \"%s\"" % (t.name))
  1119. base_tree.is_exporting = True
  1120. if self.export_trees_together:
  1121. export_data = export_to_json(trees, base_tree, self.filepath)
  1122. write_json_data(export_data, self.filepath)
  1123. else:
  1124. export_multi_file(trees, base_tree, self.filepath, base_tree.name)
  1125. base_tree.is_exporting = False
  1126. base_tree.prevent_next_exec = True
  1127. # set the properties on the base tree for re-exporting with alt-s
  1128. base_tree.filepath = self.filepath
  1129. base_tree.export_all_subtrees_together = self.export_trees_together
  1130. return {'FINISHED'}
  1131. # Save
  1132. class MantisExportNodeTreeSave(Operator):
  1133. """Save a Mantis Node Tree to disk."""
  1134. bl_idname = "mantis.export_save"
  1135. bl_label = "Export Mantis Tree (.rig)"
  1136. @classmethod
  1137. def poll(cls, context):
  1138. return hasattr(context.space_data, 'path')
  1139. def execute(self, context):
  1140. base_tree=context.space_data.path[-1].node_tree
  1141. filepath = base_tree.filepath
  1142. from .utilities import all_trees_in_tree
  1143. trees = all_trees_in_tree(base_tree)[::-1]
  1144. prGreen("Exporting node graph with dependencies...")
  1145. for t in trees:
  1146. prGreen ("Node graph: \"%s\"" % (t.name))
  1147. base_tree.is_exporting = True
  1148. if base_tree.export_all_subtrees_together:
  1149. export_data = export_to_json(trees, base_tree, filepath)
  1150. write_json_data(export_data, filepath)
  1151. else:
  1152. export_multi_file(trees, base_tree, filepath, base_tree.name)
  1153. base_tree.is_exporting = False
  1154. base_tree.prevent_next_exec = True
  1155. return {'FINISHED'}
  1156. # Save Choose:
  1157. class MantisExportNodeTree(Operator):
  1158. """Save a Mantis Node Tree to disk."""
  1159. bl_idname = "mantis.export_save_choose"
  1160. bl_label = "Export Mantis Tree (.rig)"
  1161. @classmethod
  1162. def poll(cls, context):
  1163. return hasattr(context.space_data, 'path')
  1164. def execute(self, context):
  1165. base_tree=context.space_data.path[-1].node_tree
  1166. if base_tree.filepath:
  1167. prRed(base_tree.filepath)
  1168. return bpy.ops.mantis.export_save()
  1169. else:
  1170. return bpy.ops.mantis.export_save_as('INVOKE_DEFAULT')
  1171. # here is what needs to be done...
  1172. # - modify this to work with a sort of parsed-tree instead (sort of)
  1173. # - this needs to treat each sub-graph on its own
  1174. # - is this a problem? do I need to reconsider how I treat the graph data in mantis?
  1175. # - I should learn functional programming / currying
  1176. # - then the parsed-tree this builds must be executed as Blender nodes
  1177. # - I think... this is not important right now. not yet.
  1178. # - KEEP IT SIMPLE, STUPID
  1179. class MantisImportNodeTree(Operator, ImportHelper):
  1180. """Import a Mantis Node Tree."""
  1181. bl_idname = "mantis.import_tree"
  1182. bl_label = "Import Mantis Tree (.rig)"
  1183. # ImportHelper mixin class uses this
  1184. filename_ext = ".rig"
  1185. filter_glob : StringProperty(
  1186. default="*.rig",
  1187. options={'HIDDEN'},
  1188. maxlen=255, # Max internal buffer length, longer would be clamped.
  1189. )
  1190. def execute(self, context):
  1191. import cProfile
  1192. from os import environ
  1193. do_profile=False
  1194. if environ.get("DOPROFILE"):
  1195. do_profile=True
  1196. pass_error = True
  1197. if do_profile:
  1198. import pstats, io
  1199. from pstats import SortKey
  1200. with cProfile.Profile() as pr:
  1201. return_value = do_import_from_file(self.filepath, context)
  1202. s = io.StringIO()
  1203. sortby = SortKey.TIME
  1204. # sortby = SortKey.CUMULATIVE
  1205. ps = pstats.Stats(pr, stream=s).strip_dirs().sort_stats(sortby)
  1206. ps.print_stats(20) # print the top 20
  1207. print(s.getvalue())
  1208. return return_value
  1209. else:
  1210. return do_import_from_file(self.filepath, context)
  1211. class MantisImportNodeTreeNoMenu(Operator):
  1212. """Import a Mantis Node Tree."""
  1213. bl_idname = "mantis.import_tree_no_menu"
  1214. bl_label = "Import Mantis Tree (.rig)"
  1215. filepath : StringProperty()
  1216. def execute(self, context):
  1217. return do_import_from_file(self.filepath, context)
  1218. # this is useful:
  1219. # https://blender.stackexchange.com/questions/73286/how-to-call-a-confirmation-dialog-box
  1220. # class MantisReloadConfirmMenu(bpy.types.Panel):
  1221. # bl_label = "Confirm?"
  1222. # bl_idname = "OBJECT_MT_mantis_reload_confirm"
  1223. # def draw(self, context):
  1224. # layout = self.layout
  1225. # layout.operator("mantis.reload_tree")
  1226. class MantisReloadNodeTree(Operator):
  1227. # """Import a Mantis Node Tree."""
  1228. # bl_idname = "mantis.reload_tree"
  1229. # bl_label = "Import Mantis Tree"
  1230. """Reload Mantis Tree"""
  1231. bl_idname = "mantis.reload_tree"
  1232. bl_label = "Confirm reload tree?"
  1233. bl_options = {'REGISTER', 'INTERNAL'}
  1234. @classmethod
  1235. def poll(cls, context):
  1236. if hasattr(context.space_data, 'path'):
  1237. return True
  1238. return False
  1239. def invoke(self, context, event):
  1240. return context.window_manager.invoke_confirm(self, event)
  1241. def execute(self, context):
  1242. base_tree=context.space_data.path[-1].node_tree
  1243. if not base_tree.filepath:
  1244. self.report({'ERROR'}, "Tree has not been saved - so it cannot be reloaded.")
  1245. return {'CANCELLED'}
  1246. self.report({'INFO'}, "reloading tree")
  1247. return do_import_from_file(base_tree.filepath, context)
  1248. # todo:
  1249. # - export metarig and option to import it
  1250. # - same with controls
  1251. # - it would be nice to have a library of these that can be imported alongside the mantis graph