link_containers.py 62 KB

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