deformer_containers.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. from .node_container_common import *
  2. from .xForm_containers import xFormGeometryObject
  3. from .misc_containers import InputExistingGeometryObject
  4. from bpy.types import Node
  5. from .base_definitions import MantisNode
  6. from .utilities import (prRed, prGreen, prPurple, prWhite, prOrange,
  7. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  8. wrapOrange,)
  9. def TellClasses():
  10. return [
  11. DeformerArmature,
  12. DeformerHook,
  13. DeformerMorphTarget,
  14. DeformerMorphTargetDeform,
  15. ]
  16. def trace_xForm_back(nc, socket):
  17. from .xForm_containers import xFormGeometryObject
  18. from .misc_containers import InputExistingGeometryObject
  19. from bpy.types import Object
  20. if (trace := trace_single_line(nc, socket)[0] ) :
  21. for i in range(len(trace)): # have to look in reverse, actually
  22. if ( isinstance(trace[ i ], xFormGeometryObject ) ) or ( isinstance(trace[ i ], InputExistingGeometryObject ) ):
  23. return trace[ i ].bGetObject()
  24. raise GraphError(wrapRed(f"No other object found for {nc}."))
  25. def default_evaluate_input(nc, input_name):
  26. # duped from link_containers... should be common?
  27. # should catch 'Target', 'Pole Target' and ArmatureConstraint targets, too
  28. if ('Target' in input_name) and input_name != "Target Space":
  29. socket = nc.inputs.get(input_name)
  30. if socket.is_linked:
  31. return socket.links[0].from_node
  32. return None
  33. else:
  34. return evaluate_input(nc, input_name)
  35. # semi-duplicated from link_containers
  36. def GetxForm(nc):
  37. trace = trace_single_line_up(nc, "Deformer")
  38. for node in trace[0]:
  39. if (node.__class__ in [xFormGeometryObject, InputExistingGeometryObject]):
  40. return node
  41. raise GraphError("%s is not connected to a downstream xForm" % nc)
  42. class DeformerArmature:
  43. '''A node representing an armature deformer'''
  44. def __init__(self, signature, base_tree):
  45. self.base_tree=base_tree
  46. self.signature = signature
  47. self.inputs = {
  48. "Input Relationship" : NodeSocket(is_input = True, name = "Input Relationship", node = self,),
  49. "Armature Object" : NodeSocket(is_input = True, name = "Armature Object", node = self,),
  50. "Blend Vertex Group" : NodeSocket(is_input = True, name = "Blend Vertex Group", node = self),
  51. "Invert Vertex Group" : NodeSocket(is_input = True, name = "Invert Vertex Group", node = self),
  52. "Preserve Volume" : NodeSocket(is_input = True, name = "Preserve Volume", node = self),
  53. "Use Multi Modifier" : NodeSocket(is_input = True, name = "Use Multi Modifier", node = self),
  54. "Use Envelopes" : NodeSocket(is_input = True, name = "Use Envelopes", node = self),
  55. "Use Vertex Groups" : NodeSocket(is_input = True, name = "Use Vertex Groups", node = self),
  56. "Skinning Method" : NodeSocket(is_input = True, name = "Skinning Method", node = self),
  57. "Deformer" : NodeSocket(is_input = True, name = "Deformer", node = self),
  58. "Copy Skin Weights From" : NodeSocket(is_input = True, name = "Copy Skin Weights From", node = self),
  59. }
  60. self.outputs = {
  61. "Deformer" : NodeSocket(is_input = False, name = "Deformer", node=self), }
  62. self.parameters = {
  63. "Name" : None,
  64. "Armature Object" : None,
  65. "Blend Vertex Group" : None,
  66. "Invert Vertex Group" : None,
  67. "Preserve Volume" : None,
  68. "Use Multi Modifier" : None,
  69. "Use Envelopes" : None,
  70. "Use Vertex Groups" : None,
  71. "Skinning Method" : None,
  72. "Deformer" : None,
  73. "Copy Skin Weights From" : None,
  74. }
  75. # now set up the traverse target...
  76. self.inputs["Deformer"].set_traverse_target(self.outputs["Deformer"])
  77. self.outputs["Deformer"].set_traverse_target(self.inputs["Deformer"])
  78. self.node_type = "LINK"
  79. self.hierarchy_connections, self.connections = [], []
  80. self.hierarchy_dependencies, self.dependencies = [], []
  81. self.prepared = True
  82. self.executed = False
  83. def evaluate_input(self, input_name):
  84. return default_evaluate_input(self, input_name)
  85. def GetxForm(self, socket="Deformer"):
  86. if socket == "Deformer":
  87. return GetxForm(self)
  88. else:
  89. trace_xForm_back(self, socket)
  90. # DUPLICATED FROM xForm_containers::xFormBone
  91. # DEDUP HACK HACK HACK HACK HACK
  92. def bGetParentArmature(self):
  93. from .xForm_containers import xFormArmature
  94. from .misc_containers import InputExistingGeometryObject
  95. from bpy.types import Object
  96. if (trace := trace_single_line(self, "Armature Object")[0] ) :
  97. for i in range(len(trace)):
  98. # have to look in reverse, actually
  99. if ( isinstance(trace[ i ], xFormArmature ) ):
  100. return trace[ i ].bGetObject()
  101. elif ( isinstance(trace[i], InputExistingGeometryObject)):
  102. if (ob := trace[i].bGetObject()).type == "ARMATURE":
  103. return ob
  104. raise RuntimeError(f"Cannot find armature for node {self}")
  105. return None
  106. #should do the trick...
  107. def bExecute(self, bContext = None,):
  108. self.executed = True
  109. def initialize_vgroups(self,):
  110. ob = self.GetxForm().bGetObject()
  111. armOb = self.bGetParentArmature()
  112. for b in armOb.data.bones:
  113. if b.use_deform == False:
  114. continue
  115. vg = ob.vertex_groups.get(b.name)
  116. if not vg:
  117. vg = ob.vertex_groups.new(name=b.name)
  118. num_verts = len(ob.data.vertices)
  119. vg.add(range(num_verts), 0, 'REPLACE')
  120. def copy_weights(self):
  121. # we'll use modifiers for this, maybe use GN for it in the future tho
  122. import bpy
  123. ob = self.GetxForm().bGetObject()
  124. try:
  125. copy_from = self.GetxForm(socket="Copy Skin Weights From")
  126. except GraphError:
  127. copy_from = None
  128. prRed(f"No object found for copying weights in {self}, continuing anyway.")
  129. m = ob.modifiers.new(type="DATA_TRANSFER", name="Mantis_temp_data_transfer")
  130. m.object = None; m.use_vert_data = True
  131. m.data_types_verts = {'VGROUP_WEIGHTS'}
  132. m.vert_mapping = 'POLYINTERP_NEAREST'
  133. m.layers_vgroup_select_src = 'ALL'
  134. m.layers_vgroup_select_dst = 'NAME'
  135. m.object = copy_from
  136. # m.use_object_transform = False # testing reveals that this is undesirable - since the objects may not have their transforms applied.
  137. ob.modifiers.move(len(ob.modifiers)-1, 0)
  138. # ob.data = ob.data.copy()
  139. if False: #MAYBE the mouse needs to be in the 3D viewport, no idea how to set this in an override
  140. # TODO: figure out how to apply this, context is incorrect because armature is still in pose mode
  141. original_active = bpy.context.active_object
  142. original_mode = original_active.mode
  143. bpy.ops.object.mode_set(mode='OBJECT')
  144. with bpy.context.temp_override(**{'active_object':ob, 'selected_objects':[ob, copy_from]}):
  145. # bpy.ops.object.datalayout_transfer(modifier=m.name) # note: this operator is used by the modifier or stand-alone in the UI
  146. # the poll for this operator is defined in blender/source/blender/editors/object/object_data_transfer.cc
  147. # and blender/source/blender/editors/object/object_modifier.cc
  148. # bpy.ops.object.modifier_apply(modifier=m.name, single_user=True)
  149. bpy.ops.object.datalayout_transfer(data_type='VGROUP_WEIGHTS')
  150. bpy.ops.object.data_transfer(data_type='VGROUP_WEIGHTS')
  151. bpy.ops.object.mode_set(mode=original_mode)
  152. def bFinalize(self, bContext=None):
  153. prGreen("Executing Armature Deform Node")
  154. mod_name = self.evaluate_input("Name")
  155. d = self.GetxForm().bGetObject().modifiers.new(mod_name, type='ARMATURE')
  156. if d is None:
  157. raise RuntimeError(f"Modifier was not created in node {self} -- the object is invalid.")
  158. self.bObject = d
  159. d.object = self.bGetParentArmature()
  160. props_sockets = {
  161. 'vertex_group' : ("Blend Vertex Group", ""),
  162. 'invert_vertex_group' : ("Invert Vertex Group", ""),
  163. 'use_deform_preserve_volume' : ("Preserve Volume", False),
  164. 'use_multi_modifier' : ("Use Multi Modifier", False),
  165. 'use_bone_envelopes' : ("Use Envelopes", False),
  166. 'use_vertex_groups' : ("Use Vertex Groups", False),
  167. }
  168. evaluate_sockets(self, d, props_sockets)
  169. #
  170. if (skin_method := self.evaluate_input("Skinning Method")) == "AUTOMATIC_HEAT":
  171. # This is bad and leads to somewhat unpredictable
  172. # behaviour, e.g. what object will be selected? What mode?
  173. # also bpy.ops is ugly and prone to error when used in
  174. # scripts. I don't intend to use bpy.ops when I can avoid it.
  175. import bpy
  176. self.initialize_vgroups()
  177. bContext.view_layer.depsgraph.update()
  178. ob = self.GetxForm().bGetObject()
  179. armOb = self.bGetParentArmature()
  180. deform_bones = []
  181. for pb in armOb.pose.bones:
  182. if pb.bone.use_deform == True:
  183. deform_bones.append(pb)
  184. context_override = {
  185. 'active_object':ob,
  186. 'selected_objects':[ob, armOb],
  187. 'active_pose_bone':deform_bones[0],
  188. 'selected_pose_bones':deform_bones,}
  189. #
  190. with bContext.temp_override(**{'active_object':armOb}):
  191. bpy.ops.object.mode_set(mode='POSE')
  192. bpy.ops.pose.select_all(action='SELECT')
  193. with bContext.temp_override(**context_override):
  194. bpy.ops.paint.weight_paint_toggle()
  195. bpy.ops.paint.weight_from_bones(type='AUTOMATIC')
  196. bpy.ops.paint.weight_paint_toggle()
  197. #
  198. with bContext.temp_override(**{'active_object':armOb}):
  199. bpy.ops.object.mode_set(mode='POSE')
  200. bpy.ops.pose.select_all(action='DESELECT')
  201. bpy.ops.object.mode_set(mode='OBJECT')
  202. # TODO: modify Blender to make this available as a Python API function.
  203. elif skin_method == "EXISTING_GROUPS":
  204. pass
  205. elif skin_method == "COPY_FROM_OBJECT":
  206. self.initialize_vgroups()
  207. self.copy_weights()
  208. class DeformerHook:
  209. '''A node representing a hook deformer'''
  210. def __init__(self, signature, base_tree):
  211. self.base_tree=base_tree
  212. self.signature = signature
  213. self.inputs = {
  214. "Hook Target" : NodeSocket(is_input = True, name = "Hook Target", node = self,),
  215. "Index" : NodeSocket(is_input = True, name = "Index", node = self),
  216. "Deformer" : NodeSocket(is_input = True, name = "Deformer", node = self),
  217. }
  218. self.outputs = {
  219. "Deformer" : NodeSocket(is_input = False, name = "Deformer", node=self), }
  220. self.parameters = {
  221. "Hook Target" : None,
  222. "Index" : None,
  223. "Deformer" : None,
  224. "Name" : None,
  225. }
  226. # now set up the traverse target...
  227. self.inputs["Deformer"].set_traverse_target(self.outputs["Deformer"])
  228. self.outputs["Deformer"].set_traverse_target(self.inputs["Deformer"])
  229. self.node_type = "LINK"
  230. self.hierarchy_connections, self.connections = [], []
  231. self.hierarchy_dependencies, self.dependencies = [], []
  232. self.prepared = True
  233. self.executed = False
  234. def evaluate_input(self, input_name):
  235. return default_evaluate_input(self, input_name)
  236. def GetxForm(self, socket="Deformer"):
  237. if socket == "Deformer":
  238. return GetxForm(self)
  239. else:
  240. trace_xForm_back(self, socket)
  241. def bExecute(self, bContext = None,):
  242. self.executed = True
  243. def bFinalize(self, bContext=None):
  244. from bpy.types import Bone, PoseBone, Object
  245. prGreen(f"Executing Hook Deform Node: {self}")
  246. mod_name = self.evaluate_input("Name")
  247. target_node = self.evaluate_input('Hook Target')
  248. target = target_node.bGetObject(); subtarget = ""
  249. if isinstance(target, Bone) or isinstance(target, PoseBone):
  250. subtarget = target.name; target = target.id_data
  251. ob=self.GetxForm().bGetObject()
  252. reuse = False
  253. for m in ob.modifiers:
  254. if m.type == 'HOOK' and m.object == target and m.subtarget == subtarget:
  255. d = m; reuse = True; break
  256. else:
  257. d = ob.modifiers.new(mod_name, type='HOOK')
  258. if d is None:
  259. raise RuntimeError(f"Modifier was not created in node {self} -- the object is invalid.")
  260. get_target_and_subtarget(self, d, input_name="Hook Target")
  261. vertices_used=[]
  262. if reuse: # Get the verts in the list... filter out all the unneeded 0's
  263. vertices_used = list(d.vertex_indices)
  264. include_0 = 0 in vertices_used
  265. vertices_used = list(filter(lambda a : a != 0, vertices_used))
  266. if include_0: vertices_used.append(0)
  267. # now we add the selected vertex to the list, too
  268. vertices_used.append(self.evaluate_input("Index"))
  269. d.vertex_indices_set(vertices_used)
  270. # todo: this should be able to take many indices in the future.
  271. class DeformerMorphTarget:
  272. '''A node representing an armature deformer'''
  273. def __init__(self, signature, base_tree):
  274. self.base_tree=base_tree
  275. self.signature = signature
  276. self.inputs = {
  277. "Relative to" : NodeSocket(is_input = True, name = "Relative To", node = self,),
  278. "Object" : NodeSocket(is_input = True, name = "Object", node = self,),
  279. "Deformer" : NodeSocket(is_input = True, name = "Deformer", node = self),
  280. "Vertex Group" : NodeSocket(is_input = True, name = "Vertex Group", node = self),
  281. }
  282. self.outputs = {
  283. "Deformer" : NodeSocket(is_input = False, name = "Deformer", node=self),
  284. "Morph Target" : NodeSocket(is_input = False, name = "Morph Target", node=self), }
  285. self.parameters = {
  286. "Name" : None,
  287. "Relative to" : None,
  288. "Object" : None,
  289. "Morph Target" : None,
  290. "Deformer" : None,
  291. "Vertex Group" : None,
  292. }
  293. # now set up the traverse target...
  294. self.inputs["Deformer"].set_traverse_target(self.outputs["Deformer"])
  295. self.outputs["Deformer"].set_traverse_target(self.inputs["Deformer"])
  296. self.node_type = "LINK"
  297. self.hierarchy_connections, self.connections = [], []
  298. self.hierarchy_dependencies, self.dependencies = [], []
  299. self.prepared = True
  300. self.executed = False
  301. def GetxForm(self, trace_input="Object"):
  302. trace = trace_single_line(self, trace_input)
  303. for node in trace[0]:
  304. if (node.__class__ in [xFormGeometryObject, InputExistingGeometryObject]):
  305. return node
  306. raise GraphError("%s is not connected to an upstream xForm" % self)
  307. def bExecute(self, bContext = None,):
  308. prGreen("Executing Morph Target Node")
  309. ob = None; relative = None
  310. # do NOT check if the object exists here. Just let the next node deal with that.
  311. try:
  312. ob = self.GetxForm().bGetObject().name
  313. except Exception as e: # this will and should throw an error if it fails
  314. ob = self.GetxForm().evaluate_input("Name")
  315. if self.inputs["Relative to"].is_linked:
  316. try:
  317. relative = self.GetxForm("Relative to").bGetObject().name
  318. except Exception as e: # same here
  319. prRed(f"Execution failed at {self}: no relative object found for morph target, despite link existing.")
  320. raise e
  321. vg = self.evaluate_input("Vertex Group") if self.evaluate_input("Vertex Group") else "" # just make sure it is a string
  322. mt={"object":ob, "vertex_group":vg, "relative_shape":relative}
  323. self.parameters["Morph Target"] = mt
  324. self.parameters["Name"] = ob # this is redundant but it's OK since accessing the mt is tedious
  325. self.executed = True
  326. class DeformerMorphTargetDeform:
  327. '''A node representing an armature deformer'''
  328. def __init__(self, signature, base_tree):
  329. self.base_tree=base_tree
  330. self.signature = signature
  331. self.inputs = {
  332. "Deformer" : NodeSocket(is_input = True, name = "Deformer", node = self),
  333. "Use Shape Key" : NodeSocket(is_input = True, name = "Use Shape Key", node = self),
  334. }
  335. self.outputs = {
  336. "Deformer" : NodeSocket(is_input = False, name = "Deformer", node=self), }
  337. self.parameters = {
  338. "Name" : None,
  339. "Deformer" : None,
  340. "Deformer" : None,
  341. "Use Shape Key" : None,}
  342. # now set up the traverse target...
  343. self.inputs["Deformer"].set_traverse_target(self.outputs["Deformer"])
  344. self.outputs["Deformer"].set_traverse_target(self.inputs["Deformer"])
  345. self.node_type = "LINK"
  346. self.hierarchy_connections, self.connections = [], []
  347. self.hierarchy_dependencies, self.dependencies = [], []
  348. self.prepared = True
  349. self.executed = True
  350. self.bObject = None
  351. setup_custom_props(self)
  352. def GetxForm(self):
  353. return GetxForm(self)
  354. # bpy.data.node_groups["Morph Deform.045"].nodes["Named Attribute.020"].data_type = 'FLOAT_VECTOR'
  355. # bpy.context.object.add_rest_position_attribute = True
  356. def gen_morph_target_modifier(self, context):
  357. # first let's see if this is a no-op
  358. targets = []
  359. for k,v in self.inputs.items():
  360. if "Target" in k:
  361. targets.append(v)
  362. if not targets:
  363. return # nothing to do here.
  364. mod_name = self.evaluate_input("Name")
  365. self_ob = self.GetxForm().bGetObject()
  366. m = self_ob.modifiers.new(mod_name, type='NODES')
  367. self.bObject = m
  368. # at this point we make the node tree
  369. self_ob.add_rest_position_attribute = True
  370. from bpy import data
  371. ng = data.node_groups.new(mod_name, "GeometryNodeTree")
  372. m.node_group = ng
  373. ng.interface.new_socket("Geometry", in_out="INPUT", socket_type="NodeSocketGeometry")
  374. ng.interface.new_socket("Geometry", in_out="OUTPUT", socket_type="NodeSocketGeometry")
  375. inp = ng.nodes.new("NodeGroupInput")
  376. out = ng.nodes.new("NodeGroupOutput")
  377. # TODO CLEANUP here
  378. if (position := ng.nodes.get("Position")) is None: position = ng.nodes.new("GeometryNodeInputPosition")
  379. if (index := ng.nodes.get("Index")) is None: index = ng.nodes.new("GeometryNodeInputIndex")
  380. rest_position = ng.nodes.new("GeometryNodeInputNamedAttribute")
  381. rest_position.inputs["Name"].default_value="rest_position"
  382. rest_position.data_type = 'FLOAT_VECTOR'
  383. # rest_position = position
  384. add_these = []
  385. props_sockets={}
  386. object_map = {}
  387. for i, t in enumerate(targets):
  388. mt_node = t.links[0].from_node
  389. mt_ob = mt_node.GetxForm().bGetObject()
  390. if mt_ob is None: # create it
  391. mt_ob = data.objects.new(mt_node.evaluate_input("Name"), data.meshes.new_from_object(self_ob))
  392. context.collection.objects.link(mt_ob)
  393. prOrange(f"WARN: no object found for f{mt_node}; creating duplicate of current object ")
  394. mt_name = mt_ob.name
  395. vg = mt_node.parameters["Morph Target"]["vertex_group"]
  396. if vg: mt_name = mt_name+"."+vg
  397. try:
  398. ob_relative = t.links[0].from_node.inputs["Relative to"].links[0].from_node.bGetObject()
  399. except IndexError:
  400. ob_relative = None
  401. ng.interface.new_socket(mt_name, in_out = "INPUT", socket_type="NodeSocketObject")
  402. ng.interface.new_socket(mt_name+" Value", in_out = "INPUT", socket_type="NodeSocketFloat")
  403. ob_node = ng.nodes.new("GeometryNodeObjectInfo")
  404. sample_index = ng.nodes.new("GeometryNodeSampleIndex"); sample_index.data_type = 'FLOAT_VECTOR'
  405. subtract = ng.nodes.new("ShaderNodeVectorMath"); subtract.operation="SUBTRACT"
  406. scale1 = ng.nodes.new("ShaderNodeVectorMath"); scale1.operation="SCALE"
  407. ng.links.new(input=inp.outputs[mt_name], output=ob_node.inputs["Object"])
  408. ng.links.new(input=index.outputs["Index"], output=sample_index.inputs["Index"])
  409. ng.links.new(input=position.outputs["Position"], output=sample_index.inputs["Value"])
  410. ng.links.new(input=sample_index.outputs["Value"], output=subtract.inputs[0])
  411. ng.links.new(input=ob_node.outputs["Geometry"], output=sample_index.inputs["Geometry"])
  412. if ob_relative: # TODO: this should also be exposed as an input
  413. ob_node1 = ng.nodes.new("GeometryNodeObjectInfo"); ob_node1.inputs["Object"].default_value = ob_relative
  414. sample_index1 = ng.nodes.new("GeometryNodeSampleIndex"); sample_index1.data_type = 'FLOAT_VECTOR'
  415. ng.links.new(input=index.outputs["Index"], output=sample_index1.inputs["Index"])
  416. ng.links.new(input=position.outputs["Position"], output=sample_index1.inputs["Value"])
  417. ng.links.new(input=ob_node1.outputs["Geometry"], output=sample_index1.inputs["Geometry"])
  418. ng.links.new(input=sample_index1.outputs["Value"], output=subtract.inputs[1])
  419. else:
  420. # ng.links.new(input=rest_position.outputs["Attribute"], output=subtract.inputs[1])
  421. ng.links.new(input=rest_position.outputs[0], output=subtract.inputs[1])
  422. ng.links.new(input=subtract.outputs["Vector"], output=scale1.inputs[0])
  423. # TODO: this should be exposed as a node tree input
  424. if vg:= mt_node.evaluate_input("Vertex Group"): # works
  425. vg_att = ng.nodes.new("GeometryNodeInputNamedAttribute"); vg_att.inputs["Name"].default_value=vg
  426. multiply = ng.nodes.new("ShaderNodeMath"); multiply.operation = "MULTIPLY"
  427. ng.links.new(input=vg_att.outputs["Attribute"], output=multiply.inputs[1])
  428. ng.links.new(input=inp.outputs[mt_name+" Value"], output=multiply.inputs[0])
  429. ng.links.new(input=multiply.outputs[0], output=scale1.inputs["Scale"])
  430. else:
  431. ng.links.new(input=inp.outputs[mt_name+" Value"], output=scale1.inputs["Scale"])
  432. add_these.append(scale1)
  433. object_map["Socket_"+str((i+1)*2)]=mt_node.GetxForm().bGetObject()
  434. props_sockets["Socket_"+str((i+1)*2+1)]= ("Value."+str(i).zfill(3), 1.0)
  435. set_position = ng.nodes.new("GeometryNodeSetPosition")
  436. bake = ng.nodes.new("GeometryNodeBake")
  437. ng.links.new(inp.outputs["Geometry"], output=set_position.inputs["Geometry"])
  438. ng.links.new(set_position.outputs["Geometry"], output=bake.inputs[0])
  439. ng.links.new(bake.outputs[0], output=out.inputs[0])
  440. # prev_node = ng.nodes.new("ShaderNodeVectorMath"); prev_node.operation="SUBTRACT"
  441. # ng.links.new(position.outputs[0], output=prev_node.inputs[0])
  442. # ng.links.new(rest_position.outputs[0], output=prev_node.inputs[1])
  443. prev_node = ng.nodes.new("FunctionNodeInputVector")
  444. for i, node in enumerate(add_these):
  445. add = ng.nodes.new("ShaderNodeVectorMath"); add.operation="ADD"
  446. ng.links.new(prev_node.outputs[0], output=add.inputs[0])
  447. ng.links.new(node.outputs[0], output=add.inputs[1])
  448. prev_node = add
  449. ng.links.new(add.outputs[0], output=set_position.inputs["Offset"])
  450. from .utilities import SugiyamaGraph
  451. SugiyamaGraph(ng, 12)
  452. evaluate_sockets(self, m, props_sockets)
  453. for socket, ob in object_map.items():
  454. m[socket]=ob
  455. finish_drivers(self)
  456. def gen_shape_key(self, context): # TODO: make this a feature of the node definition that appears only when there are no prior deformers - and shows a warning!
  457. # TODO: the below works well, but it is quite slow. It does not seem to have better performence. Its only advantage is export to FBX.
  458. # there are a number of things I need to fix here
  459. # - reuse shape keys if possible
  460. # - figure out how to make this a lot faster
  461. # - edit the xForm stuff to delete drivers from shape key ID's, since they belong to the Key, not the Object.
  462. # first check if we need to do anythign
  463. targets = []
  464. for k,v in self.inputs.items():
  465. if "Target" in k:
  466. targets.append(v)
  467. if not targets:
  468. return # nothing to do here
  469. from time import time
  470. start_time = time()
  471. from bpy import data
  472. xf = self.GetxForm()
  473. ob = xf.bGetObject()
  474. dg = context.view_layer.depsgraph
  475. dg.update()
  476. if xf.has_shape_keys == False:
  477. m = data.meshes.new_from_object(ob, preserve_all_data_layers=True, depsgraph=dg)
  478. ob.data = m
  479. ob.add_rest_position_attribute = True
  480. ob.shape_key_clear()
  481. ob.shape_key_add(name='Basis', from_mix=False)
  482. else:
  483. m = ob.data
  484. xf.has_shape_keys = True
  485. # using the built-in shapekey feature is actually a lot harder in terms of programming because I need...
  486. # min/max, as it is just not a feature of the GN version
  487. # to carry info from the morph target node regarding relative shapes and vertex groups and all that
  488. # the drivers may be more difficult to apply, too.
  489. # hafta make new geometry for the object and add shape keys and all that
  490. # the benefit to all this being exporting to game engines via .fbx
  491. # first make a basis shape key
  492. keys={}
  493. props_sockets={}
  494. for i, t in enumerate(targets):
  495. mt_node = t.links[0].from_node; sk_ob = mt_node.GetxForm().bGetObject()
  496. if sk_ob is None:
  497. sk_ob = data.objects.new(mt_node.evaluate_input("Name"), data.meshes.new_from_object(ob))
  498. context.collection.objects.link(sk_ob)
  499. prOrange(f"WARN: no object found for f{mt_node}; creating duplicate of current object ")
  500. sk_ob = dg.id_eval_get(sk_ob)
  501. mt_name = sk_ob.name
  502. vg = mt_node.parameters["Morph Target"]["vertex_group"]
  503. if vg: mt_name = mt_name+"."+vg
  504. sk = ob.shape_key_add(name=mt_name, from_mix=False)
  505. # the shapekey data is absolute point data for each vertex, in order, very simple
  506. # SERIOUSLY IMPORTANT:
  507. # use the current position of the vertex AFTER SHAPE KEYS AND DEFORMERS
  508. # easiest way to do it is to eval the depsgraph
  509. # TODO: try and get it without depsgraph update, since that may be (very) slow
  510. sk_m = sk_ob.data#data.meshes.new_from_object(sk_ob, preserve_all_data_layers=True, depsgraph=dg)
  511. for j in range(len(m.vertices)):
  512. sk.data[j].co = sk_m.vertices[j].co # assume they match
  513. # data.meshes.remove(sk_m)
  514. sk.vertex_group = vg
  515. sk.slider_min = -10
  516. sk.slider_max = 10
  517. keys[mt_name]=sk
  518. props_sockets[mt_name]= ("Value."+str(i).zfill(3), 1.0)
  519. for i, t in enumerate(targets):
  520. mt_node = t.links[0].from_node; sk_ob = mt_node.GetxForm().bGetObject()
  521. if sk_ob is None: continue
  522. if rel := mt_node.parameters["Morph Target"]["relative_shape"]:
  523. sk = keys.get(mt_name)
  524. sk.relative_key = keys.get(rel)
  525. self.bObject = sk.id_data
  526. evaluate_sockets(self, sk.id_data, props_sockets)
  527. finish_drivers(self)
  528. prWhite(f"Initializing morph target took {time() -start_time} seconds")
  529. def bFinalize(self, bContext=None):
  530. prGreen(f"Executing Morph Deform node {self}")
  531. # if there is a not a prior deformer then there should be an option to use plain 'ol shape keys
  532. # GN is always desirable as an option though because it can be baked & many other reasons
  533. use_shape_keys = self.evaluate_input("Use Shape Key")
  534. if use_shape_keys: # check and see if we can.
  535. if (links := self.inputs["Deformer"].links):
  536. if not links[0].from_node.inputs.get("Use Shape Key"):
  537. use_shape_keys = False
  538. elif links[0].from_node.parameters.get("Use Shape Key") == False:
  539. use_shape_keys = False
  540. self.parameters["Use Shape Key"] = use_shape_keys
  541. if use_shape_keys:
  542. self.gen_shape_key(bContext)
  543. else:
  544. self.gen_morph_target_modifier(bContext)
  545. for c in TellClasses():
  546. setup_container(c)