link_containers.py 35 KB

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