deformer_containers.py 25 KB

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