link_nodes.py 41 KB

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