link_nodes.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. from .node_container_common import *
  2. from bpy.types import Bone, NodeTree
  3. from .base_definitions import MantisNode, GraphError, FLOAT_EPSILON
  4. from .link_socket_templates import *
  5. def TellClasses():
  6. return [
  7. # special
  8. LinkInherit,
  9. # copy
  10. LinkCopyLocation,
  11. LinkCopyRotation,
  12. LinkCopyScale,
  13. LinkCopyTransforms,
  14. LinkTransformation,
  15. # limit
  16. LinkLimitLocation,
  17. LinkLimitRotation,
  18. LinkLimitScale,
  19. LinkLimitDistance,
  20. # tracking
  21. LinkStretchTo,
  22. LinkDampedTrack,
  23. LinkLockedTrack,
  24. LinkTrackTo,
  25. #misc
  26. LinkInheritConstraint,
  27. LinkArmature,
  28. # IK
  29. LinkInverseKinematics,
  30. LinkSplineIK,
  31. # stuff that snaps or limits a bone
  32. LinkFloor,
  33. LinkShrinkWrap,
  34. # Drivers
  35. LinkDrivenParameter,
  36. ]
  37. # set the name if it is available, otherwise just use the constraint's nice name
  38. set_constraint_name = lambda nc : nc.evaluate_input("Name") if nc.evaluate_input("Name") else nc.__class__.__name__
  39. class MantisLinkNode(MantisNode):
  40. def __init__(self, signature : tuple,
  41. base_tree : NodeTree,
  42. socket_templates : list[SockTemplate]=[]):
  43. super().__init__(signature, base_tree, socket_templates)
  44. self.node_type = 'LINK'
  45. self.prepared = True; self.bObject=[]
  46. def evaluate_input(self, input_name, index=0):
  47. # should catch 'Target', 'Pole Target' and ArmatureConstraint targets, too
  48. if ('Target' in input_name) and input_name not in ["Target Space", "Use Target Z"]:
  49. socket = self.inputs.get(input_name)
  50. if socket.is_linked:
  51. node_line, _last_socket = trace_single_line(self, input_name, index)
  52. for other_node in node_line:
  53. if other_node.node_type == 'XFORM':
  54. return other_node
  55. return None
  56. else:
  57. return super().evaluate_input(input_name)
  58. def gen_property_socket_map(self) -> dict:
  59. props_sockets = super().gen_property_socket_map()
  60. if (os := self.inputs.get("Owner Space")) and os.is_connected:
  61. node_line, _last_socket = trace_single_line(self, "Owner Space")
  62. for other_node in node_line:
  63. if other_node.node_type == 'XFORM':
  64. del props_sockets['owner_space']; break
  65. if (ts := self.inputs.get("Target Space")) and ts.is_connected:
  66. node_line, _last_socket = trace_single_line(self, "Target Space")
  67. for other_node in node_line:
  68. if other_node.node_type == 'XFORM':
  69. del props_sockets['target_space']; break
  70. return props_sockets
  71. def set_custom_space(self):
  72. for c in self.bObject:
  73. if (os := self.inputs.get("Owner Space")) and os.is_connected:
  74. node_line, _last_socket = trace_single_line(self, "Owner Space")
  75. owner_space_target = None
  76. for other_node in node_line:
  77. if other_node.node_type == 'XFORM':
  78. owner_space_target = other_node; break
  79. if owner_space_target:
  80. c.owner_space='CUSTOM'
  81. xf = owner_space_target.bGetObject(mode="OBJECT")
  82. if isinstance(xf, Bone):
  83. c.space_object=owner_space_target.bGetParentArmature(); c.space_subtarget=xf.name
  84. else:
  85. c.space_object=xf
  86. if (ts := self.inputs.get("Target Space")) and ts.is_connected:
  87. node_line, _last_socket = trace_single_line(self, "Target Space")
  88. target_space_target = None
  89. for other_node in node_line:
  90. if other_node.node_type == 'XFORM':
  91. target_space_target = other_node; break
  92. if target_space_target:
  93. c.target_space='CUSTOM'
  94. xf = target_space_target.bGetObject(mode="OBJECT")
  95. if isinstance(xf, Bone):
  96. c.space_object=target_space_target.bGetParentArmature(); c.space_subtarget=xf.name
  97. else:
  98. c.space_object=xf
  99. def GetxForm(nc, output_name="Output Relationship"):
  100. break_condition= lambda node : node.node_type=='XFORM'
  101. xforms = trace_line_up_branching(nc, output_name, break_condition)
  102. return_me=[]
  103. for xf in xforms:
  104. if xf.node_type != 'XFORM':
  105. continue
  106. if xf in return_me:
  107. continue
  108. return_me.append(xf)
  109. return return_me
  110. def reset_execution(self):
  111. super().reset_execution()
  112. self.prepared = True; self.bObject = []
  113. def bFinalize(self, bContext=None):
  114. finish_drivers(self)
  115. #*#-------------------------------#++#-------------------------------#*#
  116. # L I N K N O D E S
  117. #*#-------------------------------#++#-------------------------------#*#
  118. class LinkInherit(MantisLinkNode):
  119. '''A node representing inheritance'''
  120. def __init__(self, signature, base_tree):
  121. super().__init__(signature, base_tree, LinkInheritSockets)
  122. self.init_parameters()
  123. self.set_traverse([('Parent', 'Inheritance')])
  124. self.executed = True
  125. def GetxForm(self):
  126. # I think this is only run in display update.
  127. trace = trace_single_line_up(self, "Inheritance")
  128. for node in trace[0]:
  129. if (node.node_type == 'XFORM'):
  130. return node
  131. raise GraphError("%s is not connected to a downstream xForm" % self)
  132. class LinkCopyLocation(MantisLinkNode):
  133. '''A node representing Copy Location'''
  134. def __init__(self, signature : tuple,
  135. base_tree : NodeTree,):
  136. super().__init__(signature, base_tree, LinkCopyLocationSockets)
  137. additional_parameters = { "Name":None }
  138. self.init_parameters(additional_parameters=additional_parameters)
  139. self.set_traverse([("Input Relationship", "Output Relationship")])
  140. def bRelationshipPass(self, context):
  141. prepare_parameters(self)
  142. for xf in self.GetxForm():
  143. c = xf.bGetObject().constraints.new('COPY_LOCATION')
  144. self.get_target_and_subtarget(c)
  145. print(wrapGreen("Creating ")+wrapWhite("Copy Location")+
  146. wrapGreen(" Constraint for bone: ") +
  147. wrapOrange(xf.bGetObject().name))
  148. if constraint_name := self.evaluate_input("Name"):
  149. c.name = constraint_name
  150. self.bObject.append(c)
  151. self.set_custom_space()
  152. props_sockets = self.gen_property_socket_map()
  153. evaluate_sockets(self, c, props_sockets)
  154. self.executed = True
  155. class LinkCopyRotation(MantisLinkNode):
  156. '''A node representing Copy Rotation'''
  157. def __init__(self, signature, base_tree):
  158. super().__init__(signature, base_tree, LinkCopyRotationSockets)
  159. additional_parameters = { "Name":None }
  160. self.init_parameters(additional_parameters=additional_parameters)
  161. self.set_traverse([("Input Relationship", "Output Relationship")])
  162. def bRelationshipPass(self, context):
  163. prepare_parameters(self)
  164. for xf in self.GetxForm():
  165. c = xf.bGetObject().constraints.new('COPY_ROTATION')
  166. self.get_target_and_subtarget(c)
  167. print(wrapGreen("Creating ")+wrapWhite("Copy Rotation")+
  168. wrapGreen(" Constraint for bone: ") +
  169. wrapOrange(xf.bGetObject().name))
  170. rotation_order = self.evaluate_input("RotationOrder")
  171. if ((rotation_order == 'QUATERNION') or (rotation_order == 'AXIS_ANGLE')):
  172. c.euler_order = 'AUTO'
  173. else:
  174. try:
  175. c.euler_order = rotation_order
  176. except TypeError: # it's a driver or incorrect
  177. c.euler_order = 'AUTO'
  178. if constraint_name := self.evaluate_input("Name"):
  179. c.name = constraint_name
  180. self.bObject.append(c)
  181. self.set_custom_space()
  182. props_sockets = self.gen_property_socket_map()
  183. evaluate_sockets(self, c, props_sockets)
  184. self.executed = True
  185. class LinkCopyScale(MantisLinkNode):
  186. '''A node representing Copy Scale'''
  187. def __init__(self, signature, base_tree):
  188. super().__init__(signature, base_tree, LinkCopyScaleSockets)
  189. additional_parameters = { "Name":None }
  190. self.init_parameters(additional_parameters=additional_parameters)
  191. self.set_traverse([("Input Relationship", "Output Relationship")])
  192. def bRelationshipPass(self, context):
  193. prepare_parameters(self)
  194. for xf in self.GetxForm():
  195. c = xf.bGetObject().constraints.new('COPY_SCALE')
  196. self.get_target_and_subtarget(c)
  197. print(wrapGreen("Creating ")+wrapWhite("Copy Scale")+
  198. wrapGreen(" Constraint for bone: ") +
  199. wrapOrange(xf.bGetObject().name))
  200. if constraint_name := self.evaluate_input("Name"):
  201. c.name = constraint_name
  202. self.bObject.append(c)
  203. self.set_custom_space()
  204. props_sockets = self.gen_property_socket_map()
  205. evaluate_sockets(self, c, props_sockets)
  206. self.executed = True
  207. class LinkCopyTransforms(MantisLinkNode):
  208. '''A node representing Copy Transfoms'''
  209. def __init__(self, signature, base_tree):
  210. super().__init__(signature, base_tree, LinkCopyTransformsSockets)
  211. additional_parameters = { "Name":None }
  212. self.init_parameters(additional_parameters=additional_parameters)
  213. self.set_traverse([("Input Relationship", "Output Relationship")])
  214. def bRelationshipPass(self, context):
  215. prepare_parameters(self)
  216. for xf in self.GetxForm():
  217. c = xf.bGetObject().constraints.new('COPY_TRANSFORMS')
  218. self.get_target_and_subtarget(c)
  219. print(wrapGreen("Creating ")+wrapWhite("Copy Transforms")+
  220. wrapGreen(" Constraint for bone: ") +
  221. wrapOrange(xf.bGetObject().name))
  222. if constraint_name := self.evaluate_input("Name"):
  223. c.name = constraint_name
  224. self.bObject.append(c)
  225. self.set_custom_space()
  226. props_sockets = self.gen_property_socket_map()
  227. evaluate_sockets(self, c, props_sockets)
  228. self.executed = True
  229. class LinkTransformation(MantisLinkNode):
  230. '''A node representing Copy Transfoms'''
  231. def __init__(self, signature, base_tree):
  232. super().__init__(signature, base_tree, LinkTransformationSockets)
  233. self.init_parameters(additional_parameters={"Name":None })
  234. self.set_traverse([("Input Relationship", "Output Relationship")])
  235. def ui_modify_socket(self, ui_socket, socket_name=None):
  236. from_suffix, to_suffix = '', ''
  237. if self.evaluate_input("Map From") == 'ROTATION': from_suffix='_rot'
  238. elif self.evaluate_input("Map From") == 'SCALE': from_suffix='_scale'
  239. if self.evaluate_input("Map To") == 'ROTATION': to_suffix='_rot'
  240. elif self.evaluate_input("Map To") == 'SCALE': to_suffix='_scale'
  241. if ('To' in ui_socket.name or 'From' in ui_socket.name) and (from_suffix or to_suffix):
  242. for s_temp in self.socket_templates:
  243. if s_temp.name == ui_socket.name: break
  244. if 'from' in s_temp.blender_property:
  245. socket_name=s_temp.blender_property+from_suffix
  246. else:
  247. socket_name=s_temp.blender_property+to_suffix
  248. return self.update_socket_value(socket_name, ui_socket.default_value)
  249. return super().ui_modify_socket(ui_socket, socket_name)
  250. def bRelationshipPass(self, context):
  251. prepare_parameters(self)
  252. for xf in self.GetxForm():
  253. c = xf.bGetObject().constraints.new('TRANSFORM')
  254. self.get_target_and_subtarget(c)
  255. print(wrapGreen("Creating ")+wrapWhite("Transformation")+
  256. wrapGreen(" Constraint for bone: ") +
  257. wrapOrange(xf.bGetObject().name))
  258. if constraint_name := self.evaluate_input("Name"):
  259. c.name = constraint_name
  260. self.bObject.append(c)
  261. self.set_custom_space()
  262. props_sockets = self.gen_property_socket_map()
  263. # we have to fix the blender-property for scale/rotation
  264. # because Blender stores these separately.
  265. # I do not care that this code is ugly.
  266. from_suffix, to_replace = '', ''
  267. if self.evaluate_input("Map From") == 'ROTATION':
  268. from_suffix='_rot'
  269. elif self.evaluate_input("Map From") == 'SCALE':
  270. from_suffix='_scale'
  271. if self.evaluate_input("Map To") == 'ROTATION':
  272. to_replace='_rot'
  273. elif self.evaluate_input("Map To") == 'SCALE':
  274. to_replace='_scale'
  275. if from_suffix:
  276. for axis in ['x', 'y', 'z']:
  277. stub='from_min_'+axis
  278. props_sockets[stub+from_suffix]=props_sockets[stub]
  279. del props_sockets[stub]
  280. stub='from_max_'+axis
  281. props_sockets[stub+from_suffix]=props_sockets[stub]
  282. del props_sockets[stub]
  283. if to_replace:
  284. for axis in ['x', 'y', 'z']:
  285. stub='to_min_'+axis
  286. props_sockets[stub+to_replace]=props_sockets[stub]
  287. del props_sockets[stub]
  288. stub='to_max_'+axis
  289. props_sockets[stub+to_replace]=props_sockets[stub]
  290. del props_sockets[stub]
  291. evaluate_sockets(self, c, props_sockets)
  292. self.executed = True
  293. class LinkLimitLocation(MantisLinkNode):
  294. def __init__(self, signature, base_tree):
  295. super().__init__(signature, base_tree, LinkLimitLocationScaleSockets)
  296. self.init_parameters(additional_parameters={ "Name":None })
  297. self.set_traverse([("Input Relationship", "Output Relationship")])
  298. def bRelationshipPass(self, context):
  299. prepare_parameters(self)
  300. for xf in self.GetxForm():
  301. c = xf.bGetObject().constraints.new('LIMIT_LOCATION')
  302. print(wrapGreen("Creating ")+wrapWhite("Limit Location")+
  303. wrapGreen(" Constraint for bone: ") +
  304. wrapOrange(xf.bGetObject().name))
  305. if constraint_name := self.evaluate_input("Name"):
  306. c.name = constraint_name
  307. self.bObject.append(c)
  308. self.set_custom_space()
  309. props_sockets = self.gen_property_socket_map()
  310. evaluate_sockets(self, c, props_sockets)
  311. self.executed = True
  312. class LinkLimitRotation(MantisLinkNode):
  313. def __init__(self, signature, base_tree):
  314. super().__init__(signature, base_tree, LinkLimitRotationSockets)
  315. self.init_parameters(additional_parameters={ "Name":None })
  316. self.set_traverse([("Input Relationship", "Output Relationship")])
  317. def bRelationshipPass(self, context):
  318. prepare_parameters(self)
  319. for xf in self.GetxForm():
  320. c = xf.bGetObject().constraints.new('LIMIT_ROTATION')
  321. print(wrapGreen("Creating ")+wrapWhite("Limit Rotation")+
  322. wrapGreen(" Constraint for bone: ") +
  323. wrapOrange(xf.bGetObject().name))
  324. if constraint_name := self.evaluate_input("Name"):
  325. c.name = constraint_name
  326. self.bObject.append(c)
  327. self.set_custom_space()
  328. props_sockets = self.gen_property_socket_map()
  329. evaluate_sockets(self, c, props_sockets)
  330. self.executed = True
  331. class LinkLimitScale(MantisLinkNode):
  332. def __init__(self, signature, base_tree):
  333. super().__init__(signature, base_tree, LinkLimitLocationScaleSockets)
  334. self.init_parameters(additional_parameters={ "Name":None })
  335. self.set_traverse([("Input Relationship", "Output Relationship")])
  336. def bRelationshipPass(self, context):
  337. prepare_parameters(self)
  338. for xf in self.GetxForm():
  339. c = xf.bGetObject().constraints.new('LIMIT_SCALE')
  340. print(wrapGreen("Creating ")+wrapWhite("Limit Scale")+
  341. wrapGreen(" Constraint for bone: ") +
  342. wrapOrange(xf.bGetObject().name))
  343. if constraint_name := self.evaluate_input("Name"):
  344. c.name = constraint_name
  345. self.bObject.append(c)
  346. self.set_custom_space()
  347. props_sockets = self.gen_property_socket_map()
  348. evaluate_sockets(self, c, props_sockets)
  349. self.executed = True
  350. class LinkLimitDistance(MantisLinkNode):
  351. def __init__(self, signature, base_tree):
  352. super().__init__(signature, base_tree, LinkLimitDistanceSockets)
  353. self.init_parameters(additional_parameters={ "Name":None })
  354. self.set_traverse([("Input Relationship", "Output Relationship")])
  355. def bRelationshipPass(self, context):
  356. prepare_parameters(self)
  357. for xf in self.GetxForm():
  358. print(wrapGreen("Creating ")+wrapWhite("Limit Distance")+
  359. wrapGreen(" Constraint for bone: ") +
  360. wrapOrange(xf.bGetObject().name))
  361. c = xf.bGetObject().constraints.new('LIMIT_DISTANCE')
  362. self.get_target_and_subtarget(c)
  363. if constraint_name := self.evaluate_input("Name"):
  364. c.name = constraint_name
  365. self.bObject.append(c)
  366. self.set_custom_space()
  367. props_sockets = self.gen_property_socket_map()
  368. evaluate_sockets(self, c, props_sockets)
  369. self.executed = True
  370. # Tracking
  371. class LinkStretchTo(MantisLinkNode):
  372. def __init__(self, signature, base_tree):
  373. super().__init__(signature, base_tree, LinkStretchToSockets)
  374. self.init_parameters(additional_parameters={ "Name":None })
  375. self.set_traverse([("Input Relationship", "Output Relationship")])
  376. def bRelationshipPass(self, context):
  377. prepare_parameters(self)
  378. for xf in self.GetxForm():
  379. print(wrapGreen("Creating ")+wrapWhite("Stretch-To")+
  380. wrapGreen(" Constraint for bone: ") +
  381. wrapOrange(xf.bGetObject().name))
  382. c = xf.bGetObject().constraints.new('STRETCH_TO')
  383. self.get_target_and_subtarget(c)
  384. if constraint_name := self.evaluate_input("Name"):
  385. c.name = constraint_name
  386. self.bObject.append(c)
  387. props_sockets = self.gen_property_socket_map()
  388. evaluate_sockets(self, c, props_sockets)
  389. if (self.evaluate_input("Original Length") == 0):
  390. # this is meant to be set automatically.
  391. c.rest_length = xf.bGetObject().bone.length
  392. self.executed = True
  393. class LinkDampedTrack(MantisLinkNode):
  394. def __init__(self, signature, base_tree):
  395. super().__init__(signature, base_tree, LinkDampedTrackSockets)
  396. self.init_parameters(additional_parameters={ "Name":None })
  397. self.set_traverse([("Input Relationship", "Output Relationship")])
  398. def bRelationshipPass(self, context):
  399. prepare_parameters(self)
  400. for xf in self.GetxForm():
  401. print(wrapGreen("Creating ")+wrapWhite("Damped Track")+
  402. wrapGreen(" Constraint for bone: ") +
  403. wrapOrange(xf.bGetObject().name))
  404. c = xf.bGetObject().constraints.new('DAMPED_TRACK')
  405. self.get_target_and_subtarget(c)
  406. if constraint_name := self.evaluate_input("Name"):
  407. c.name = constraint_name
  408. self.bObject.append(c)
  409. props_sockets = self.gen_property_socket_map()
  410. evaluate_sockets(self, c, props_sockets)
  411. self.executed = True
  412. class LinkLockedTrack(MantisLinkNode):
  413. def __init__(self, signature, base_tree):
  414. super().__init__(signature, base_tree,LinkLockedTrackSockets)
  415. self.init_parameters(additional_parameters={"Name":None })
  416. self.set_traverse([("Input Relationship", "Output Relationship")])
  417. def bRelationshipPass(self, context):
  418. prepare_parameters(self)
  419. for xf in self.GetxForm():
  420. print(wrapGreen("Creating ")+wrapWhite("Locked Track")+
  421. wrapGreen(" Constraint for bone: ") +
  422. wrapOrange(xf.bGetObject().name))
  423. c = xf.bGetObject().constraints.new('LOCKED_TRACK')
  424. self.get_target_and_subtarget(c)
  425. if constraint_name := self.evaluate_input("Name"):
  426. c.name = constraint_name
  427. self.bObject.append(c)
  428. props_sockets = self.gen_property_socket_map()
  429. evaluate_sockets(self, c, props_sockets)
  430. self.executed = True
  431. class LinkTrackTo(MantisLinkNode):
  432. def __init__(self, signature, base_tree):
  433. super().__init__(signature, base_tree, LinkTrackToSockets)
  434. self.init_parameters(additional_parameters={"Name":None })
  435. self.set_traverse([("Input Relationship", "Output Relationship")])
  436. def bRelationshipPass(self, context):
  437. prepare_parameters(self)
  438. for xf in self.GetxForm():
  439. print(wrapGreen("Creating ")+wrapWhite("Track-To")+
  440. wrapGreen(" Constraint for bone: ") +
  441. wrapOrange(xf.bGetObject().name))
  442. c = xf.bGetObject().constraints.new('TRACK_TO')
  443. self.get_target_and_subtarget(c)
  444. if constraint_name := self.evaluate_input("Name"):
  445. c.name = constraint_name
  446. self.bObject.append(c)
  447. props_sockets = self.gen_property_socket_map()
  448. evaluate_sockets(self, c, props_sockets)
  449. self.executed = True
  450. class LinkInheritConstraint(MantisLinkNode):
  451. def __init__(self, signature, base_tree):
  452. super().__init__(signature, base_tree, LinkInheritConstraintSockets)
  453. self.init_parameters(additional_parameters={"Name":None })
  454. self.set_traverse([("Input Relationship", "Output Relationship")])
  455. def bRelationshipPass(self, context):
  456. prepare_parameters(self)
  457. for xf in self.GetxForm():
  458. print(wrapGreen("Creating ")+wrapWhite("Child-Of")+
  459. wrapGreen(" Constraint for bone: ") +
  460. wrapOrange(xf.bGetObject().name))
  461. c = xf.bGetObject().constraints.new('CHILD_OF')
  462. self.get_target_and_subtarget(c)
  463. if constraint_name := self.evaluate_input("Name"):
  464. c.name = constraint_name
  465. self.bObject.append(c)
  466. props_sockets = self.gen_property_socket_map()
  467. evaluate_sockets(self, c, props_sockets)
  468. c.set_inverse_pending
  469. self.executed = True
  470. class LinkInverseKinematics(MantisLinkNode):
  471. def __init__(self, signature, base_tree):
  472. super().__init__(signature, base_tree, LinkInverseKinematicsSockets)
  473. self.init_parameters(additional_parameters={"Name":None })
  474. self.set_traverse([("Input Relationship", "Output Relationship")])
  475. def get_base_ik_bone(self, ik_bone):
  476. chain_length : int = (self.evaluate_input("Chain Length"))
  477. if not isinstance(chain_length, (int, float)):
  478. raise GraphError(f"Chain Length must be an integer number in {self}::Chain Length")
  479. if chain_length == 0:
  480. chain_length = int("inf")
  481. base_ik_bone = ik_bone; i=1
  482. while (i<chain_length) and (base_ik_bone.parent):
  483. base_ik_bone=base_ik_bone.parent; i+=1
  484. return base_ik_bone
  485. # We need to do the calculation in a "full circle", meaning the pole_angle
  486. # can go over pi or less than -pi - but the actuall constraint value must
  487. # be clamped in that range.
  488. # so we simply wrap the value.
  489. # not very efficient but it's OK
  490. def set_pole_angle(self, constraint, angle: float) -> None:
  491. from math import pi
  492. from .utilities import wrap
  493. constraint.pole_angle = wrap(-pi, pi, angle)
  494. def calc_pole_angle_pre(self, c, ik_bone):
  495. """
  496. This function gets us most of the way to a correct IK pole angle. Unfortunately,
  497. due to the unpredictable nature of the iterative IK calculation, I can't figure
  498. out an exact solution. So we do a bisect search in calc_pole_angle_post().
  499. """
  500. # TODO: instead of these checks, convert all to armature local space. But this is tedious.
  501. if not c.target:
  502. raise GraphError(f"IK Constraint {self} must have target.")
  503. elif c.target.type != "ARMATURE":
  504. raise NotImplementedError(f"Currently, IK Constraint Target for {self} must be a bone within the same armature.")
  505. if c.pole_target.type != "ARMATURE":
  506. raise NotImplementedError(f"Currently, IK Constraint Pole Target for {self} must be a bone within the same armature.")
  507. ik_handle = c.target.pose.bones[c.subtarget]
  508. if ik_handle.id_data != ik_bone.id_data:
  509. raise NotImplementedError(f"Currently, IK Constraint Target for {self} must be a bone within the same armature.")
  510. ik_pole = c.pole_target.pose.bones[c.pole_subtarget]
  511. if ik_pole.id_data != ik_bone.id_data:
  512. raise NotImplementedError(f"Currently,IK Constraint Pole Target for {self} must be a bone within the same armature.")
  513. base_ik_bone = self.get_base_ik_bone(ik_bone)
  514. start_effector = base_ik_bone.bone.head_local
  515. end_effector = ik_handle.bone.head_local
  516. pole_location = ik_pole.bone.head_local
  517. # this is the X-Axis of the bone's rest-pose, added to its bone
  518. knee_location = base_ik_bone.bone.matrix_local.col[0].xyz+start_effector
  519. ik_axis = (end_effector-start_effector).normalized()
  520. from .utilities import project_point_to_plane
  521. pole_planar_projection = project_point_to_plane(pole_location, start_effector, ik_axis)
  522. # this planar projection is necessary because the IK axis is different than the base_bone's y axis
  523. planar_projection = project_point_to_plane(knee_location, start_effector, ik_axis)
  524. knee_direction =(planar_projection - start_effector).normalized()
  525. pole_direction =(pole_planar_projection - start_effector).normalized()
  526. return knee_direction.angle(pole_direction)
  527. def calc_pole_angle_post(self, c, ik_bone, context):
  528. """
  529. This function should give us a completely accurate result for IK.
  530. """
  531. from time import time
  532. start_time=time()
  533. def signed_angle(vector_u, vector_v, normal):
  534. # it seems that this fails if the vectors are exactly aligned under certain circumstances.
  535. angle = vector_u.angle(vector_v, 0.0) # So we use a fallback of 0
  536. # Normal specifies orientation
  537. if angle != 0 and vector_u.cross(vector_v).angle(normal) < 1:
  538. angle = -angle
  539. return angle
  540. # we have already checked for valid data.
  541. ik_handle = c.target.pose.bones[c.subtarget]
  542. base_ik_bone = self.get_base_ik_bone(ik_bone)
  543. start_effector = base_ik_bone.bone.head_local
  544. angle = c.pole_angle
  545. dg = context.view_layer.depsgraph
  546. dg.update()
  547. ik_axis = (ik_handle.bone.head_local-start_effector).normalized()
  548. center_point = start_effector +(ik_axis*base_ik_bone.bone.length)
  549. knee_direction = base_ik_bone.bone.tail_local - center_point
  550. current_knee_direction = base_ik_bone.tail-center_point
  551. error=signed_angle(current_knee_direction, knee_direction, ik_axis)
  552. if error == 0:
  553. prGreen("No Fine-tuning needed."); return
  554. # Flip it if needed
  555. dot_before=current_knee_direction.dot(knee_direction)
  556. if dot_before < 0 and angle!=0: # then it is not aligned and we should check the inverse
  557. angle = -angle; c.pole_angle=angle
  558. dg.update()
  559. current_knee_direction = base_ik_bone.tail-center_point
  560. dot_after=current_knee_direction.dot(knee_direction)
  561. if dot_after < dot_before: # they are somehow less aligned
  562. prPurple("Mantis has gone down an unexpected code path. Please report this as a bug.")
  563. angle = -angle; self.set_pole_angle(c, angle)
  564. dg.update()
  565. # now we can do a bisect search to find the best value.
  566. error_threshhold = FLOAT_EPSILON
  567. max_iterations=600
  568. error=signed_angle(current_knee_direction, knee_direction, ik_axis)
  569. if error == 0:
  570. prGreen("No Fine-tuning needed."); return
  571. angle+=error
  572. alt_angle = angle+(error*-2) # should be very near the center when flipped here
  573. # we still need to bisect search because the relationship of pole_angle <==> error is somewhat unpredictable
  574. upper_bounds = alt_angle if alt_angle > angle else angle
  575. lower_bounds = alt_angle if alt_angle < angle else angle
  576. i, error_identical = 0, 0
  577. while ( True ):
  578. if (i>=max_iterations):
  579. prOrange(f"IK Pole Angle Set reached max iterations of {i-error_identical} in {time()-start_time} seconds")
  580. break
  581. if (abs(error)<error_threshhold) or (upper_bounds<=lower_bounds) or (error_identical > 3):
  582. prPurple(f"IK Pole Angle Set converged after {i-error_identical} iterations with error={error} in {time()-start_time} seconds")
  583. break
  584. # get the center-point betweeen the bounds
  585. try_angle = lower_bounds + (upper_bounds-lower_bounds)/2
  586. self.set_pole_angle(c, try_angle); dg.update()
  587. prev_error = error
  588. error = signed_angle((base_ik_bone.tail-center_point), knee_direction, ik_axis)
  589. error_identical+= int(error == prev_error)
  590. if error>0: upper_bounds=try_angle
  591. if error<0: lower_bounds=try_angle
  592. i+=1
  593. def bRelationshipPass(self, context):
  594. prepare_parameters(self)
  595. for xf in self.GetxForm():
  596. print(wrapGreen("Creating ")+wrapOrange("Inverse Kinematics")+
  597. wrapGreen(" Constraint for bone: ") +
  598. wrapOrange(xf.bGetObject().name))
  599. ik_bone = xf.bGetObject()
  600. c = xf.bGetObject().constraints.new('IK')
  601. self.get_target_and_subtarget(c)
  602. self.get_target_and_subtarget(c, input_name = 'Pole Target')
  603. if constraint_name := self.evaluate_input("Name"):
  604. c.name = constraint_name
  605. self.bObject.append(c)
  606. c.chain_count = 1 # so that, if there are errors, this doesn't print
  607. # a whole bunch of circular dependency crap from having infinite chain length
  608. if (c.pole_target):
  609. self.set_pole_angle(c, self.calc_pole_angle_pre(c, ik_bone))
  610. props_sockets = self.gen_property_socket_map()
  611. evaluate_sockets(self, c, props_sockets)
  612. c.use_location = self.evaluate_input("Position") > 0
  613. c.use_rotation = self.evaluate_input("Rotation") > 0
  614. self.executed = True
  615. def bFinalize(self, bContext = None):
  616. # adding a test here
  617. if bContext:
  618. for i, constraint in enumerate(self.bObject):
  619. ik_bone = self.GetxForm()[i].bGetObject(mode='POSE')
  620. if constraint.pole_target:
  621. prWhite(f"Fine-tuning IK Pole Angle for {self}")
  622. # make sure to enable it first
  623. enabled_before = constraint.mute
  624. constraint.mute = False
  625. self.calc_pole_angle_post(constraint, ik_bone, bContext)
  626. constraint.mute = enabled_before
  627. super().bFinalize(bContext)
  628. def ik_report_error(pb, context, do_print=False):
  629. dg = context.view_layer.depsgraph
  630. dg.update()
  631. loc1, rot_quaternion1, scl1 = pb.matrix.decompose()
  632. loc2, rot_quaternion2, scl2 = pb.bone.matrix_local.decompose()
  633. location_error=(loc1-loc2).length
  634. rotation_error = rot_quaternion1.rotation_difference(rot_quaternion2).angle
  635. scale_error = (scl1-scl2).length
  636. if location_error < FLOAT_EPSILON: location_error = 0
  637. if abs(rotation_error) < FLOAT_EPSILON: rotation_error = 0
  638. if scale_error < FLOAT_EPSILON: scale_error = 0
  639. if do_print:
  640. print (f"IK Location Error: {location_error}")
  641. print (f"IK Rotation Error: {rotation_error}")
  642. print (f"IK Scale Error : {scale_error}")
  643. return (location_error, rotation_error, scale_error)
  644. # This is kinda a weird design decision?
  645. class LinkDrivenParameter(MantisLinkNode):
  646. '''A node representing an armature object'''
  647. def __init__(self, signature, base_tree):
  648. super().__init__(signature, base_tree, LinkDrivenParameterSockets)
  649. self.init_parameters(additional_parameters={ "Name":None })
  650. self.set_traverse([("Input Relationship", "Output Relationship")])
  651. def bRelationshipPass(self, bContext = None,):
  652. prepare_parameters(self)
  653. prGreen("Executing Driven Parameter node")
  654. prop = self.evaluate_input("Parameter")
  655. index = self.evaluate_input("Index")
  656. value = self.evaluate_input("Value")
  657. for xf in self.GetxForm():
  658. ob = xf.bGetObject(mode="POSE")
  659. # IMPORTANT: this node only works on pose bone attributes.
  660. self.bObject.append(ob)
  661. length=1
  662. if hasattr(ob, prop):
  663. try:
  664. length = len(getattr(ob, prop))
  665. except TypeError:
  666. pass
  667. except AttributeError:
  668. pass
  669. else:
  670. raise AttributeError(f"Cannot Set value {prop} on object because it does not exist.")
  671. def_value = 0.0
  672. if length>1:
  673. def_value=[0.0]*length
  674. self.parameters["Value"] = tuple( 0.0 if i != index else value for i in range(length))
  675. props_sockets = {
  676. prop: ("Value", def_value)
  677. }
  678. evaluate_sockets(self, ob, props_sockets)
  679. self.executed = True
  680. def bFinalize(self, bContext = None):
  681. driver = self.evaluate_input("Value")
  682. try:
  683. for i, val in enumerate(self.parameters["Value"]):
  684. from .drivers import MantisDriver
  685. if isinstance(val, MantisDriver):
  686. driver["ind"] = i
  687. val = driver
  688. except AttributeError:
  689. self.parameters["Value"] = driver
  690. except TypeError:
  691. self.parameters["Value"] = driver
  692. super().bFinalize(bContext)
  693. class LinkArmature(MantisLinkNode):
  694. '''A node representing an armature object'''
  695. def __init__(self, signature, base_tree,):
  696. super().__init__(signature, base_tree, LinkArmatureSockets)
  697. self.init_parameters(additional_parameters={"Name":None })
  698. self.set_traverse([("Input Relationship", "Output Relationship")])
  699. setup_custom_props(self) # <-- this takes care of the runtime-added sockets
  700. def bRelationshipPass(self, bContext = None,):
  701. prepare_parameters(self)
  702. for xf in self.GetxForm():
  703. print(wrapGreen("Creating ")+wrapOrange("Armature")+
  704. wrapGreen(" Constraint for bone: ") +
  705. wrapOrange(xf.bGetObject().name))
  706. c = xf.bGetObject().constraints.new('ARMATURE')
  707. if constraint_name := self.evaluate_input("Name"):
  708. c.name = constraint_name
  709. self.bObject.append(c)
  710. # get number of targets
  711. num_targets = len( list(self.inputs.values())[6:] )//2
  712. props_sockets = self.gen_property_socket_map()
  713. targets_weights = {}
  714. for i in range(num_targets):
  715. target = c.targets.new()
  716. target_input_name = list(self.inputs.keys())[i*2+6 ]
  717. weight_input_name = list(self.inputs.keys())[i*2+6+1]
  718. self.get_target_and_subtarget(target, target_input_name)
  719. weight_value=self.evaluate_input(weight_input_name)
  720. if not isinstance(weight_value, float):
  721. weight_value=0
  722. targets_weights[i]=weight_value
  723. props_sockets["targets[%d].weight" % i] = (weight_input_name, 0)
  724. evaluate_sockets(self, c, props_sockets)
  725. for target, value in targets_weights.items():
  726. c.targets[target].weight=value
  727. self.executed = True
  728. class LinkSplineIK(MantisLinkNode):
  729. '''A node representing an armature object'''
  730. def __init__(self, signature, base_tree):
  731. super().__init__(signature, base_tree, LinkSplineIKSockets)
  732. self.init_parameters(additional_parameters={"Name":None })
  733. self.set_traverse([("Input Relationship", "Output Relationship")])
  734. def bRelationshipPass(self, bContext = None,):
  735. prepare_parameters(self)
  736. if not self.inputs['Target'].is_linked:
  737. raise GraphError(f"ERROR: {self} is not connected to a target curve.")
  738. for xf in self.GetxForm():
  739. print(wrapGreen("Creating ")+wrapOrange("Spline-IK")+
  740. wrapGreen(" Constraint for bone: ") +
  741. wrapOrange(xf.bGetObject().name))
  742. c = xf.bGetObject().constraints.new('SPLINE_IK')
  743. # set the spline - we need to get the right one
  744. spline_index = self.evaluate_input("Spline Index")
  745. proto_curve = None
  746. node_line, _last_socket = trace_single_line(self, "Target")
  747. for other_node in node_line: # trace and get the input
  748. if other_node.node_type == 'XFORM':
  749. proto_curve = other_node.bGetObject(); break
  750. from .utilities import get_extracted_spline_object
  751. curve = get_extracted_spline_object(proto_curve, spline_index, self.mContext)
  752. # link it to the view layer
  753. if (curve.name not in bContext.view_layer.active_layer_collection.collection.objects):
  754. bContext.view_layer.active_layer_collection.collection.objects.link(curve)
  755. c.target=curve
  756. if constraint_name := self.evaluate_input("Name"):
  757. c.name = constraint_name
  758. self.bObject.append(c)
  759. props_sockets = self.gen_property_socket_map()
  760. evaluate_sockets(self, c, props_sockets)
  761. self.executed = True
  762. class LinkFloor(MantisLinkNode):
  763. '''A node representing an armature object'''
  764. def __init__(self, signature, base_tree,):
  765. super().__init__(signature, base_tree, LinkFloorSockets)
  766. self.init_parameters(additional_parameters={"Name":None })
  767. self.set_traverse([("Input Relationship", "Output Relationship")])
  768. def bRelationshipPass(self, bContext = None,):
  769. prepare_parameters(self)
  770. for xf in self.GetxForm():
  771. print(wrapGreen("Creating ")+wrapOrange("Floor")+
  772. wrapGreen(" Constraint for bone: ") +
  773. wrapOrange(xf.bGetObject().name))
  774. c = xf.bGetObject().constraints.new('FLOOR')
  775. self.get_target_and_subtarget(c)
  776. if constraint_name := self.evaluate_input("Name"):
  777. c.name = constraint_name
  778. self.bObject.append(c)
  779. self.set_custom_space()
  780. props_sockets = self.gen_property_socket_map()
  781. evaluate_sockets(self, c, props_sockets)
  782. self.executed = True
  783. class LinkShrinkWrap(MantisLinkNode):
  784. '''A node representing a shrinkwrap relationship.'''
  785. def __init__(self, signature, base_tree,):
  786. super().__init__(signature, base_tree, LinkShrinkWrapSockets)
  787. self.init_parameters(additional_parameters={"Name":None })
  788. self.set_traverse([("Input Relationship", "Output Relationship")])
  789. def bRelationshipPass(self, bContext = None,):
  790. prepare_parameters(self)
  791. for xf in self.GetxForm():
  792. print(wrapGreen("Creating ")+wrapOrange("Shrinkwrap")+
  793. wrapGreen(" Constraint for bone: ") +
  794. wrapOrange(xf.bGetObject().name))
  795. c = xf.bGetObject().constraints.new('SHRINKWRAP')
  796. self.get_target_and_subtarget(c)
  797. if constraint_name := self.evaluate_input("Name"):
  798. c.name = constraint_name
  799. props_sockets = self.gen_property_socket_map()
  800. print (props_sockets.keys())
  801. self.bObject.append(c)
  802. # self.set_custom_space() # this needs to be overridden for me TODO
  803. evaluate_sockets(self, c, props_sockets)
  804. self.executed = True