link_containers.py 37 KB

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