link_containers.py 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. from .node_container_common import *
  2. from bpy.types import Bone
  3. from .base_definitions import MantisNode, NodeSocket, GraphError, FLOAT_EPSILON
  4. def TellClasses():
  5. return [
  6. # special
  7. LinkInherit,
  8. # copy
  9. LinkCopyLocation,
  10. LinkCopyRotation,
  11. LinkCopyScale,
  12. LinkCopyTransforms,
  13. LinkTransformation,
  14. # limit
  15. LinkLimitLocation,
  16. LinkLimitRotation,
  17. LinkLimitScale,
  18. LinkLimitDistance,
  19. # tracking
  20. LinkStretchTo,
  21. LinkDampedTrack,
  22. LinkLockedTrack,
  23. LinkTrackTo,
  24. #misc
  25. LinkInheritConstraint,
  26. LinkArmature,
  27. # IK
  28. LinkInverseKinematics,
  29. LinkSplineIK,
  30. # Drivers
  31. LinkDrivenParameter,
  32. ]
  33. class MantisLinkNode(MantisNode):
  34. def __init__(self, signature, base_tree):
  35. super().__init__(signature, base_tree)
  36. self.node_type = 'LINK'
  37. self.prepared = True
  38. def evaluate_input(self, input_name, index=0):
  39. # should catch 'Target', 'Pole Target' and ArmatureConstraint targets, too
  40. if ('Target' in input_name) and input_name not in ["Target Space", "Use Target Z"]:
  41. socket = self.inputs.get(input_name)
  42. if socket.is_linked:
  43. return socket.links[0].from_node
  44. return None
  45. else:
  46. return super().evaluate_input(input_name)
  47. # set the name if it is available, otherwise just use the constraint's nice name
  48. set_constraint_name = lambda nc : nc.evaluate_input("Name") if nc.evaluate_input("Name") else nc.__class__.__name__
  49. #*#-------------------------------#++#-------------------------------#*#
  50. # L I N K N O D E S
  51. #*#-------------------------------#++#-------------------------------#*#
  52. def GetxForm(nc):
  53. trace = trace_single_line_up(nc, "Output Relationship")
  54. for node in trace[0]:
  55. if (node.node_type == 'XFORM'):
  56. return node
  57. raise GraphError("%s is not connected to a downstream xForm" % nc)
  58. class LinkInherit(MantisLinkNode):
  59. '''A node representing inheritance'''
  60. def __init__(self, signature, base_tree):
  61. super().__init__(signature, base_tree)
  62. inputs = ["Parent", "Inherit Rotation", "Inherit Scale", "Connected"]
  63. self.inputs.init_sockets(inputs)
  64. self.outputs.init_sockets(["Inheritance"])
  65. self.init_parameters()
  66. self.set_traverse([('Parent', 'Inheritance')])
  67. self.executed = True
  68. def GetxForm(self): # DUPLICATED, TODO fix this
  69. # I think this is only run in display update.
  70. trace = trace_single_line_up(self, "Inheritance")
  71. for node in trace[0]:
  72. if (node.node_type == 'XFORM'):
  73. return node
  74. raise GraphError("%s is not connected to a downstream xForm" % self)
  75. class LinkCopyLocation(MantisLinkNode):
  76. '''A node representing Copy Location'''
  77. def __init__(self, signature, base_tree):
  78. super().__init__(signature, base_tree)
  79. inputs = [
  80. "Input Relationship",
  81. "Head/Tail",
  82. "UseBBone",
  83. "Axes",
  84. "Invert",
  85. "Target Space",
  86. "Owner Space",
  87. "Offset",
  88. "Influence",
  89. "Target",
  90. "Enable",
  91. ]
  92. additional_parameters = { "Name":None }
  93. self.inputs.init_sockets(inputs)
  94. self.outputs.init_sockets(["Output Relationship"])
  95. self.init_parameters(additional_parameters=additional_parameters)
  96. self.set_traverse([("Input Relationship", "Output Relationship")])
  97. def GetxForm(self):
  98. return GetxForm(self)
  99. def bExecute(self, context):
  100. prepare_parameters(self)
  101. c = self.GetxForm().bGetObject().constraints.new('COPY_LOCATION')
  102. get_target_and_subtarget(self, c)
  103. print(wrapGreen("Creating ")+wrapWhite("Copy Location")+
  104. wrapGreen(" Constraint for bone: ") +
  105. wrapOrange(self.GetxForm().bGetObject().name))
  106. if constraint_name := self.evaluate_input("Name"):
  107. c.name = constraint_name
  108. self.bObject = c
  109. custom_space_owner, custom_space_target = False, False
  110. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  111. custom_space_owner=True
  112. c.owner_space='CUSTOM'
  113. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  114. if isinstance(xf, Bone):
  115. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  116. else:
  117. c.space_object=xf
  118. if self.inputs["Target Space"].is_connected and self.inputs["Target Space"].links[0].from_node.node_type == 'XFORM':
  119. custom_space_target=True
  120. c.target_space='CUSTOM'
  121. xf = self.inputs["Target Space"].links[0].from_node.bGetObject(mode="OBJECT")
  122. if isinstance(xf, Bone):
  123. c.space_object=self.inputs["Target Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  124. else:
  125. c.space_object=xf
  126. props_sockets = {
  127. 'use_offset' : ("Offset", False),
  128. 'head_tail' : ("Head/Tail", 0),
  129. 'use_bbone_shape' : ("UseBBone", False),
  130. 'invert_x' : ( ("Invert", 0), False),
  131. 'invert_y' : ( ("Invert", 1), False),
  132. 'invert_z' : ( ("Invert", 2), False),
  133. 'use_x' : ( ("Axes", 0), False),
  134. 'use_y' : ( ("Axes", 1), False),
  135. 'use_z' : ( ("Axes", 2), False),
  136. 'owner_space' : ("Owner Space", 'WORLD'),
  137. 'target_space' : ("Target Space", 'WORLD'),
  138. 'influence' : ("Influence", 1),
  139. 'mute' : ("Enable", True),
  140. }
  141. if custom_space_owner: del props_sockets['owner_space']
  142. if custom_space_target: del props_sockets['target_space']
  143. #
  144. evaluate_sockets(self, c, props_sockets)
  145. self.executed = True
  146. def bFinalize(self, bContext = None):
  147. finish_drivers(self)
  148. class LinkCopyRotation(MantisLinkNode):
  149. '''A node representing Copy Rotation'''
  150. def __init__(self, signature, base_tree):
  151. super().__init__(signature, base_tree)
  152. inputs = [
  153. "Input Relationship",
  154. "RotationOrder",
  155. "Rotation Mix",
  156. "Axes",
  157. "Invert",
  158. "Target Space",
  159. "Owner Space",
  160. "Influence",
  161. "Target",
  162. "Enable",
  163. ]
  164. additional_parameters = { "Name":None }
  165. self.inputs.init_sockets(inputs)
  166. self.outputs.init_sockets(["Output Relationship"])
  167. self.init_parameters(additional_parameters=additional_parameters)
  168. self.set_traverse([("Input Relationship", "Output Relationship")])
  169. def GetxForm(self):
  170. return GetxForm(self)
  171. def bExecute(self, context):
  172. prepare_parameters(self)
  173. c = self.GetxForm().bGetObject().constraints.new('COPY_ROTATION')
  174. get_target_and_subtarget(self, c)
  175. print(wrapGreen("Creating ")+wrapWhite("Copy Rotation")+
  176. wrapGreen(" Constraint for bone: ") +
  177. wrapOrange(self.GetxForm().bGetObject().name))
  178. rotation_order = self.evaluate_input("RotationOrder")
  179. if ((rotation_order == 'QUATERNION') or (rotation_order == 'AXIS_ANGLE')):
  180. c.euler_order = 'AUTO'
  181. else:
  182. try:
  183. c.euler_order = rotation_order
  184. except TypeError: # it's a driver or incorrect
  185. c.euler_order = 'AUTO'
  186. if constraint_name := self.evaluate_input("Name"):
  187. c.name = constraint_name
  188. self.bObject = c
  189. custom_space_owner, custom_space_target = False, False
  190. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  191. custom_space_owner=True
  192. c.owner_space='CUSTOM'
  193. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  194. if isinstance(xf, Bone):
  195. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  196. else:
  197. c.space_object=xf
  198. if self.inputs["Target Space"].is_connected and self.inputs["Target Space"].links[0].from_node.node_type == 'XFORM':
  199. custom_space_target=True
  200. c.target_space='CUSTOM'
  201. xf = self.inputs["Target Space"].links[0].from_node.bGetObject(mode="OBJECT")
  202. if isinstance(xf, Bone):
  203. c.space_object=self.inputs["Target Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  204. else:
  205. c.space_object=xf
  206. props_sockets = {
  207. 'euler_order' : ("RotationOrder", 'AUTO'),
  208. 'mix_mode' : ("Rotation Mix", 'REPLACE'),
  209. 'invert_x' : ( ("Invert", 0), False),
  210. 'invert_y' : ( ("Invert", 1), False),
  211. 'invert_z' : ( ("Invert", 2), False),
  212. 'use_x' : ( ("Axes", 0), False),
  213. 'use_y' : ( ("Axes", 1), False),
  214. 'use_z' : ( ("Axes", 2), False),
  215. 'owner_space' : ("Owner Space", 'WORLD'),
  216. 'target_space' : ("Target Space", 'WORLD'),
  217. 'influence' : ("Influence", 1),
  218. 'mute' : ("Enable", True),
  219. }
  220. if custom_space_owner: del props_sockets['owner_space']
  221. if custom_space_target: del props_sockets['target_space']
  222. #
  223. evaluate_sockets(self, c, props_sockets)
  224. self.executed = True
  225. def bFinalize(self, bContext = None):
  226. finish_drivers(self)
  227. class LinkCopyScale(MantisLinkNode):
  228. '''A node representing Copy Scale'''
  229. def __init__(self, signature, base_tree):
  230. super().__init__(signature, base_tree)
  231. inputs = [
  232. "Input Relationship",
  233. "Offset",
  234. "Average",
  235. "Additive",
  236. "Axes",
  237. "Target Space",
  238. "Owner Space",
  239. "Influence",
  240. "Target",
  241. "Enable",
  242. ]
  243. additional_parameters = { "Name":None }
  244. self.inputs.init_sockets(inputs)
  245. self.outputs.init_sockets(["Output Relationship"])
  246. self.init_parameters(additional_parameters=additional_parameters)
  247. self.set_traverse([("Input Relationship", "Output Relationship")])
  248. def GetxForm(self):
  249. return GetxForm(self)
  250. def bExecute(self, context):
  251. prepare_parameters(self)
  252. c = self.GetxForm().bGetObject().constraints.new('COPY_SCALE')
  253. get_target_and_subtarget(self, c)
  254. print(wrapGreen("Creating ")+wrapWhite("Copy Scale")+
  255. wrapGreen(" Constraint for bone: ") +
  256. wrapOrange(self.GetxForm().bGetObject().name))
  257. if constraint_name := self.evaluate_input("Name"):
  258. c.name = constraint_name
  259. self.bObject = c
  260. custom_space_owner, custom_space_target = False, False
  261. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  262. custom_space_owner=True
  263. c.owner_space='CUSTOM'
  264. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  265. if isinstance(xf, Bone):
  266. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  267. else:
  268. c.space_object=xf
  269. if self.inputs["Target Space"].is_connected and self.inputs["Target Space"].links[0].from_node.node_type == 'XFORM':
  270. custom_space_target=True
  271. c.target_space='CUSTOM'
  272. xf = self.inputs["Target Space"].links[0].from_node.bGetObject(mode="OBJECT")
  273. if isinstance(xf, Bone):
  274. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  275. else:
  276. c.space_object=xf
  277. props_sockets = {
  278. 'use_offset' : ("Offset", False),
  279. 'use_make_uniform' : ("Average", False),
  280. 'owner_space' : ("Owner Space", 'WORLD'),
  281. 'target_space' : ("Target Space", 'WORLD'),
  282. 'use_x' : ( ("Axes", 0), False),
  283. 'use_y' : ( ("Axes", 1), False),
  284. 'use_z' : ( ("Axes", 2), False),
  285. 'influence' : ("Influence", 1),
  286. 'mute' : ("Enable", True),
  287. }
  288. if custom_space_owner: del props_sockets['owner_space']
  289. if custom_space_target: del props_sockets['target_space']
  290. #
  291. evaluate_sockets(self, c, props_sockets)
  292. self.executed = True
  293. def bFinalize(self, bContext = None):
  294. finish_drivers(self)
  295. class LinkCopyTransforms(MantisLinkNode):
  296. '''A node representing Copy Transfoms'''
  297. def __init__(self, signature, base_tree):
  298. super().__init__(signature, base_tree)
  299. inputs = [
  300. "Input Relationship",
  301. "Head/Tail",
  302. "UseBBone",
  303. "Additive",
  304. "Mix",
  305. "Target Space",
  306. "Owner Space",
  307. "Influence",
  308. "Target",
  309. "Enable",
  310. ]
  311. additional_parameters = { "Name":None }
  312. self.inputs.init_sockets(inputs)
  313. self.outputs.init_sockets(["Output Relationship"])
  314. self.init_parameters(additional_parameters=additional_parameters)
  315. self.set_traverse([("Input Relationship", "Output Relationship")])
  316. def GetxForm(self):
  317. return GetxForm(self)
  318. def bExecute(self, context):
  319. prepare_parameters(self)
  320. c = self.GetxForm().bGetObject().constraints.new('COPY_TRANSFORMS')
  321. get_target_and_subtarget(self, c)
  322. print(wrapGreen("Creating ")+wrapWhite("Copy Transforms")+
  323. wrapGreen(" Constraint for bone: ") +
  324. wrapOrange(self.GetxForm().bGetObject().name))
  325. if constraint_name := self.evaluate_input("Name"):
  326. c.name = constraint_name
  327. self.bObject = c
  328. custom_space_owner, custom_space_target = False, False
  329. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  330. custom_space_owner=True
  331. c.owner_space='CUSTOM'
  332. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  333. if isinstance(xf, Bone):
  334. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  335. else:
  336. c.space_object=xf
  337. if self.inputs["Target Space"].is_connected and self.inputs["Target Space"].links[0].from_node.node_type == 'XFORM':
  338. custom_space_target=True
  339. c.target_space='CUSTOM'
  340. xf = self.inputs["Target Space"].links[0].from_node.bGetObject(mode="OBJECT")
  341. if isinstance(xf, Bone):
  342. c.space_object=self.inputs["Target Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  343. else:
  344. c.space_object=xf
  345. props_sockets = {
  346. 'head_tail' : ("Head/Tail", 0),
  347. 'use_bbone_shape' : ("UseBBone", False),
  348. 'mix_mode' : ("Mix", 'REPLACE'),
  349. 'owner_space' : ("Owner Space", 'WORLD'),
  350. 'target_space' : ("Target Space", 'WORLD'),
  351. 'influence' : ("Influence", 1),
  352. 'mute' : ("Enable", False)
  353. }
  354. if custom_space_owner: del props_sockets['owner_space']
  355. if custom_space_target: del props_sockets['target_space']
  356. #
  357. evaluate_sockets(self, c, props_sockets)
  358. self.executed = True
  359. def bFinalize(self, bContext = None):
  360. finish_drivers(self)
  361. transformation_props_sockets = {
  362. 'use_motion_extrapolate' : ("Extrapolate", False),
  363. 'map_from' : ("Map From", 'LOCATION'),
  364. 'from_rotation_mode' : ("Rotation Mode", 'AUTO'),
  365. 'from_min_x' : ("X Min From", 0.0),
  366. 'from_max_x' : ("X Max From", 0.0),
  367. 'from_min_y' : ("Y Min From", 0.0),
  368. 'from_max_y' : ("Y Max From", 0.0),
  369. 'from_min_z' : ("Z Min From", 0.0),
  370. 'from_max_z' : ("Z Max From", 0.0),
  371. 'from_min_x_rot' : ("X Min From", 0.0),
  372. 'from_max_x_rot' : ("X Max From", 0.0),
  373. 'from_min_y_rot' : ("Y Min From", 0.0),
  374. 'from_max_y_rot' : ("Y Max From", 0.0),
  375. 'from_min_z_rot' : ("Z Min From", 0.0),
  376. 'from_max_z_rot' : ("Z Max From", 0.0),
  377. 'from_min_x_scale' : ("X Min From", 0.0),
  378. 'from_max_x_scale' : ("X Max From", 0.0),
  379. 'from_min_y_scale' : ("Y Min From", 0.0),
  380. 'from_max_y_scale' : ("Y Max From", 0.0),
  381. 'from_min_z_scale' : ("Z Min From", 0.0),
  382. 'from_max_z_scale' : ("Z Max From", 0.0),
  383. 'map_to' : ("Map To", "LOCATION"),
  384. 'map_to_x_from' : ("X Source Axis", "X"),
  385. 'map_to_y_from' : ("Y Source Axis", "Y"),
  386. 'map_to_z_from' : ("Z Source Axis", "Z"),
  387. 'to_min_x' : ("X Min To", 0.0),
  388. 'to_max_x' : ("X Max To", 0.0),
  389. 'to_min_y' : ("Y Min To", 0.0),
  390. 'to_max_y' : ("Y Max To", 0.0),
  391. 'to_min_z' : ("Z Min To", 0.0),
  392. 'to_max_z' : ("Z Max To", 0.0),
  393. 'to_min_x_rot' : ("X Min To", 0.0),
  394. 'to_max_x_rot' : ("X Max To", 0.0),
  395. 'to_min_y_rot' : ("Y Min To", 0.0),
  396. 'to_max_y_rot' : ("Y Max To", 0.0),
  397. 'to_min_z_rot' : ("Z Min To", 0.0),
  398. 'to_max_z_rot' : ("Z Max To", 0.0),
  399. 'to_min_x_scale' : ("X Min To", 0.0),
  400. 'to_max_x_scale' : ("X Max To", 0.0),
  401. 'to_min_y_scale' : ("Y Min To", 0.0),
  402. 'to_max_y_scale' : ("Y Max To", 0.0),
  403. 'to_min_z_scale' : ("Z Min To", 0.0),
  404. 'to_max_z_scale' : ("Z Max To", 0.0),
  405. 'to_euler_order' : ("Rotation Mode", "AUTO"),
  406. 'mix_mode' : ("Mix Mode (Translation)", "ADD"),
  407. 'mix_mode_rot' : ("Mix Mode (Rotation)", "ADD"),
  408. 'mix_mode_scale' : ("Mix Mode (Scale)", "MULTIPLY"),
  409. 'owner_space' : ("Owner Space", 'WORLD'),
  410. 'target_space' : ("Target Space", 'WORLD'),
  411. 'influence' : ("Influence", 1),
  412. 'mute' : ("Enable", False),
  413. }
  414. class LinkTransformation(MantisLinkNode):
  415. '''A node representing Copy Transfoms'''
  416. def __init__(self, signature, base_tree):
  417. super().__init__(signature, base_tree)
  418. inputs = [
  419. "Input Relationship" ,
  420. "Target Space" ,
  421. "Owner Space" ,
  422. "Influence" ,
  423. "Target" ,
  424. "Enable" ,
  425. "Extrapolate" ,
  426. "Map From" ,
  427. "Rotation Mode" ,
  428. "X Min From" ,
  429. "X Max From" ,
  430. "Y Min From" ,
  431. "Y Max From" ,
  432. "Z Min From" ,
  433. "Z Max From" ,
  434. "Map To" ,
  435. "X Source Axis" ,
  436. "X Min To" ,
  437. "X Max To" ,
  438. "Y Source Axis" ,
  439. "Y Min To" ,
  440. "Y Max To" ,
  441. "Z Source Axis" ,
  442. "Z Min To" ,
  443. "Z Max To" ,
  444. "Mix Mode (Translation)" ,
  445. "Mix Mode (Rotation)" ,
  446. "Mix Mode (Scale)" ,
  447. ]
  448. additional_parameters = { "Name":None }
  449. self.inputs.init_sockets(inputs)
  450. self.outputs.init_sockets(["Output Relationship"])
  451. self.init_parameters(additional_parameters=additional_parameters)
  452. self.set_traverse([("Input Relationship", "Output Relationship")])
  453. def GetxForm(self):
  454. return GetxForm(self)
  455. def bExecute(self, context):
  456. prepare_parameters(self)
  457. c = self.GetxForm().bGetObject().constraints.new('TRANSFORM')
  458. get_target_and_subtarget(self, c)
  459. print(wrapGreen("Creating ")+wrapWhite("Transformation")+
  460. wrapGreen(" Constraint for bone: ") +
  461. wrapOrange(self.GetxForm().bGetObject().name))
  462. if constraint_name := self.evaluate_input("Name"):
  463. c.name = constraint_name
  464. self.bObject = c
  465. custom_space_owner, custom_space_target = False, False
  466. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  467. custom_space_owner=True
  468. c.owner_space='CUSTOM'
  469. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  470. if isinstance(xf, Bone):
  471. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  472. else:
  473. c.space_object=xf
  474. if self.inputs["Target Space"].is_connected and self.inputs["Target Space"].links[0].from_node.node_type == 'XFORM':
  475. custom_space_target=True
  476. c.target_space='CUSTOM'
  477. xf = self.inputs["Target Space"].links[0].from_node.bGetObject(mode="OBJECT")
  478. if isinstance(xf, Bone):
  479. c.space_object=self.inputs["Target Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  480. else:
  481. c.space_object=xf
  482. props_sockets = transformation_props_sockets.copy()
  483. if custom_space_owner: del props_sockets['owner_space']
  484. if custom_space_target: del props_sockets['target_space']
  485. #
  486. evaluate_sockets(self, c, props_sockets)
  487. self.executed = True
  488. def bFinalize(self, bContext = None):
  489. finish_drivers(self)
  490. class LinkLimitLocation(MantisLinkNode):
  491. def __init__(self, signature, base_tree):
  492. super().__init__(signature, base_tree)
  493. inputs = [
  494. "Input Relationship" ,
  495. "Use Max X" ,
  496. "Max X" ,
  497. "Use Max Y" ,
  498. "Max Y" ,
  499. "Use Max Z" ,
  500. "Max Z" ,
  501. "Use Min X" ,
  502. "Min X" ,
  503. "Use Min Y" ,
  504. "Min Y" ,
  505. "Use Min Z" ,
  506. "Min Z" ,
  507. "Affect Transform" ,
  508. "Owner Space" ,
  509. "Influence" ,
  510. "Enable" ,
  511. ]
  512. additional_parameters = { "Name":None }
  513. self.inputs.init_sockets(inputs)
  514. self.outputs.init_sockets(["Output Relationship"])
  515. self.init_parameters(additional_parameters=additional_parameters)
  516. self.set_traverse([("Input Relationship", "Output Relationship")])
  517. def GetxForm(self):
  518. return GetxForm(self)
  519. def bExecute(self, context):
  520. prepare_parameters(self)
  521. c = self.GetxForm().bGetObject().constraints.new('LIMIT_LOCATION')
  522. #
  523. print(wrapGreen("Creating ")+wrapWhite("Limit Location")+
  524. wrapGreen(" Constraint for bone: ") +
  525. wrapOrange(self.GetxForm().bGetObject().name))
  526. if constraint_name := self.evaluate_input("Name"):
  527. c.name = constraint_name
  528. self.bObject = c
  529. custom_space_owner = False
  530. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  531. custom_space_owner=True
  532. c.owner_space='CUSTOM'
  533. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  534. if isinstance(xf, Bone):
  535. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  536. else:
  537. c.space_object=xf
  538. props_sockets = {
  539. 'use_transform_limit' : ("Affect Transform", False),
  540. 'use_max_x' : ("Use Max X", False),
  541. 'use_max_y' : ("Use Max Y", False),
  542. 'use_max_z' : ("Use Max Z", False),
  543. 'use_min_x' : ("Use Min X", False),
  544. 'use_min_y' : ("Use Min Y", False),
  545. 'use_min_z' : ("Use Min Z", False),
  546. 'max_x' : ("Max X", 0),
  547. 'max_y' : ("Max Y", 0),
  548. 'max_z' : ("Max Z", 0),
  549. 'min_x' : ("Min X", 0),
  550. 'min_y' : ("Min Y", 0),
  551. 'min_z' : ("Min Z", 0),
  552. 'owner_space' : ("Owner Space", 'WORLD'),
  553. 'influence' : ("Influence", 1),
  554. 'mute' : ("Enable", True),
  555. }
  556. if custom_space_owner: del props_sockets['owner_space']
  557. #
  558. evaluate_sockets(self, c, props_sockets)
  559. self.executed = True
  560. def bFinalize(self, bContext = None):
  561. finish_drivers(self)
  562. class LinkLimitRotation(MantisLinkNode):
  563. def __init__(self, signature, base_tree):
  564. super().__init__(signature, base_tree)
  565. inputs = [
  566. "Input Relationship" ,
  567. "Use X" ,
  568. "Use Y" ,
  569. "Use Z" ,
  570. "Max X" ,
  571. "Max Y" ,
  572. "Max Z" ,
  573. "Min X" ,
  574. "Min Y" ,
  575. "Min Z" ,
  576. "Affect Transform" ,
  577. "Owner Space" ,
  578. "Influence" ,
  579. "Enable" ,
  580. ]
  581. additional_parameters = { "Name":None }
  582. self.inputs.init_sockets(inputs)
  583. self.outputs.init_sockets(["Output Relationship"])
  584. self.init_parameters(additional_parameters=additional_parameters)
  585. self.set_traverse([("Input Relationship", "Output Relationship")])
  586. def GetxForm(self):
  587. return GetxForm(self)
  588. def bExecute(self, context):
  589. prepare_parameters(self)
  590. c = self.GetxForm().bGetObject().constraints.new('LIMIT_ROTATION')
  591. print(wrapGreen("Creating ")+wrapWhite("Limit Rotation")+
  592. wrapGreen(" Constraint for bone: ") +
  593. wrapOrange(self.GetxForm().bGetObject().name))
  594. if constraint_name := self.evaluate_input("Name"):
  595. c.name = constraint_name
  596. self.bObject = c
  597. custom_space_owner = False
  598. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  599. custom_space_owner=True
  600. c.owner_space='CUSTOM'
  601. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  602. if isinstance(xf, Bone):
  603. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  604. else:
  605. c.space_object=xf
  606. props_sockets = {
  607. 'use_transform_limit' : ("Affect Transform", False),
  608. 'use_limit_x' : ("Use X", False),
  609. 'use_limit_y' : ("Use Y", False),
  610. 'use_limit_z' : ("Use Z", False),
  611. 'max_x' : ("Max X", 0),
  612. 'max_y' : ("Max Y", 0),
  613. 'max_z' : ("Max Z", 0),
  614. 'min_x' : ("Min X", 0),
  615. 'min_y' : ("Min Y", 0),
  616. 'min_z' : ("Min Z", 0),
  617. 'owner_space' : ("Owner Space", 'WORLD'),
  618. 'influence' : ("Influence", 1),
  619. 'mute' : ("Enable", True),
  620. }
  621. if custom_space_owner: del props_sockets['owner_space']
  622. #
  623. evaluate_sockets(self, c, props_sockets)
  624. self.executed = True
  625. def bFinalize(self, bContext = None):
  626. finish_drivers(self)
  627. class LinkLimitScale(MantisLinkNode):
  628. def __init__(self, signature, base_tree):
  629. super().__init__(signature, base_tree)
  630. inputs = [
  631. "Input Relationship" ,
  632. "Use Max X" ,
  633. "Max X" ,
  634. "Use Max Y" ,
  635. "Max Y" ,
  636. "Use Max Z" ,
  637. "Max Z" ,
  638. "Use Min X" ,
  639. "Min X" ,
  640. "Use Min Y" ,
  641. "Min Y" ,
  642. "Use Min Z" ,
  643. "Min Z" ,
  644. "Affect Transform" ,
  645. "Owner Space" ,
  646. "Influence" ,
  647. "Enable" ,
  648. ]
  649. additional_parameters = { "Name":None }
  650. self.inputs.init_sockets(inputs)
  651. self.outputs.init_sockets(["Output Relationship"])
  652. self.init_parameters(additional_parameters=additional_parameters)
  653. self.set_traverse([("Input Relationship", "Output Relationship")])
  654. def GetxForm(self):
  655. return GetxForm(self)
  656. def bExecute(self, context):
  657. prepare_parameters(self)
  658. c = self.GetxForm().bGetObject().constraints.new('LIMIT_SCALE')
  659. print(wrapGreen("Creating ")+wrapWhite("Limit Scale")+
  660. wrapGreen(" Constraint for bone: ") +
  661. wrapOrange(self.GetxForm().bGetObject().name))
  662. if constraint_name := self.evaluate_input("Name"):
  663. c.name = constraint_name
  664. self.bObject = c
  665. custom_space_owner = False
  666. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  667. custom_space_owner=True
  668. c.owner_space='CUSTOM'
  669. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  670. if isinstance(xf, Bone):
  671. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  672. else:
  673. c.space_object=xf
  674. props_sockets = {
  675. 'use_transform_limit' : ("Affect Transform", False),
  676. 'use_max_x' : ("Use Max X", False),
  677. 'use_max_y' : ("Use Max Y", False),
  678. 'use_max_z' : ("Use Max Z", False),
  679. 'use_min_x' : ("Use Min X", False),
  680. 'use_min_y' : ("Use Min Y", False),
  681. 'use_min_z' : ("Use Min Z", False),
  682. 'max_x' : ("Max X", 0),
  683. 'max_y' : ("Max Y", 0),
  684. 'max_z' : ("Max Z", 0),
  685. 'min_x' : ("Min X", 0),
  686. 'min_y' : ("Min Y", 0),
  687. 'min_z' : ("Min Z", 0),
  688. 'owner_space' : ("Owner Space", 'WORLD'),
  689. 'influence' : ("Influence", 1),
  690. 'mute' : ("Enable", True),
  691. }
  692. if custom_space_owner: del props_sockets['owner_space']
  693. #
  694. evaluate_sockets(self, c, props_sockets)
  695. self.executed = True
  696. def bFinalize(self, bContext = None):
  697. finish_drivers(self)
  698. class LinkLimitDistance(MantisLinkNode):
  699. def __init__(self, signature, base_tree):
  700. super().__init__(signature, base_tree)
  701. inputs = [
  702. "Input Relationship" ,
  703. "Head/Tail" ,
  704. "UseBBone" ,
  705. "Distance" ,
  706. "Clamp Region" ,
  707. "Affect Transform" ,
  708. "Owner Space" ,
  709. "Target Space" ,
  710. "Influence" ,
  711. "Target" ,
  712. "Enable" ,
  713. ]
  714. additional_parameters = { "Name":None }
  715. self.inputs.init_sockets(inputs)
  716. self.outputs.init_sockets(["Output Relationship"])
  717. self.init_parameters(additional_parameters=additional_parameters)
  718. self.set_traverse([("Input Relationship", "Output Relationship")])
  719. def GetxForm(self):
  720. return GetxForm(self)
  721. def bExecute(self, context):
  722. prepare_parameters(self)
  723. print(wrapGreen("Creating ")+wrapWhite("Limit Distance")+
  724. wrapGreen(" Constraint for bone: ") +
  725. wrapOrange(self.GetxForm().bGetObject().name))
  726. c = self.GetxForm().bGetObject().constraints.new('LIMIT_DISTANCE')
  727. get_target_and_subtarget(self, c)
  728. if constraint_name := self.evaluate_input("Name"):
  729. c.name = constraint_name
  730. self.bObject = c
  731. #
  732. # TODO: set distance automagically
  733. # IMPORTANT TODO BUG
  734. custom_space_owner, custom_space_target = False, False
  735. if self.inputs["Owner Space"].is_connected and self.inputs["Owner Space"].links[0].from_node.node_type == 'XFORM':
  736. custom_space_owner=True
  737. c.owner_space='CUSTOM'
  738. xf = self.inputs["Owner Space"].links[0].from_node.bGetObject(mode="OBJECT")
  739. if isinstance(xf, Bone):
  740. c.space_object=self.inputs["Owner Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  741. else:
  742. c.space_object=xf
  743. if self.inputs["Target Space"].is_connected and self.inputs["Target Space"].links[0].from_node.node_type == 'XFORM':
  744. custom_space_target=True
  745. c.target_space='CUSTOM'
  746. xf = self.inputs["Target Space"].links[0].from_node.bGetObject(mode="OBJECT")
  747. if isinstance(xf, Bone):
  748. c.space_object=self.inputs["Target Space"].links[0].from_node.bGetParentArmature(); c.space_subtarget=xf.name
  749. else:
  750. c.space_object=xf
  751. props_sockets = {
  752. 'distance' : ("Distance", 0),
  753. 'head_tail' : ("Head/Tail", 0),
  754. 'limit_mode' : ("Clamp Region", "LIMITDIST_INSIDE"),
  755. 'use_bbone_shape' : ("UseBBone", False),
  756. 'use_transform_limit' : ("Affect Transform", 1),
  757. 'owner_space' : ("Owner Space", 1),
  758. 'target_space' : ("Target Space", 1),
  759. 'influence' : ("Influence", 1),
  760. 'mute' : ("Enable", True),
  761. }
  762. if custom_space_owner: del props_sockets['owner_space']
  763. if custom_space_target: del props_sockets['target_space']
  764. #
  765. evaluate_sockets(self, c, props_sockets)
  766. self.executed = True
  767. def bFinalize(self, bContext = None):
  768. finish_drivers(self)
  769. # Tracking
  770. class LinkStretchTo(MantisLinkNode):
  771. def __init__(self, signature, base_tree):
  772. super().__init__(signature, base_tree)
  773. inputs = [
  774. "Input Relationship" ,
  775. "Head/Tail" ,
  776. "UseBBone" ,
  777. "Original Length" ,
  778. "Volume Variation" ,
  779. "Use Volume Min" ,
  780. "Volume Min" ,
  781. "Use Volume Max" ,
  782. "Volume Max" ,
  783. "Smooth" ,
  784. "Maintain Volume" ,
  785. "Rotation" ,
  786. "Influence" ,
  787. "Target" ,
  788. "Enable" ,
  789. ]
  790. additional_parameters = { "Name":None }
  791. self.inputs.init_sockets(inputs)
  792. self.outputs.init_sockets(["Output Relationship"])
  793. self.init_parameters(additional_parameters=additional_parameters)
  794. self.set_traverse([("Input Relationship", "Output Relationship")])
  795. def GetxForm(self):
  796. return GetxForm(self)
  797. def bExecute(self, context):
  798. prepare_parameters(self)
  799. print(wrapGreen("Creating ")+wrapWhite("Stretch-To")+
  800. wrapGreen(" Constraint for bone: ") +
  801. wrapOrange(self.GetxForm().bGetObject().name))
  802. c = self.GetxForm().bGetObject().constraints.new('STRETCH_TO')
  803. get_target_and_subtarget(self, c)
  804. if constraint_name := self.evaluate_input("Name"):
  805. c.name = constraint_name
  806. self.bObject = c
  807. props_sockets = {
  808. 'head_tail' : ("Head/Tail", 0),
  809. 'use_bbone_shape' : ("UseBBone", False),
  810. 'bulge' : ("Volume Variation", 0),
  811. 'use_bulge_min' : ("Use Volume Min", False),
  812. 'bulge_min' : ("Volume Min", 0),
  813. 'use_bulge_max' : ("Use Volume Max", False),
  814. 'bulge_max' : ("Volume Max", 0),
  815. 'bulge_smooth' : ("Smooth", 0),
  816. 'volume' : ("Maintain Volume", 'VOLUME_XZX'),
  817. 'keep_axis' : ("Rotation", 'PLANE_X'),
  818. 'rest_length' : ("Original Length", self.GetxForm().bGetObject().bone.length),
  819. 'influence' : ("Influence", 1),
  820. 'mute' : ("Enable", True),
  821. }
  822. evaluate_sockets(self, c, props_sockets)
  823. if (self.evaluate_input("Original Length") == 0):
  824. # this is meant to be set automatically.
  825. c.rest_length = self.GetxForm().bGetObject().bone.length
  826. self.executed = True
  827. def bFinalize(self, bContext = None):
  828. finish_drivers(self)
  829. class LinkDampedTrack(MantisLinkNode):
  830. def __init__(self, signature, base_tree):
  831. super().__init__(signature, base_tree)
  832. inputs = [
  833. "Input Relationship" ,
  834. "Head/Tail" ,
  835. "UseBBone" ,
  836. "Track Axis" ,
  837. "Influence" ,
  838. "Target" ,
  839. "Enable" ,
  840. ]
  841. additional_parameters = { "Name":None }
  842. self.inputs.init_sockets(inputs)
  843. self.outputs.init_sockets(["Output Relationship"])
  844. self.init_parameters(additional_parameters=additional_parameters)
  845. self.set_traverse([("Input Relationship", "Output Relationship")])
  846. def GetxForm(self):
  847. return GetxForm(self)
  848. def bExecute(self, context):
  849. prepare_parameters(self)
  850. print(wrapGreen("Creating ")+wrapWhite("Damped Track")+
  851. wrapGreen(" Constraint for bone: ") +
  852. wrapOrange(self.GetxForm().bGetObject().name))
  853. c = self.GetxForm().bGetObject().constraints.new('DAMPED_TRACK')
  854. get_target_and_subtarget(self, c)
  855. if constraint_name := self.evaluate_input("Name"):
  856. c.name = constraint_name
  857. self.bObject = c
  858. props_sockets = {
  859. 'head_tail' : ("Head/Tail", 0),
  860. 'use_bbone_shape' : ("UseBBone", False),
  861. 'track_axis' : ("Track Axis", 'TRACK_Y'),
  862. 'influence' : ("Influence", 1),
  863. 'mute' : ("Enable", True),
  864. }
  865. evaluate_sockets(self, c, props_sockets)
  866. self.executed = True
  867. def bFinalize(self, bContext = None):
  868. finish_drivers(self)
  869. class LinkLockedTrack(MantisLinkNode):
  870. def __init__(self, signature, base_tree):
  871. super().__init__(signature, base_tree)
  872. inputs = [
  873. "Input Relationship" ,
  874. "Head/Tail" ,
  875. "UseBBone" ,
  876. "Track Axis" ,
  877. "Lock Axis" ,
  878. "Influence" ,
  879. "Target" ,
  880. "Enable" ,
  881. ]
  882. additional_parameters = { "Name":None }
  883. self.inputs.init_sockets(inputs)
  884. self.outputs.init_sockets(["Output Relationship"])
  885. self.init_parameters(additional_parameters=additional_parameters)
  886. self.set_traverse([("Input Relationship", "Output Relationship")])
  887. def GetxForm(self):
  888. return GetxForm(self)
  889. def bExecute(self, context):
  890. prepare_parameters(self)
  891. print(wrapGreen("Creating ")+wrapWhite("Locked Track")+
  892. wrapGreen(" Constraint for bone: ") +
  893. wrapOrange(self.GetxForm().bGetObject().name))
  894. c = self.GetxForm().bGetObject().constraints.new('LOCKED_TRACK')
  895. get_target_and_subtarget(self, c)
  896. if constraint_name := self.evaluate_input("Name"):
  897. c.name = constraint_name
  898. self.bObject = c
  899. props_sockets = {
  900. 'head_tail' : ("Head/Tail", 0),
  901. 'use_bbone_shape' : ("UseBBone", False),
  902. 'track_axis' : ("Track Axis", 'TRACK_Y'),
  903. 'lock_axis' : ("Lock Axis", 'UP_X'),
  904. 'influence' : ("Influence", 1),
  905. 'mute' : ("Enable", True),
  906. }
  907. evaluate_sockets(self, c, props_sockets)
  908. self.executed = True
  909. def bFinalize(self, bContext = None):
  910. finish_drivers(self)
  911. class LinkTrackTo(MantisLinkNode):
  912. def __init__(self, signature, base_tree):
  913. super().__init__(signature, base_tree)
  914. inputs = [
  915. "Input Relationship" ,
  916. "Head/Tail" ,
  917. "UseBBone" ,
  918. "Track Axis" ,
  919. "Up Axis" ,
  920. "Use Target Z" ,
  921. "Influence" ,
  922. "Target" ,
  923. "Enable" ,
  924. ]
  925. additional_parameters = { "Name":None }
  926. self.inputs.init_sockets(inputs)
  927. self.outputs.init_sockets(["Output Relationship"])
  928. self.init_parameters(additional_parameters=additional_parameters)
  929. self.set_traverse([("Input Relationship", "Output Relationship")])
  930. def GetxForm(self):
  931. return GetxForm(self)
  932. def bExecute(self, context):
  933. prepare_parameters(self)
  934. print(wrapGreen("Creating ")+wrapWhite("Track-To")+
  935. wrapGreen(" Constraint for bone: ") +
  936. wrapOrange(self.GetxForm().bGetObject().name))
  937. c = self.GetxForm().bGetObject().constraints.new('TRACK_TO')
  938. get_target_and_subtarget(self, c)
  939. if constraint_name := self.evaluate_input("Name"):
  940. c.name = constraint_name
  941. self.bObject = c
  942. props_sockets = {
  943. 'head_tail' : ("Head/Tail", 0),
  944. 'use_bbone_shape' : ("UseBBone", False),
  945. 'track_axis' : ("Track Axis", "TRACK_Y"),
  946. 'up_axis' : ("Up Axis", "UP_Z"),
  947. 'use_target_z' : ("Use Target Z", False),
  948. 'influence' : ("Influence", 1),
  949. 'mute' : ("Enable", True),
  950. }
  951. evaluate_sockets(self, c, props_sockets)
  952. self.executed = True
  953. def bFinalize(self, bContext = None):
  954. finish_drivers(self)
  955. # relationships & misc.
  956. class LinkInheritConstraint(MantisLinkNode):
  957. def __init__(self, signature, base_tree):
  958. super().__init__(signature, base_tree)
  959. inputs = [
  960. "Input Relationship" ,
  961. "Location" ,
  962. "Rotation" ,
  963. "Scale" ,
  964. "Influence" ,
  965. "Target" ,
  966. "Enable" ,
  967. ]
  968. additional_parameters = { "Name":None }
  969. self.inputs.init_sockets(inputs)
  970. self.outputs.init_sockets(["Output Relationship"])
  971. self.init_parameters(additional_parameters=additional_parameters)
  972. self.set_traverse([("Input Relationship", "Output Relationship")])
  973. def GetxForm(self):
  974. return GetxForm(self)
  975. def bExecute(self, context):
  976. prepare_parameters(self)
  977. print(wrapGreen("Creating ")+wrapWhite("Child-Of")+
  978. wrapGreen(" Constraint for bone: ") +
  979. wrapOrange(self.GetxForm().bGetObject().name))
  980. c = self.GetxForm().bGetObject().constraints.new('CHILD_OF')
  981. get_target_and_subtarget(self, c)
  982. if constraint_name := self.evaluate_input("Name"):
  983. c.name = constraint_name
  984. self.bObject = c
  985. props_sockets = {
  986. 'use_location_x' : (("Location", 0) , 1),
  987. 'use_location_y' : (("Location", 1) , 1),
  988. 'use_location_z' : (("Location", 2) , 1),
  989. 'use_rotation_x' : (("Rotation", 0) , 1),
  990. 'use_rotation_y' : (("Rotation", 1) , 1),
  991. 'use_rotation_z' : (("Rotation", 2) , 1),
  992. 'use_scale_x' : (("Scale" , 0) , 1),
  993. 'use_scale_y' : (("Scale" , 1) , 1),
  994. 'use_scale_z' : (("Scale" , 2) , 1),
  995. 'influence' : ( "Influence" , 1),
  996. 'mute' : ("Enable", True),
  997. }
  998. evaluate_sockets(self, c, props_sockets)
  999. c.set_inverse_pending
  1000. self.executed = True
  1001. def bFinalize(self, bContext = None):
  1002. finish_drivers(self)
  1003. class LinkInverseKinematics(MantisLinkNode):
  1004. def __init__(self, signature, base_tree):
  1005. super().__init__(signature, base_tree)
  1006. inputs = [
  1007. "Input Relationship" ,
  1008. "Chain Length" ,
  1009. "Use Tail" ,
  1010. "Stretch" ,
  1011. "Position" ,
  1012. "Rotation" ,
  1013. "Influence" ,
  1014. "Target" ,
  1015. "Pole Target" ,
  1016. "Enable" ,
  1017. ]
  1018. additional_parameters = { "Name":None }
  1019. self.inputs.init_sockets(inputs)
  1020. self.outputs.init_sockets(["Output Relationship"])
  1021. self.init_parameters(additional_parameters=additional_parameters)
  1022. self.set_traverse([("Input Relationship", "Output Relationship")])
  1023. def GetxForm(self):
  1024. return GetxForm(self)
  1025. def get_base_ik_bone(self, ik_bone):
  1026. chain_length : int = (self.evaluate_input("Chain Length"))
  1027. if not isinstance(chain_length, (int, float)):
  1028. raise GraphError(f"Chain Length must be an integer number in {self}::Chain Length")
  1029. if chain_length == 0:
  1030. chain_length = int("inf")
  1031. base_ik_bone = ik_bone; i=1
  1032. while (i<chain_length) and (base_ik_bone.parent):
  1033. base_ik_bone=base_ik_bone.parent
  1034. return base_ik_bone
  1035. # We need to do the calculation in a "full circle", meaning the pole_angle
  1036. # can go over pi or less than -pi - but the actuall constraint value must
  1037. # be clamped in that range.
  1038. # so we simply wrap the value.
  1039. # not very efficient but it's OK
  1040. def set_pole_angle(self, angle: float) -> None:
  1041. from math import pi
  1042. def wrap(min : float, max : float, value: float) -> float:
  1043. range = max-min; remainder = value % range
  1044. if remainder > max: return min + remainder-max
  1045. else: return remainder
  1046. self.bObject.pole_angle = wrap(-pi, pi, angle)
  1047. def calc_pole_angle_pre(self, c, ik_bone):
  1048. """
  1049. This function gets us most of the way to a correct IK pole angle. Unfortunately,
  1050. due to the unpredictable nature of the iterative IK calculation, I can't figure
  1051. out an exact solution. So we do a bisect search in calc_pole_angle_post().
  1052. """
  1053. # TODO: instead of these checks, convert all to armature local space. But this is tedious.
  1054. if not c.target:
  1055. raise GraphError(f"IK Constraint {self} must have target.")
  1056. elif c.target.type != "ARMATURE":
  1057. raise NotImplementedError(f"Currently, IK Constraint Target for {self} must be a bone within the same armature.")
  1058. if c.pole_target.type != "ARMATURE":
  1059. raise NotImplementedError(f"Currently, IK Constraint Pole Target for {self} must be a bone within the same armature.")
  1060. ik_handle = c.target.pose.bones[c.subtarget]
  1061. if ik_handle.id_data != ik_bone.id_data:
  1062. raise NotImplementedError(f"Currently, IK Constraint Target for {self} must be a bone within the same armature.")
  1063. ik_pole = c.pole_target.pose.bones[c.pole_subtarget]
  1064. if ik_pole.id_data != ik_bone.id_data:
  1065. raise NotImplementedError(f"Currently,IK Constraint Pole Target for {self} must be a bone within the same armature.")
  1066. base_ik_bone = self.get_base_ik_bone(ik_bone)
  1067. start_effector = base_ik_bone.bone.head_local
  1068. end_effector = ik_handle.bone.head_local
  1069. pole_location = ik_pole.bone.head_local
  1070. # this is the X-Axis of the bone's rest-pose, added to its bone
  1071. knee_location = base_ik_bone.bone.matrix_local.col[0].xyz+start_effector
  1072. ik_axis = (end_effector-start_effector).normalized()
  1073. from .utilities import project_point_to_plane
  1074. pole_planar_projection = project_point_to_plane(pole_location, start_effector, ik_axis)
  1075. # this planar projection is necessary because the IK axis is different than the base_bone's y axis
  1076. planar_projection = project_point_to_plane(knee_location, start_effector, ik_axis)
  1077. knee_direction =(planar_projection - start_effector).normalized()
  1078. pole_direction =(pole_planar_projection - start_effector).normalized()
  1079. return knee_direction.angle(pole_direction)
  1080. def calc_pole_angle_post(self, c, ik_bone, context):
  1081. """
  1082. This function should give us a completely accurate result for IK.
  1083. """
  1084. from time import time
  1085. start_time=time()
  1086. def signed_angle(vector_u, vector_v, normal):
  1087. # it seems that this fails if the vectors are exactly aligned under certain circumstances.
  1088. angle = vector_u.angle(vector_v, 0.0) # So we use a fallback of 0
  1089. # Normal specifies orientation
  1090. if angle != 0 and vector_u.cross(vector_v).angle(normal) < 1:
  1091. angle = -angle
  1092. return angle
  1093. # we have already checked for valid data.
  1094. ik_handle = c.target.pose.bones[c.subtarget]
  1095. base_ik_bone = self.get_base_ik_bone(ik_bone)
  1096. start_effector = base_ik_bone.bone.head_local
  1097. angle = c.pole_angle
  1098. dg = context.view_layer.depsgraph
  1099. dg.update()
  1100. ik_axis = (ik_handle.bone.head_local-start_effector).normalized()
  1101. center_point = start_effector +(ik_axis*base_ik_bone.bone.length)
  1102. knee_direction = base_ik_bone.bone.tail_local - center_point
  1103. current_knee_direction = base_ik_bone.tail-center_point
  1104. error=signed_angle(current_knee_direction, knee_direction, ik_axis)
  1105. if error == 0:
  1106. prGreen("No Fine-tuning needed."); return
  1107. # Flip it if needed
  1108. dot_before=current_knee_direction.dot(knee_direction)
  1109. if dot_before < 0 and angle!=0: # then it is not aligned and we should check the inverse
  1110. angle = -angle; c.pole_angle=angle
  1111. dg.update()
  1112. current_knee_direction = base_ik_bone.tail-center_point
  1113. dot_after=current_knee_direction.dot(knee_direction)
  1114. if dot_after < dot_before: # they are somehow less aligned
  1115. prPurple("Mantis has gone down an unexpected code path. Please report this as a bug.")
  1116. angle = -angle; self.set_pole_angle(angle)
  1117. dg.update()
  1118. # now we can do a bisect search to find the best value.
  1119. error_threshhold = FLOAT_EPSILON
  1120. max_iterations=600
  1121. error=signed_angle(current_knee_direction, knee_direction, ik_axis)
  1122. if error == 0:
  1123. prGreen("No Fine-tuning needed."); return
  1124. angle+=error
  1125. alt_angle = angle+(error*-2) # should be very near the center when flipped here
  1126. # we still need to bisect search because the relationship of pole_angle <==> error is somewhat unpredictable
  1127. upper_bounds = alt_angle if alt_angle > angle else angle
  1128. lower_bounds = alt_angle if alt_angle < angle else angle
  1129. i=0
  1130. while ( True ):
  1131. if (i>=max_iterations):
  1132. prOrange(f"IK Pole Angle Set reached max iterations of {i} in {time()-start_time} seconds")
  1133. break
  1134. if (abs(error)<error_threshhold) or (upper_bounds<=lower_bounds):
  1135. prPurple(f"IK Pole Angle Set converged after {i} iterations with error={error} in {time()-start_time} seconds")
  1136. break
  1137. # get the center-point betweeen the bounds
  1138. try_angle = lower_bounds + (upper_bounds-lower_bounds)/2
  1139. self.set_pole_angle(try_angle); dg.update()
  1140. error=signed_angle((base_ik_bone.tail-center_point), knee_direction, ik_axis)
  1141. if error>0: upper_bounds=try_angle
  1142. if error<0: lower_bounds=try_angle
  1143. i+=1
  1144. def bExecute(self, context):
  1145. prepare_parameters(self)
  1146. print(wrapGreen("Creating ")+wrapOrange("Inverse Kinematics")+
  1147. wrapGreen(" Constraint for bone: ") +
  1148. wrapOrange(self.GetxForm().bGetObject().name))
  1149. ik_bone = self.GetxForm().bGetObject()
  1150. c = self.GetxForm().bGetObject().constraints.new('IK')
  1151. get_target_and_subtarget(self, c)
  1152. get_target_and_subtarget(self, c, input_name = 'Pole Target')
  1153. if constraint_name := self.evaluate_input("Name"):
  1154. c.name = constraint_name
  1155. self.bObject = c
  1156. c.chain_count = 1 # so that, if there are errors, this doesn't print a whole bunch of circular dependency crap from having infinite chain length
  1157. if (c.pole_target): # Calculate the pole angle, the user shouldn't have to.
  1158. # my_xf = self.GetxForm()
  1159. # from .xForm_containers import xFormBone
  1160. # if not isinstance(my_xf, xFormBone):
  1161. # raise GraphError(f"ERROR: Pole Target must be ")
  1162. # if c.target !=
  1163. self.set_pole_angle(self.calc_pole_angle_pre(c, ik_bone))
  1164. props_sockets = {
  1165. 'chain_count' : ("Chain Length", 1),
  1166. 'use_tail' : ("Use Tail", True),
  1167. 'use_stretch' : ("Stretch", True),
  1168. "weight" : ("Position", 1.0),
  1169. "orient_weight" : ("Rotation", 0.0),
  1170. "influence" : ("Influence", 1.0),
  1171. 'mute' : ("Enable", True),
  1172. }
  1173. evaluate_sockets(self, c, props_sockets)
  1174. # TODO: handle drivers
  1175. # (it should be assumed we want it on if it's plugged
  1176. # into a driver).
  1177. c.use_location = self.evaluate_input("Position") > 0
  1178. c.use_rotation = self.evaluate_input("Rotation") > 0
  1179. self.executed = True
  1180. def bFinalize(self, bContext = None):
  1181. # adding a test here
  1182. if bContext:
  1183. ik_bone = self.GetxForm().bGetObject(mode='POSE')
  1184. if self.bObject.pole_target:
  1185. prWhite(f"Fine-tuning IK Pole Angle for {self}")
  1186. self.calc_pole_angle_post(self.bObject, ik_bone, bContext)
  1187. finish_drivers(self)
  1188. def ik_report_error(pb, context, do_print=False):
  1189. dg = context.view_layer.depsgraph
  1190. dg.update()
  1191. loc1, rot_quaternion1, scl1 = pb.matrix.decompose()
  1192. loc2, rot_quaternion2, scl2 = pb.bone.matrix_local.decompose()
  1193. location_error=(loc1-loc2).length
  1194. rotation_error = rot_quaternion1.rotation_difference(rot_quaternion2).angle
  1195. scale_error = (scl1-scl2).length
  1196. if location_error < FLOAT_EPSILON: location_error = 0
  1197. if abs(rotation_error) < FLOAT_EPSILON: rotation_error = 0
  1198. if scale_error < FLOAT_EPSILON: scale_error = 0
  1199. if do_print:
  1200. print (f"IK Location Error: {location_error}")
  1201. print (f"IK Rotation Error: {rotation_error}")
  1202. print (f"IK Scale Error : {scale_error}")
  1203. return (location_error, rotation_error, scale_error)
  1204. # This is kinda a weird design decision?
  1205. class LinkDrivenParameter(MantisLinkNode):
  1206. '''A node representing an armature object'''
  1207. def __init__(self, signature, base_tree):
  1208. self.base_tree=base_tree
  1209. inputs = [
  1210. "Input Relationship" ,
  1211. "Value" ,
  1212. "Parameter" ,
  1213. "Index" ,
  1214. ]
  1215. self.signature = signature
  1216. additional_parameters = { "Name":None }
  1217. self.inputs.init_sockets(inputs)
  1218. self.outputs.init_sockets(["Output Relationship"])
  1219. self.init_parameters(additional_parameters=additional_parameters)
  1220. self.set_traverse([("Input Relationship", "Output Relationship")])
  1221. def GetxForm(self):
  1222. return GetxForm(self)
  1223. def bExecute(self, bContext = None,):
  1224. prepare_parameters(self)
  1225. prGreen("Executing Driven Parameter node")
  1226. prop = self.evaluate_input("Parameter")
  1227. index = self.evaluate_input("Index")
  1228. value = self.evaluate_input("Value")
  1229. xf = self.GetxForm()
  1230. ob = xf.bGetObject(mode="POSE")
  1231. # IMPORTANT: this node only works on pose bone attributes.
  1232. self.bObject = ob
  1233. length=1
  1234. if hasattr(ob, prop):
  1235. try:
  1236. length = len(getattr(ob, prop))
  1237. except TypeError:
  1238. pass
  1239. except AttributeError:
  1240. pass
  1241. else:
  1242. raise AttributeError(f"Cannot Set value {prop} on object because it does not exist.")
  1243. def_value = 0.0
  1244. if length>1:
  1245. def_value=[0.0]*length
  1246. self.parameters["Value"] = tuple( 0.0 if i != index else value for i in range(length))
  1247. props_sockets = {
  1248. prop: ("Value", def_value)
  1249. }
  1250. evaluate_sockets(self, ob, props_sockets)
  1251. self.executed = True
  1252. def bFinalize(self, bContext = None):
  1253. driver = self.evaluate_input("Value")
  1254. try:
  1255. for i, val in enumerate(self.parameters["Value"]):
  1256. from .drivers import MantisDriver
  1257. if isinstance(val, MantisDriver):
  1258. driver["ind"] = i
  1259. val = driver
  1260. except AttributeError:
  1261. self.parameters["Value"] = driver
  1262. except TypeError:
  1263. self.parameters["Value"] = driver
  1264. finish_drivers(self)
  1265. class LinkArmature(MantisLinkNode):
  1266. '''A node representing an armature object'''
  1267. def __init__(self, signature, base_tree,):
  1268. super().__init__(signature, base_tree)
  1269. inputs = [
  1270. "Input Relationship" ,
  1271. "Preserve Volume" ,
  1272. "Use Envelopes" ,
  1273. "Use Current Location" ,
  1274. "Influence" ,
  1275. "Enable" ,
  1276. ]
  1277. additional_parameters = { "Name":None }
  1278. self.inputs.init_sockets(inputs)
  1279. self.outputs.init_sockets(["Output Relationship"])
  1280. self.init_parameters(additional_parameters=additional_parameters)
  1281. self.set_traverse([("Input Relationship", "Output Relationship")])
  1282. setup_custom_props(self)
  1283. def GetxForm(self):
  1284. return GetxForm(self)
  1285. def bExecute(self, bContext = None,):
  1286. prGreen("Creating Armature Constraint for bone: \""+ self.GetxForm().bGetObject().name + "\"")
  1287. prepare_parameters(self)
  1288. c = self.GetxForm().bGetObject().constraints.new('ARMATURE')
  1289. if constraint_name := self.evaluate_input("Name"):
  1290. c.name = constraint_name
  1291. self.bObject = c
  1292. # get number of targets
  1293. num_targets = len( list(self.inputs.values())[6:] )//2
  1294. props_sockets = {
  1295. 'use_deform_preserve_volume' : ("Preserve Volume", 0),
  1296. 'use_bone_envelopes' : ("Use Envelopes", 0),
  1297. 'use_current_location' : ("Use Current Location", 0),
  1298. 'influence' : ( "Influence" , 1),
  1299. 'mute' : ("Enable", True),
  1300. }
  1301. targets_weights = {}
  1302. for i in range(num_targets):
  1303. target = c.targets.new()
  1304. target_input_name = list(self.inputs.keys())[i*2+6 ]
  1305. weight_input_name = list(self.inputs.keys())[i*2+6+1]
  1306. get_target_and_subtarget(self, target, target_input_name)
  1307. weight_value=self.evaluate_input(weight_input_name)
  1308. if not isinstance(weight_value, float):
  1309. weight_value=0
  1310. targets_weights[i]=weight_value
  1311. props_sockets["targets[%d].weight" % i] = (weight_input_name, 0)
  1312. # targets_weights.append({"weight":(weight_input_name, 0)})
  1313. evaluate_sockets(self, c, props_sockets)
  1314. for target, value in targets_weights.items():
  1315. c.targets[target].weight=value
  1316. # for i, (target, weight) in enumerate(zip(c.targets, targets_weights)):
  1317. # evaluate_sockets(self, target, weight)
  1318. self.executed = True
  1319. def bFinalize(self, bContext = None):
  1320. finish_drivers(self)
  1321. class LinkSplineIK(MantisLinkNode):
  1322. '''A node representing an armature object'''
  1323. def __init__(self, signature, base_tree):
  1324. super().__init__(signature, base_tree)
  1325. inputs = [
  1326. "Input Relationship" ,
  1327. "Target" ,
  1328. "Chain Length" ,
  1329. "Even Divisions" ,
  1330. "Chain Offset" ,
  1331. "Use Curve Radius" ,
  1332. "Y Scale Mode" ,
  1333. "XZ Scale Mode" ,
  1334. "Use Original Scale" ,
  1335. "Influence" ,
  1336. ]
  1337. additional_parameters = { "Name":None }
  1338. self.inputs.init_sockets(inputs)
  1339. self.outputs.init_sockets(["Output Relationship"])
  1340. self.init_parameters(additional_parameters=additional_parameters)
  1341. self.set_traverse([("Input Relationship", "Output Relationship")])
  1342. def GetxForm(self):
  1343. return GetxForm(self)
  1344. def bExecute(self, bContext = None,):
  1345. prepare_parameters(self)
  1346. prGreen("Creating Spline-IK Constraint for bone: \""+ self.GetxForm().bGetObject().name + "\"")
  1347. c = self.GetxForm().bGetObject().constraints.new('SPLINE_IK')
  1348. get_target_and_subtarget(self, c)
  1349. if constraint_name := self.evaluate_input("Name"):
  1350. c.name = constraint_name
  1351. self.bObject = c
  1352. props_sockets = {
  1353. 'chain_count' : ("Chain Length", 0),
  1354. 'use_even_divisions' : ("Even Divisions", False),
  1355. 'use_chain_offset' : ("Chain Offset", False),
  1356. 'use_curve_radius' : ("Use Curve Radius", False),
  1357. 'y_scale_mode' : ("Y Scale Mode", "FIT_CURVE"),
  1358. 'xz_scale_mode' : ("XZ Scale Mode", "NONE"),
  1359. 'use_original_scale' : ("Use Original Scale", False),
  1360. 'influence' : ("Influence", 1),
  1361. }
  1362. evaluate_sockets(self, c, props_sockets)
  1363. self.executed = True