link_containers.py 37 KB

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