link_containers.py 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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. def calc_pole_angle_pre(self, c, ik_bone):
  1036. """
  1037. This function gets us most of the way to a correct IK pole angle. Unfortunately,
  1038. due to the unpredictable nature of the iterative IK calculation, I can't figure
  1039. out an exact solution. So we do a bisect search in calc_pole_angle_post().
  1040. """
  1041. # TODO: instead of these checks, convert all to armature local space. But this is tedious.
  1042. if not c.target:
  1043. raise GraphError(f"IK Constraint {self} must have target.")
  1044. elif c.target.type != "ARMATURE":
  1045. raise NotImplementedError(f"Currently, IK Constraint Target for {self} must be a bone within the same armature.")
  1046. if c.pole_target.type != "ARMATURE":
  1047. raise NotImplementedError(f"Currently, IK Constraint Pole Target for {self} must be a bone within the same armature.")
  1048. ik_handle = c.target.pose.bones[c.subtarget]
  1049. if ik_handle.id_data != ik_bone.id_data:
  1050. raise NotImplementedError(f"Currently, IK Constraint Target for {self} must be a bone within the same armature.")
  1051. ik_pole = c.pole_target.pose.bones[c.pole_subtarget]
  1052. if ik_pole.id_data != ik_bone.id_data:
  1053. raise NotImplementedError(f"Currently,IK Constraint Pole Target for {self} must be a bone within the same armature.")
  1054. base_ik_bone = self.get_base_ik_bone(ik_bone)
  1055. start_effector = base_ik_bone.bone.head_local
  1056. end_effector = ik_handle.bone.head_local
  1057. pole_location = ik_pole.bone.head_local
  1058. # this is the X-Axis of the bone's rest-pose, added to its bone
  1059. knee_location = base_ik_bone.bone.matrix_local.col[0].xyz+start_effector
  1060. ik_axis = (end_effector-start_effector).normalized()
  1061. from .utilities import project_point_to_plane
  1062. pole_planar_projection = project_point_to_plane(pole_location, start_effector, ik_axis)
  1063. # this planar projection is necessary because the IK axis is different than the base_bone's y axis
  1064. planar_projection = project_point_to_plane(knee_location, start_effector, ik_axis)
  1065. knee_direction =(planar_projection - start_effector).normalized()
  1066. pole_direction =(pole_planar_projection - start_effector).normalized()
  1067. return knee_direction.angle(pole_direction)
  1068. def calc_pole_angle_post(self, c, ik_bone, context):
  1069. """
  1070. This function should give us a completely accurate result for IK.
  1071. """
  1072. from time import time
  1073. start_time=time()
  1074. def signed_angle(vector_u, vector_v, normal):
  1075. # it seems that this fails if the vectors are exactly aligned under certain circumstances.
  1076. angle = vector_u.angle(vector_v, 0.0) # So we use a fallback of 0
  1077. # Normal specifies orientation
  1078. if angle != 0 and vector_u.cross(vector_v).angle(normal) < 1:
  1079. angle = -angle
  1080. return angle
  1081. # we have already checked for valid data.
  1082. ik_handle = c.target.pose.bones[c.subtarget]
  1083. base_ik_bone = self.get_base_ik_bone(ik_bone)
  1084. start_effector = base_ik_bone.bone.head_local
  1085. angle = c.pole_angle
  1086. dg = context.view_layer.depsgraph
  1087. dg.update()
  1088. ik_axis = (ik_handle.bone.head_local-start_effector).normalized()
  1089. center_point = start_effector +(ik_axis*base_ik_bone.bone.length)
  1090. knee_direction = base_ik_bone.bone.tail_local - center_point
  1091. current_knee_direction = base_ik_bone.tail-center_point
  1092. error=signed_angle(current_knee_direction, knee_direction, ik_axis)
  1093. if error == 0:
  1094. prGreen("No Fine-tuning needed."); return
  1095. # Flip it if needed
  1096. dot_before=current_knee_direction.dot(knee_direction)
  1097. if dot_before < 0 and angle!=0: # then it is not aligned and we should check the inverse
  1098. angle = -angle; c.pole_angle=angle
  1099. dg.update()
  1100. current_knee_direction = base_ik_bone.tail-center_point
  1101. dot_after=current_knee_direction.dot(knee_direction)
  1102. if dot_after < dot_before: # they are somehow less aligned
  1103. prPurple("Mantis has gone down an unexpected code path. Please report this as a bug.")
  1104. angle = -angle; c.pole_angle = angle
  1105. dg.update()
  1106. # now we can do a bisect search to find the best value.
  1107. error_threshhold = FLOAT_EPSILON
  1108. max_iterations=600
  1109. error=signed_angle(current_knee_direction, knee_direction, ik_axis)
  1110. if error == 0:
  1111. prGreen("No Fine-tuning needed."); return
  1112. angle+=error
  1113. alt_angle = angle+(error*-2) # should be very near the center when flipped here
  1114. # we still need to bisect search because the relationship of pole_angle <==> error is somewhat unpredictable
  1115. upper_bounds = alt_angle if alt_angle > angle else angle
  1116. lower_bounds = alt_angle if alt_angle < angle else angle
  1117. i=0
  1118. while ( True ):
  1119. if (i>=max_iterations):
  1120. prOrange(f"IK Pole Angle Set reached max iterations of {i} in {time()-start_time} seconds")
  1121. break
  1122. if (abs(error)<error_threshhold) or (upper_bounds<=lower_bounds):
  1123. prPurple(f"IK Pole Angle Set converged after {i} iterations with error={error} in {time()-start_time} seconds")
  1124. break
  1125. # get the center-point betweeen the bounds
  1126. try_angle = lower_bounds + (upper_bounds-lower_bounds)/2
  1127. c.pole_angle = try_angle; dg.update()
  1128. error=signed_angle((base_ik_bone.tail-center_point), knee_direction, ik_axis)
  1129. if error>0: upper_bounds=try_angle
  1130. if error<0: lower_bounds=try_angle
  1131. i+=1
  1132. def bExecute(self, context):
  1133. prepare_parameters(self)
  1134. print(wrapGreen("Creating ")+wrapOrange("Inverse Kinematics")+
  1135. wrapGreen(" Constraint for bone: ") +
  1136. wrapOrange(self.GetxForm().bGetObject().name))
  1137. ik_bone = self.GetxForm().bGetObject()
  1138. c = self.GetxForm().bGetObject().constraints.new('IK')
  1139. get_target_and_subtarget(self, c)
  1140. get_target_and_subtarget(self, c, input_name = 'Pole Target')
  1141. if constraint_name := self.evaluate_input("Name"):
  1142. c.name = constraint_name
  1143. self.bObject = c
  1144. 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
  1145. if (c.pole_target): # Calculate the pole angle, the user shouldn't have to.
  1146. # my_xf = self.GetxForm()
  1147. # from .xForm_containers import xFormBone
  1148. # if not isinstance(my_xf, xFormBone):
  1149. # raise GraphError(f"ERROR: Pole Target must be ")
  1150. # if c.target !=
  1151. c.pole_angle = self.calc_pole_angle_pre(c, ik_bone)
  1152. props_sockets = {
  1153. 'chain_count' : ("Chain Length", 1),
  1154. 'use_tail' : ("Use Tail", True),
  1155. 'use_stretch' : ("Stretch", True),
  1156. "weight" : ("Position", 1.0),
  1157. "orient_weight" : ("Rotation", 0.0),
  1158. "influence" : ("Influence", 1.0),
  1159. 'mute' : ("Enable", True),
  1160. }
  1161. evaluate_sockets(self, c, props_sockets)
  1162. # TODO: handle drivers
  1163. # (it should be assumed we want it on if it's plugged
  1164. # into a driver).
  1165. c.use_location = self.evaluate_input("Position") > 0
  1166. c.use_rotation = self.evaluate_input("Rotation") > 0
  1167. self.executed = True
  1168. def bFinalize(self, bContext = None):
  1169. # adding a test here
  1170. if bContext:
  1171. ik_bone = self.GetxForm().bGetObject(mode='POSE')
  1172. if self.bObject.pole_target:
  1173. prWhite(f"Fine-tuning IK Pole Angle for {self}")
  1174. self.calc_pole_angle_post(self.bObject, ik_bone, bContext)
  1175. finish_drivers(self)
  1176. def ik_report_error(pb, context, do_print=False):
  1177. dg = context.view_layer.depsgraph
  1178. dg.update()
  1179. loc1, rot_quaternion1, scl1 = pb.matrix.decompose()
  1180. loc2, rot_quaternion2, scl2 = pb.bone.matrix_local.decompose()
  1181. location_error=(loc1-loc2).length
  1182. rotation_error = rot_quaternion1.rotation_difference(rot_quaternion2).angle
  1183. scale_error = (scl1-scl2).length
  1184. if location_error < FLOAT_EPSILON: location_error = 0
  1185. if abs(rotation_error) < FLOAT_EPSILON: rotation_error = 0
  1186. if scale_error < FLOAT_EPSILON: scale_error = 0
  1187. if do_print:
  1188. print (f"IK Location Error: {location_error}")
  1189. print (f"IK Rotation Error: {rotation_error}")
  1190. print (f"IK Scale Error : {scale_error}")
  1191. return (location_error, rotation_error, scale_error)
  1192. # This is kinda a weird design decision?
  1193. class LinkDrivenParameter(MantisLinkNode):
  1194. '''A node representing an armature object'''
  1195. def __init__(self, signature, base_tree):
  1196. self.base_tree=base_tree
  1197. inputs = [
  1198. "Input Relationship" ,
  1199. "Value" ,
  1200. "Parameter" ,
  1201. "Index" ,
  1202. ]
  1203. self.signature = signature
  1204. additional_parameters = { "Name":None }
  1205. self.inputs.init_sockets(inputs)
  1206. self.outputs.init_sockets(["Output Relationship"])
  1207. self.init_parameters(additional_parameters=additional_parameters)
  1208. self.set_traverse([("Input Relationship", "Output Relationship")])
  1209. def GetxForm(self):
  1210. return GetxForm(self)
  1211. def bExecute(self, bContext = None,):
  1212. prepare_parameters(self)
  1213. prGreen("Executing Driven Parameter node")
  1214. prop = self.evaluate_input("Parameter")
  1215. index = self.evaluate_input("Index")
  1216. value = self.evaluate_input("Value")
  1217. xf = self.GetxForm()
  1218. ob = xf.bGetObject(mode="POSE")
  1219. # IMPORTANT: this node only works on pose bone attributes.
  1220. self.bObject = ob
  1221. length=1
  1222. if hasattr(ob, prop):
  1223. try:
  1224. length = len(getattr(ob, prop))
  1225. except TypeError:
  1226. pass
  1227. except AttributeError:
  1228. pass
  1229. else:
  1230. raise AttributeError(f"Cannot Set value {prop} on object because it does not exist.")
  1231. def_value = 0.0
  1232. if length>1:
  1233. def_value=[0.0]*length
  1234. self.parameters["Value"] = tuple( 0.0 if i != index else value for i in range(length))
  1235. props_sockets = {
  1236. prop: ("Value", def_value)
  1237. }
  1238. evaluate_sockets(self, ob, props_sockets)
  1239. self.executed = True
  1240. def bFinalize(self, bContext = None):
  1241. driver = self.evaluate_input("Value")
  1242. try:
  1243. for i, val in enumerate(self.parameters["Value"]):
  1244. from .drivers import MantisDriver
  1245. if isinstance(val, MantisDriver):
  1246. driver["ind"] = i
  1247. val = driver
  1248. except AttributeError:
  1249. self.parameters["Value"] = driver
  1250. except TypeError:
  1251. self.parameters["Value"] = driver
  1252. finish_drivers(self)
  1253. class LinkArmature(MantisLinkNode):
  1254. '''A node representing an armature object'''
  1255. def __init__(self, signature, base_tree,):
  1256. super().__init__(signature, base_tree)
  1257. inputs = [
  1258. "Input Relationship" ,
  1259. "Preserve Volume" ,
  1260. "Use Envelopes" ,
  1261. "Use Current Location" ,
  1262. "Influence" ,
  1263. "Enable" ,
  1264. ]
  1265. additional_parameters = { "Name":None }
  1266. self.inputs.init_sockets(inputs)
  1267. self.outputs.init_sockets(["Output Relationship"])
  1268. self.init_parameters(additional_parameters=additional_parameters)
  1269. self.set_traverse([("Input Relationship", "Output Relationship")])
  1270. setup_custom_props(self)
  1271. def GetxForm(self):
  1272. return GetxForm(self)
  1273. def bExecute(self, bContext = None,):
  1274. prGreen("Creating Armature Constraint for bone: \""+ self.GetxForm().bGetObject().name + "\"")
  1275. prepare_parameters(self)
  1276. c = self.GetxForm().bGetObject().constraints.new('ARMATURE')
  1277. if constraint_name := self.evaluate_input("Name"):
  1278. c.name = constraint_name
  1279. self.bObject = c
  1280. # get number of targets
  1281. num_targets = len( list(self.inputs.values())[6:] )//2
  1282. props_sockets = {
  1283. 'use_deform_preserve_volume' : ("Preserve Volume", 0),
  1284. 'use_bone_envelopes' : ("Use Envelopes", 0),
  1285. 'use_current_location' : ("Use Current Location", 0),
  1286. 'influence' : ( "Influence" , 1),
  1287. 'mute' : ("Enable", True),
  1288. }
  1289. targets_weights = {}
  1290. for i in range(num_targets):
  1291. target = c.targets.new()
  1292. target_input_name = list(self.inputs.keys())[i*2+6 ]
  1293. weight_input_name = list(self.inputs.keys())[i*2+6+1]
  1294. get_target_and_subtarget(self, target, target_input_name)
  1295. weight_value=self.evaluate_input(weight_input_name)
  1296. if not isinstance(weight_value, float):
  1297. weight_value=0
  1298. targets_weights[i]=weight_value
  1299. props_sockets["targets[%d].weight" % i] = (weight_input_name, 0)
  1300. # targets_weights.append({"weight":(weight_input_name, 0)})
  1301. evaluate_sockets(self, c, props_sockets)
  1302. for target, value in targets_weights.items():
  1303. c.targets[target].weight=value
  1304. # for i, (target, weight) in enumerate(zip(c.targets, targets_weights)):
  1305. # evaluate_sockets(self, target, weight)
  1306. self.executed = True
  1307. def bFinalize(self, bContext = None):
  1308. finish_drivers(self)
  1309. class LinkSplineIK(MantisLinkNode):
  1310. '''A node representing an armature object'''
  1311. def __init__(self, signature, base_tree):
  1312. super().__init__(signature, base_tree)
  1313. inputs = [
  1314. "Input Relationship" ,
  1315. "Target" ,
  1316. "Chain Length" ,
  1317. "Even Divisions" ,
  1318. "Chain Offset" ,
  1319. "Use Curve Radius" ,
  1320. "Y Scale Mode" ,
  1321. "XZ Scale Mode" ,
  1322. "Use Original Scale" ,
  1323. "Influence" ,
  1324. ]
  1325. additional_parameters = { "Name":None }
  1326. self.inputs.init_sockets(inputs)
  1327. self.outputs.init_sockets(["Output Relationship"])
  1328. self.init_parameters(additional_parameters=additional_parameters)
  1329. self.set_traverse([("Input Relationship", "Output Relationship")])
  1330. def GetxForm(self):
  1331. return GetxForm(self)
  1332. def bExecute(self, bContext = None,):
  1333. prepare_parameters(self)
  1334. prGreen("Creating Spline-IK Constraint for bone: \""+ self.GetxForm().bGetObject().name + "\"")
  1335. c = self.GetxForm().bGetObject().constraints.new('SPLINE_IK')
  1336. get_target_and_subtarget(self, c)
  1337. if constraint_name := self.evaluate_input("Name"):
  1338. c.name = constraint_name
  1339. self.bObject = c
  1340. props_sockets = {
  1341. 'chain_count' : ("Chain Length", 0),
  1342. 'use_even_divisions' : ("Even Divisions", False),
  1343. 'use_chain_offset' : ("Chain Offset", False),
  1344. 'use_curve_radius' : ("Use Curve Radius", False),
  1345. 'y_scale_mode' : ("Y Scale Mode", "FIT_CURVE"),
  1346. 'xz_scale_mode' : ("XZ Scale Mode", "NONE"),
  1347. 'use_original_scale' : ("Use Original Scale", False),
  1348. 'influence' : ("Influence", 1),
  1349. }
  1350. evaluate_sockets(self, c, props_sockets)
  1351. self.executed = True