xForm_nodes.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. from .node_common import *
  2. from .base_definitions import MantisNode, NodeSocket
  3. from .xForm_socket_templates import *
  4. from .mantis_dataclasses import xForm_info
  5. def TellClasses():
  6. return [
  7. # xForm
  8. xFormArmature,
  9. xFormBone,
  10. xFormGeometryObject,
  11. xFormObjectInstance,
  12. xFormCurvePin,
  13. ]
  14. #*#-------------------------------#++#-------------------------------#*#
  15. # X - F O R M N O D E S
  16. #*#-------------------------------#++#-------------------------------#*#
  17. def reset_object_data(ob):
  18. # moving this to a common function so I can figure out the details later
  19. ob.id_properties_clear() # we must remove the custom properties so we can make them again.
  20. ob.constraints.clear()
  21. ob.animation_data_clear() # this is a little dangerous. TODO find a better solution since this can wipe animation the user wants to keep
  22. ob.modifiers.clear() # I would also like a way to copy modifiers and their settings, or bake them down. oh well
  23. def get_parent_node(mantis_node, type = 'XFORM'):
  24. # type variable for selecting whether to get either
  25. # the parent xForm or the inheritance node
  26. node_line, _last_socket = trace_single_line(mantis_node, "Relationship")
  27. for i in range(len(node_line)):
  28. # check each of the possible parent types.
  29. if ( (node_line[ i ].__class__.__name__ == 'LinkInherit') ):
  30. try: # it's the next one
  31. if (type == 'XFORM'):
  32. return node_line[ i + 1 ]
  33. else: # type = 'LINK'
  34. return node_line[ i ]
  35. except IndexError: # if there is no next one...
  36. return None # then there's no parent!
  37. return None
  38. def get_matrix(node):
  39. matrix = node.evaluate_input('Matrix')
  40. if matrix is None:
  41. node_line, socket = trace_single_line(node, "Matrix")
  42. raise RuntimeError(wrapRed(f"No matrix found for Armature {node}"))
  43. return matrix
  44. def set_object_parent(node):
  45. from bpy import data
  46. parent_xForm_info = get_parent_xForm_info(node)
  47. if parent_xForm_info.object_type == '':
  48. return # no parent
  49. elif parent_xForm_info.object_type in ['armature', 'object']:
  50. parent = data.objects.get(parent_xForm_info.parent_edit_name)
  51. if parent_xForm_info.parent_edit_name and parent is None:
  52. raise GraphError(f"Could not get parent object for node {node}.")
  53. node.bGetObject().parent = parent
  54. else: # it is a bone
  55. armOb = data.objects.get(parent_xForm_info.root_armature)
  56. node_ob = node.bGetObject()
  57. node_ob.parent = armOb; node_ob.parent_type = 'BONE'
  58. # this one expects a string.
  59. node_ob.parent_bone = parent_xForm_info.self_edit_name
  60. class xFormNode(MantisNode):
  61. def __init__(self, signature, base_tree, socket_templates=[]):
  62. super().__init__(signature, base_tree, socket_templates)
  63. self.node_type = 'XFORM'
  64. self.bObject=None
  65. # because new objects are created during prep phase
  66. def reset_execution(self):
  67. super().reset_execution()
  68. self.prepared=False
  69. def setup_custom_props(self, ob, input):
  70. from .mantis_dataclasses import custom_prop_template
  71. # detect custom inputs
  72. for i, l in enumerate(self.inputs[input].links):
  73. # we need to trace back all inputs
  74. custom_prop_data = self.evaluate_input(input, i)
  75. if not isinstance(custom_prop_data, custom_prop_template): continue
  76. from .drivers import MantisDriver
  77. name = custom_prop_data.name
  78. if name in ob.keys():
  79. raise RuntimeError("Error: cannot create a custom property with "
  80. "the same name as an existing property.")
  81. prop_type = custom_prop_data.prop_type
  82. match prop_type:
  83. case 'BOOL':
  84. value = custom_prop_data.default_value_bool
  85. case 'INT':
  86. value = custom_prop_data.default_value_int
  87. case 'FLOAT':
  88. value = custom_prop_data.default_value_float
  89. case 'VECTOR':
  90. value = custom_prop_data.default_value_vector
  91. case 'STRING':
  92. value = custom_prop_data.default_value_string
  93. if (value is None):
  94. raise RuntimeError("Could not set value of custom parameter")
  95. # it creates a more confusing error later sometimes, better to catch it here.
  96. ob[name] = value
  97. ui_data = ob.id_properties_ui(name)
  98. ui_data.update(
  99. default=value,
  100. description=custom_prop_data.description)
  101. #if a number, set the min/max values. it may overflow on the C side
  102. if type(value) in [float, int]:
  103. for prop_name in ['min', 'max', 'soft_min', 'soft_max', ]:
  104. prop_value = getattr(custom_prop_data, prop_name)
  105. if type(value) == int: prop_value = int(prop_value)
  106. # DO: figure out the right way to prevent an oveflow
  107. try: # we have to do this as a keyword argument like this
  108. ui_data.update( **{prop_name:prop_value} )
  109. except OverflowError: #this occurs when the value is inf
  110. prRed(f"invalid value {prop_value} for custom prop {prop_name}"
  111. f" of type {type(value)} in {self}. It will remain unset.")
  112. ob.property_overridable_library_set(f"[\"{name}\"]", True)
  113. pass
  114. class xFormArmature(xFormNode):
  115. '''A node representing an armature object'''
  116. def __init__(self, signature, base_tree):
  117. super().__init__(signature, base_tree, xFormArmatureSockets)
  118. self.init_parameters()
  119. self.set_traverse([("Relationship", "xForm Out")])
  120. setup_custom_property_inputs_outputs(self)
  121. def bPrepare(self, bContext=None):
  122. self.parameters['Matrix'] = get_matrix(self)
  123. self.prepared = True
  124. def bTransformPass(self, bContext = None,):
  125. # from .utilities import get_ui_node
  126. import bpy
  127. if (not isinstance(bContext, bpy.types.Context)):
  128. raise RuntimeError("Incorrect context")
  129. name = self.evaluate_input("Name")
  130. matrix = self.parameters['Matrix']
  131. reset_transforms = False
  132. #check if an object by the name exists
  133. if (name) and (ob := bpy.data.objects.get(name)):
  134. if (ob.animation_data):
  135. while (ob.animation_data.drivers):
  136. ob.animation_data.drivers.remove(ob.animation_data.drivers[-1])
  137. for pb in ob.pose.bones:
  138. # clear it, even after deleting the edit bones,
  139. # if we create them again the pose bones will be reused
  140. while (pb.constraints):
  141. pb.constraints.remove(pb.constraints[-1])
  142. if reset_transforms:
  143. pb.location = (0,0,0)
  144. pb.rotation_euler = (0,0,0)
  145. pb.rotation_quaternion = (1.0,0,0,0)
  146. pb.rotation_axis_angle = (0,0,1.0,0)
  147. pb.scale = (1.0,1.0,1.0)
  148. # feels ugly and bad, whatever
  149. collections = []
  150. for bc in ob.data.collections:
  151. collections.append(bc)
  152. for bc in collections:
  153. ob.data.collections.remove(bc)
  154. del collections
  155. # end ugly/bad
  156. else:
  157. # Create the Object
  158. ob = bpy.data.objects.new(name, bpy.data.armatures.new(name)) #create ob
  159. if (ob.name != name):
  160. raise RuntimeError("Could not create xForm object", name)
  161. # it seems this data sticks around even if the object was deleted, so clear it here.
  162. ob.id_properties_clear()
  163. self.bObject = ob.name
  164. ob.matrix_world = matrix.copy()
  165. ob.data.pose_position = 'REST'
  166. set_object_parent(self)
  167. # Link to Scene:
  168. if (ob.name not in bContext.view_layer.active_layer_collection.collection.objects):
  169. bContext.view_layer.active_layer_collection.collection.objects.link(ob)
  170. print( wrapGreen("Created Armature object: ")+ wrapWhite(ob.name))
  171. # Finalize the action
  172. # oddly, overriding context doesn't seem to work
  173. try:
  174. bpy.ops.object.select_all(action='DESELECT')
  175. except RuntimeError:
  176. pass # we're already in edit mode, should be OK to do this.
  177. bContext.view_layer.objects.active = ob
  178. selected=[]
  179. for other_ob in bpy.data.objects:
  180. if other_ob.mode == "EDIT":
  181. selected.append(other_ob)
  182. selected.append(ob)
  183. context_override = {"active_object":ob, "selected_objects":selected}
  184. print("Changing Armature Mode to " +wrapPurple("EDIT"))
  185. with bContext.temp_override(**context_override):
  186. bpy.ops.object.mode_set(mode='EDIT')
  187. if ob.mode != "EDIT":
  188. prRed("eh?")
  189. # clear it
  190. while (len(ob.data.edit_bones) > 0):
  191. ob.data.edit_bones.remove(ob.data.edit_bones[0])
  192. # bContext.view_layer.objects.active = prevAct
  193. # This will OVERWRITE the root armature since bones have to trace back to this one.
  194. root_armature = ob.name
  195. parent_xForm_info = get_parent_xForm_info(self)
  196. my_info = xForm_info(
  197. object_type='armature',
  198. root_armature= ob.name,
  199. parent_pose_name=parent_xForm_info.self_pose_name,
  200. parent_edit_name=parent_xForm_info.self_edit_name,
  201. self_pose_name=ob.name,
  202. self_edit_name=ob.name,
  203. )
  204. self.parameters['xForm Out'] = my_info
  205. self.executed = True
  206. def bFinalize(self, bContext = None):
  207. # custom properties
  208. self.setup_custom_props(self.bGetObject(), "Custom Properties")
  209. finish_drivers(self)
  210. def bGetObject(self, mode = ''):
  211. import bpy; return bpy.data.objects[self.parameters['xForm Out'].self_pose_name]
  212. bone_inputs= [
  213. "Name",
  214. "Rotation Order",
  215. "Matrix",
  216. "Relationship",
  217. # IK settings
  218. "IK Stretch",
  219. "Lock IK",
  220. "IK Stiffness",
  221. "Limit IK",
  222. "X Min",
  223. "X Max",
  224. "Y Min",
  225. "Y Max",
  226. "Z Min",
  227. "Z Max",
  228. # Visual stuff
  229. "Bone Collection",
  230. "Hide",
  231. "Custom Object",
  232. "Custom Object xForm Override",
  233. "Custom Object Scale to Bone Length",
  234. "Custom Object Wireframe",
  235. "Custom Object Scale",
  236. "Custom Object Translation",
  237. "Custom Object Rotation",
  238. "Color",
  239. "Inherit Color",
  240. # Deform Stuff
  241. "Deform",
  242. "Envelope Distance",
  243. "Envelope Weight",
  244. "Envelope Multiply",
  245. "Envelope Head Radius",
  246. "Envelope Tail Radius",
  247. # BBone stuff:
  248. "BBone Segments",
  249. "BBone X Size",
  250. "BBone Z Size",
  251. "BBone HQ Deformation",
  252. "BBone X Curve-In",
  253. "BBone Z Curve-In",
  254. "BBone X Curve-Out",
  255. "BBone Z Curve-Out",
  256. "BBone Roll-In",
  257. "BBone Roll-Out",
  258. "BBone Inherit End Roll",
  259. "BBone Scale-In",
  260. "BBone Scale-Out",
  261. "BBone Ease-In",
  262. "BBone Ease-Out",
  263. "BBone Easing",
  264. "BBone Start Handle Type",
  265. "BBone Custom Start Handle",
  266. "BBone Start Handle Scale",
  267. "BBone Start Handle Ease",
  268. "BBone End Handle Type",
  269. "BBone Custom End Handle",
  270. "BBone End Handle Scale",
  271. "BBone End Handle Ease",
  272. # locks
  273. "Lock Location",
  274. "Lock Rotation",
  275. "Lock Scale",
  276. "Custom Properties",
  277. ]
  278. class xFormBone(xFormNode):
  279. '''A node representing a bone in an armature'''
  280. # DO: make a way to identify which armature this belongs to
  281. def __init__(self, signature, base_tree):
  282. super().__init__(signature, base_tree)
  283. outputs = [
  284. "xForm Out",
  285. ]
  286. self.inputs.init_sockets(bone_inputs)
  287. self.outputs.init_sockets(outputs)
  288. self.socket_templates=xFormBoneSockets
  289. # TODO: implement socket templates completely for Bone
  290. # currently it is waiting on BBone and refactoring/cleanup.
  291. self.init_parameters()
  292. self.set_traverse([("Relationship", "xForm Out")])
  293. setup_custom_property_inputs_outputs(self)
  294. def bGetParentArmature(self):
  295. parent_xForm_info = get_parent_xForm_info(self)
  296. from bpy import data
  297. return data.objects.get(parent_xForm_info.root_armature)
  298. def bSetParent(self, eb):
  299. from bpy import data
  300. parent_xForm_info = get_parent_xForm_info(self)
  301. parent_armature = data.objects.get( parent_xForm_info.root_armature)
  302. if parent_xForm_info.self_edit_name != parent_xForm_info.root_armature:
  303. parent_bone = parent_armature.data.edit_bones.get( parent_xForm_info.self_edit_name)
  304. eb.parent = parent_bone
  305. parent_mantis_node = get_parent_node(self, type = 'LINK') # get the link node.
  306. # TODO probably need to send the parenting info or at least the signatures of intervening nodes.
  307. eb.use_connect = parent_mantis_node.evaluate_input("Connected")
  308. eb.use_inherit_rotation = parent_mantis_node.evaluate_input("Inherit Rotation")
  309. eb.inherit_scale = parent_mantis_node.evaluate_input("Inherit Scale")
  310. def bPrepare(self, bContext=None):
  311. self.parameters['Matrix'] = get_matrix(self)
  312. self.prepared = True
  313. def bTransformPass(self, bContext = None,): #possibly will need to pass context?
  314. import bpy
  315. from mathutils import Vector
  316. if not (name := self.evaluate_input("Name")):
  317. raise RuntimeError(wrapRed(f"Could not set name for bone in {self}"))
  318. if (not isinstance(bContext, bpy.types.Context)):
  319. raise RuntimeError("Incorrect context")
  320. if not (xF := self.bGetParentArmature()):
  321. raise RuntimeError("Could not create edit bone: ", name, " from node:", self, " Reason: No armature object to add bone to.")
  322. matrix = self.parameters['Matrix']
  323. length = matrix[3][3]
  324. if (xF):
  325. if (xF.mode != "EDIT"):
  326. raise RuntimeError("Armature Object Not in Edit Mode, exiting...")
  327. #
  328. # Create the Object
  329. d = xF.data
  330. eb = d.edit_bones.new(name)
  331. # Bone Collections:
  332. # We treat each separate string as a Bone Collection that this object belongs to
  333. # Bone Collections are fully qualified by their hierarchy.
  334. # Separate Strings with "|" and indicate hierarchy with ">". These are special characters.
  335. # NOTE: if the user names the collections differently at different times, this will take the FIRST definition and go with it
  336. if self.inputs['Bone Collection'].links:
  337. bCol_groups = []
  338. for i, l in enumerate(self.inputs['Bone Collection'].links):
  339. bCol_group = self.evaluate_input("Bone Collection", index=i)
  340. bCol_groups.append(bCol_group)
  341. bCols = '|'.join(bCol_groups)
  342. else:
  343. bCols = self.evaluate_input("Bone Collection")
  344. if bCols: # it is actually possible to add a bone to no collections. odd.
  345. bone_collections = bCols.split("|")
  346. for collection_list in bone_collections:
  347. hierarchy = collection_list.split(">")
  348. col_parent = None
  349. for bCol in hierarchy:
  350. if bCol is None: continue # I think this
  351. if bCol == '': continue
  352. if ( col := d.collections_all.get(bCol) ) is None:
  353. col = d.collections.new(bCol)
  354. col.parent = col_parent
  355. col_parent = col
  356. if hierarchy[-1]: # will be "" or None if the box is empty
  357. d.collections_all.get(hierarchy[-1]).assign(eb)
  358. if (eb.name != name):
  359. prRed(f"Expected bone of name: {name}, got {eb.name} instead.")
  360. raise RuntimeError("Could not create bone ", name, "; Perhaps there is a duplicate bone name in the node tree?")
  361. eb.matrix = matrix.copy()
  362. tailoffset = Vector((0,length,0)) #Vector((0,self.tailoffset, 0))
  363. tailoffset = matrix.copy().to_3x3() @ tailoffset
  364. eb.tail = eb.head + tailoffset
  365. if (eb.name != name):
  366. raise RuntimeError("Could not create edit bone: ", name)
  367. assert (eb.name), "Bone must have a name."
  368. self.bObject = eb.name
  369. # The bone should have relationships going in at this point.
  370. self.bSetParent(eb)
  371. if eb.head == eb.tail:
  372. raise RuntimeError(wrapRed(f"Could not create edit bone: {name} because bone head was located in the same place as bone tail."))
  373. # Setup Deform attributes...
  374. eb.use_deform = self.evaluate_input("Deform")
  375. eb.envelope_distance = self.evaluate_input("Envelope Distance")
  376. eb.envelope_weight = self.evaluate_input("Envelope Weight")
  377. eb.use_envelope_multiply = self.evaluate_input("Envelope Multiply")
  378. eb.head_radius = self.evaluate_input("Envelope Head Radius")
  379. eb.tail_radius = self.evaluate_input("Envelope Tail Radius")
  380. print( wrapGreen("Created Bone: ") + wrapOrange(eb.name) + wrapGreen(" in ") + wrapWhite(self.bGetParentArmature().name))
  381. parent_xForm_info = get_parent_xForm_info(self)
  382. my_info = xForm_info(
  383. object_type='bone',
  384. root_armature= xF.name,
  385. parent_pose_name=parent_xForm_info.self_pose_name,
  386. parent_edit_name=parent_xForm_info.self_edit_name,
  387. self_pose_name=eb.name,
  388. self_edit_name=eb.name,
  389. )
  390. self.parameters['xForm Out'] = my_info
  391. self.executed = True
  392. def set_bone_color(self, b, inherit_color, bContext):
  393. color_values = self.evaluate_input('Color')
  394. if color_values is None:
  395. prOrange(f"Warning: No color information found for {b.name}. This should not happen.")
  396. return
  397. if inherit_color and b.parent:
  398. b.color.palette=b.parent.color.palette
  399. if b.color.palette == 'CUSTOM':
  400. b.color.custom.active=b.parent.color.custom.active
  401. b.color.custom.normal=b.parent.color.custom.normal
  402. b.color.custom.select=b.parent.color.custom.select
  403. return
  404. from mathutils import Color
  405. color_active = Color(color_values[:3])
  406. color_normal = Color(color_values[3:6])
  407. color_select = Color(color_values[6:])
  408. is_theme_colors = False
  409. theme = bContext.preferences.themes[0]
  410. for i, color_set in enumerate(theme.bone_color_sets):
  411. if ((color_active == color_set.active) and
  412. (color_normal == color_set.normal) and
  413. (color_select == color_set.select) ):
  414. is_theme_colors=True; break
  415. if is_theme_colors: # add 1, not 0-indexed
  416. b.color.palette = 'THEME'+str(i+1).zfill(2)
  417. elif ((color_active == theme.view_3d.bone_pose_active) and
  418. (color_normal == theme.view_3d.bone_solid) and
  419. (color_select == theme.view_3d.bone_pose) ):
  420. b.color.palette = 'DEFAULT'
  421. else:
  422. b.color.palette = 'CUSTOM'
  423. b.color.custom.active=color_active
  424. b.color.custom.normal=color_normal
  425. b.color.custom.select=color_select
  426. def bFinalize(self, bContext = None):
  427. b = self.bGetParentArmature().data.bones[self.bObject]
  428. # let's do bone colors first
  429. inherit_color = self.evaluate_input("Inherit Color")
  430. if len(self.inputs['Color'].links) > 0:
  431. inherit_color = False # use the link instead
  432. # try: # just in case, this shouldn't cause a failure
  433. self.set_bone_color(b, inherit_color, bContext)
  434. # except Exception as e:
  435. # prRed("WARNING: failed to set color because of error, see report below:")
  436. # prOrange(e)
  437. #
  438. do_bb=False
  439. b.bbone_x = self.evaluate_input("BBone X Size"); b.bbone_x = max(b.bbone_x, 0.0002)
  440. b.bbone_z = self.evaluate_input("BBone Z Size"); b.bbone_z = max(b.bbone_z, 0.0002)
  441. if (segs := self.evaluate_input("BBone Segments")) > 1:
  442. do_bb=True
  443. b.bbone_segments = segs
  444. b.bbone_x = self.evaluate_input("BBone X Size")
  445. b.bbone_z = self.evaluate_input("BBone Z Size")
  446. if self.evaluate_input("BBone HQ Deformation"):
  447. b.bbone_mapping_mode = "CURVED"
  448. # 'bbone_handle_type_start' : ("BBone Start Handle Type", "AUTO"),
  449. # 'bbone_handle_type_end' : ("BBone End Handle Type", "AUTO"),
  450. # 'bbone_custom_handle_start' : ("BBone Custom Start Handle", "AUTO"),
  451. # 'bbone_custom_handle_end' : ("BBone Custom End Handle", "AUTO"),
  452. if handle_type := self.evaluate_input("BBone Start Handle Type"):
  453. b.bbone_handle_type_start = handle_type
  454. if handle_type := self.evaluate_input("BBone End Handle Type"):
  455. b.bbone_handle_type_end = handle_type
  456. try:
  457. if (custom_handle := self.evaluate_input("BBone Custom Start Handle")):
  458. b.bbone_custom_handle_start = self.bGetParentArmature().data.bones[custom_handle]
  459. if (custom_handle := self.evaluate_input("BBone Custom End Handle")):
  460. b.bbone_custom_handle_end = self.bGetParentArmature().data.bones[custom_handle]
  461. except KeyError:
  462. prRed("Warning: BBone start or end handle not set because of missing bone in armature.")
  463. bone_props_socket= {
  464. 'bbone_curveinx' : ("BBone X Curve-In", 0.0),
  465. 'bbone_curveinz' : ("BBone Z Curve-In", 0.0),
  466. 'bbone_curveoutx' : ("BBone X Curve-Out", 0.0),
  467. 'bbone_curveoutz' : ("BBone Z Curve-Out", 0.0),
  468. }
  469. evaluate_sockets(self, b, bone_props_socket)
  470. # TODO this section should be done with props-socket thing
  471. b.bbone_handle_use_scale_start = self.evaluate_input("BBone Start Handle Scale")
  472. b.bbone_handle_use_scale_end = self.evaluate_input("BBone End Handle Scale")
  473. import bpy
  474. pb = self.bGetParentArmature().pose.bones[self.bObject]
  475. rotation_mode = self.evaluate_input("Rotation Order")
  476. if rotation_mode == "AUTO": rotation_mode = "XYZ"
  477. pb.rotation_mode = rotation_mode
  478. pb.id_properties_clear()
  479. # these are kept around unless explicitly deleted.
  480. # from .utilities import get_ui_node
  481. # np = get_ui_node(self.signature, self.base_tree)
  482. driver = None
  483. do_prints=False
  484. # custom properties
  485. self.setup_custom_props(pb, "Custom Properties")
  486. if (pb.is_in_ik_chain):
  487. # this props_socket thing wasn't really meant to work here but it does, neat
  488. props_sockets = {
  489. 'ik_stretch' : ("IK Stretch", 0),
  490. 'lock_ik_x' : (("Lock IK", 0), False),
  491. 'lock_ik_y' : (("Lock IK", 1), False),
  492. 'lock_ik_z' : (("Lock IK", 2), False),
  493. 'ik_stiffness_x' : (("IK Stiffness", 0), 0.0),
  494. 'ik_stiffness_y' : (("IK Stiffness", 1), 0.0),
  495. 'ik_stiffness_z' : (("IK Stiffness", 2), 0.0),
  496. 'use_ik_limit_x' : (("Limit IK", 0), False),
  497. 'use_ik_limit_y' : (("Limit IK", 1), False),
  498. 'use_ik_limit_z' : (("Limit IK", 2), False),
  499. 'ik_min_x' : ("X Min", 0),
  500. 'ik_max_x' : ("X Max", 0),
  501. 'ik_min_y' : ("Y Min", 0),
  502. 'ik_max_y' : ("Y Max", 0),
  503. 'ik_min_z' : ("Z Min", 0),
  504. 'ik_max_z' : ("Z Max", 0),
  505. }
  506. evaluate_sockets(self, pb, props_sockets)
  507. if do_bb:
  508. props_sockets = {
  509. 'bbone_curveinx' : ("BBone X Curve-In", pb.bone.bbone_curveinx),
  510. 'bbone_curveinz' : ("BBone Z Curve-In", pb.bone.bbone_curveinz),
  511. 'bbone_curveoutx' : ("BBone X Curve-Out", pb.bone.bbone_curveoutx),
  512. 'bbone_curveoutz' : ("BBone Z Curve-Out", pb.bone.bbone_curveoutz),
  513. 'bbone_easein' : ("BBone Ease-In", 0),
  514. 'bbone_easeout' : ("BBone Ease-Out", 0),
  515. 'bbone_rollin' : ("BBone Roll-In", 0),
  516. 'bbone_rollout' : ("BBone Roll-Out", 0),
  517. 'bbone_scalein' : ("BBone Scale-In", (1,1,1)),
  518. 'bbone_scaleout' : ("BBone Scale-Out", (1,1,1)),
  519. }
  520. evaluate_sockets(self, pb, props_sockets)
  521. # we need to clear this stuff since our only real goal was to get some drivers from the above
  522. for attr_name in props_sockets.keys():
  523. try:
  524. setattr(pb, attr_name, 0) # just clear it
  525. except ValueError:
  526. setattr(pb, attr_name, (1.0,1.0,1.0)) # scale needs to be set to 1
  527. # important TODO... all of the drivers and stuff should be handled this way, right?
  528. # time to set up drivers!
  529. # just gonna add this to the end and build off it I guess
  530. props_sockets = {
  531. "lock_location" : ("Lock Location", [False, False, False]),
  532. "lock_rotation" : ("Lock Rotation", [False, False, False]),
  533. "lock_scale" : ("Lock Scale", [False, False, False]),
  534. 'custom_shape_scale_xyz' : ("Custom Object Scale", (0.0,0.0,0.0) ),
  535. 'custom_shape_translation' : ("Custom Object Translation", (0.0,0.0,0.0) ),
  536. 'custom_shape_rotation_euler' : ("Custom Object Rotation", (0.0,0.0,0.0) ),
  537. 'use_custom_shape_bone_size' : ("Custom Object Scale to Bone Length", True,)
  538. }
  539. evaluate_sockets(self, pb, props_sockets)
  540. # this could probably be moved to bTransformPass
  541. props_sockets = {
  542. 'hide' : ("Hide", False),
  543. 'show_wire' : ("Custom Object Wireframe", False),
  544. }
  545. evaluate_sockets(self, pb.bone, props_sockets)
  546. if (driver):
  547. pass
  548. # whatever I was doing there.... was stupid. CLEAN UP TODO
  549. # this is the right thing to do.
  550. finish_drivers(self)
  551. #
  552. # OK, visual settings
  553. #
  554. # Get the override xform's bone:
  555. pb.custom_shape_transform = None
  556. pb.custom_shape = None
  557. if len(self.inputs["Custom Object xForm Override"].links) > 0:
  558. trace = trace_single_line(self, "Custom Object xForm Override")
  559. try:
  560. pb.custom_shape_transform = trace[0][1].bGetObject()
  561. except AttributeError:
  562. pass
  563. if len(self.inputs["Custom Object"].links) > 0:
  564. trace = trace_single_line(self, "Custom Object")
  565. try:
  566. ob = trace[0][1].bGetObject()
  567. except AttributeError:
  568. ob=None
  569. if type(ob) in [bpy.types.Object]:
  570. pb.custom_shape = ob
  571. def bGetObject(self, mode = 'POSE'):
  572. if self.bObject is None: return None
  573. if mode in ["POSE", "OBJECT"] and self.bGetParentArmature().mode == "EDIT":
  574. raise RuntimeError("Cannot get Bone or PoseBone in Edit mode.")
  575. elif mode == "EDIT" and self.bGetParentArmature().mode != "EDIT":
  576. raise RuntimeError("Cannot get EditBone except in Edit mode.")
  577. try:
  578. if (mode == 'EDIT'):
  579. return self.bGetParentArmature().data.edit_bones[self.bObject]
  580. elif (mode == 'OBJECT'):
  581. return self.bGetParentArmature().data.bones[self.bObject]
  582. elif (mode == 'POSE'):
  583. return self.bGetParentArmature().pose.bones[self.bObject]
  584. except Exception as e:
  585. prRed ("Cannot get bone for %s" % self)
  586. raise e
  587. class xFormGeometryObject(xFormNode):
  588. '''A node representing an armature object'''
  589. def __init__(self, signature, base_tree):
  590. super().__init__(signature, base_tree, xFormGeometryObjectSockets)
  591. self.init_parameters()
  592. self.set_traverse([("Relationship", "xForm Out")])
  593. self.has_shape_keys = False
  594. setup_custom_property_inputs_outputs(self)
  595. def bPrepare(self, bContext = None,):
  596. import bpy
  597. if not self.evaluate_input("Name"):
  598. self.prepared = True
  599. self.executed = True
  600. # and return an error if there are any dependencies:
  601. if self.hierarchy_connections:
  602. raise GraphError(wrapRed(f"Cannot Generate object {self} because the chosen name is empty or invalid."))
  603. return
  604. self.bObject = bpy.data.objects.get(self.evaluate_input("Name"))
  605. trace = trace_single_line(self, "Geometry")
  606. if (not self.bObject):
  607. if trace[-1]:
  608. self.bObject = bpy.data.objects.new(self.evaluate_input("Name"), trace[-1].node.bGetObject())
  609. # handle mismatched data.
  610. data_wrong = False; data = None
  611. if (self.inputs["Geometry"].is_linked and self.bObject.type == "EMPTY"):
  612. data_wrong = True; data = trace[-1].node.bGetObject()
  613. elif (not self.inputs["Geometry"].is_linked and not self.bObject.type == "EMPTY"):
  614. data_wrong = True
  615. # clumsy but functional
  616. if data_wrong:
  617. unlink_me = self.bObject
  618. unlink_me.name = "MANTIS_TRASH.000"
  619. for col in unlink_me.users_collection:
  620. col.objects.unlink(unlink_me)
  621. self.bObject = bpy.data.objects.new(self.evaluate_input("Name"), data)
  622. if self.bObject and (self.inputs["Geometry"].is_linked and self.bObject.type in ["MESH", "CURVE"]):
  623. self.bObject.data = trace[-1].node.bGetObject()
  624. # NOW: find out if we need to duplicate the object data.
  625. dupe_data=False
  626. node_line = trace_single_line(self, "Deformer")[0]
  627. from .deformer_nodes import DeformerHook
  628. for deformer in node_line:
  629. if isinstance(deformer, DeformerHook) and \
  630. deformer.evaluate_input("Affect Curve Radius") == True and \
  631. self.bObject.type == 'CURVE':
  632. print(f"INFO: Duplicating data {self.bObject.data.name} in {self} so it can be used for drivers.")
  633. dupe_data=True; break
  634. if dupe_data:
  635. name = self.bObject.data.name
  636. # it has to be a curve
  637. data = bpy.data.curves.get(self.bObject.data.name+"_MANTIS")
  638. if data: # Delete it and regenerate it if it exists.
  639. data.name+="_TRASH.000" # but we can't actually delete it here
  640. # since previous executions of the graph may be using it.
  641. # instead, we'll rename it and use a new copy. This will probably
  642. # be deleted on its own by Blender's garbage collector.
  643. # if it isn't, then it is still in use and I may NOT touch it.
  644. data=self.bObject.data.copy(); data.animation_data_clear()
  645. data.name = name+"_MANTIS"
  646. self.bObject.data = data
  647. reset_object_data(self.bObject)
  648. matrix= get_matrix(self)
  649. self.parameters['Matrix'] = matrix
  650. try:
  651. set_object_parent(self)
  652. except: # I guess it isn't ready yet. we'll do it later
  653. pass # (This can happen when solving schema.)
  654. self.bObject.matrix_world = matrix
  655. parent_xForm_info = get_parent_xForm_info(self)
  656. root_armature = parent_xForm_info.root_armature
  657. my_info = xForm_info(
  658. object_type='object',
  659. root_armature= root_armature,
  660. parent_pose_name=parent_xForm_info.self_edit_name,
  661. parent_edit_name=parent_xForm_info.self_pose_name,
  662. self_pose_name=self.bObject.name,
  663. self_edit_name=self.bObject.name,
  664. )
  665. self.parameters['xForm Out'] = my_info
  666. self.prepared = True
  667. def bTransformPass(self, bContext = None,):
  668. try:
  669. bContext.collection.objects.link(self.bObject)
  670. except RuntimeError: #already in; but a dangerous thing to pass.
  671. pass
  672. self.has_shape_keys = False
  673. # putting this in bTransformPass simply prevents it from being run more than once.
  674. # maybe I should do that with the rest of bPrepare, too.
  675. props_sockets = {
  676. 'hide_viewport' : ("Hide in Viewport", False),
  677. 'hide_render' : ("Hide in Render", False),
  678. }
  679. evaluate_sockets(self, self.bObject, props_sockets)
  680. self.executed = True
  681. def bFinalize(self, bContext = None):
  682. matrix= get_matrix(self)
  683. set_object_parent(self)
  684. self.bObject.matrix_world = matrix
  685. # custom properties
  686. self.setup_custom_props(self.bGetObject(), "Custom Properties")
  687. for i, (driver_key, driver_item) in enumerate(self.drivers.items()):
  688. print (wrapGreen(i), wrapWhite(self), wrapPurple(driver_key))
  689. prOrange(driver_item)
  690. finish_drivers(self)
  691. def bGetObject(self, mode = 'POSE'):
  692. return self.bObject
  693. class xFormObjectInstance(xFormNode):
  694. """Represents an instance of an existing geometry object."""
  695. def __init__(self, signature, base_tree):
  696. super().__init__(signature, base_tree, xFormGeometryObjectInstanceSockets)
  697. self.init_parameters()
  698. # TODO: I think this field is a leftover from a test or something. see if it can be removed.
  699. self.links = {} # leave this empty for now!
  700. self.set_traverse([("Relationship", "xForm Out")])
  701. self.has_shape_keys = False # Shape Keys will make a dupe so this is OK
  702. setup_custom_property_inputs_outputs(self)
  703. def ui_modify_socket(self, ui_socket, socket_name=None):
  704. if ui_socket.name == 'As Instance':
  705. change_handled = True
  706. try:
  707. self.bObject.modifiers[0]['Socket_1'] = ui_socket.default_value
  708. except Exception as e:
  709. print("Failed to update mantis socket because of %s" % e,
  710. "Updating tree instead.")
  711. return change_handled
  712. else:
  713. return super().ui_modify_socket(ui_socket, socket_name)
  714. def bPrepare(self, bContext = None,):
  715. from bpy import data
  716. empty_mesh = data.meshes.get("MANTIS_EMPTY_MESH")
  717. if not empty_mesh:
  718. empty_mesh = data.meshes.new("MANTIS_EMPTY_MESH")
  719. if not self.evaluate_input("Name"):
  720. self.prepared = True
  721. self.executed = True
  722. # and return an error if there are any dependencies:
  723. if self.hierarchy_connections:
  724. raise GraphError(wrapRed(f"Cannot Generate object {self} because the chosen name is empty or invalid."))
  725. return
  726. self.bObject = data.objects.get(self.evaluate_input("Name"))
  727. if (not self.bObject):
  728. self.bObject = data.objects.new(self.evaluate_input("Name"), empty_mesh)
  729. reset_object_data(self.bObject)
  730. matrix= get_matrix(self)
  731. self.parameters['Matrix'] = matrix
  732. set_object_parent(self)
  733. self.bObject.matrix_world = matrix
  734. parent_xForm_info = get_parent_xForm_info(self)
  735. root_armature = parent_xForm_info.root_armature
  736. my_info = xForm_info(
  737. object_type='object',
  738. root_armature= root_armature,
  739. parent_pose_name=parent_xForm_info.self_edit_name,
  740. parent_edit_name=parent_xForm_info.self_pose_name,
  741. self_pose_name=self.bObject.name,
  742. self_edit_name=self.bObject.name,
  743. )
  744. self.parameters['xForm Out'] = my_info
  745. self.prepared = True
  746. def bTransformPass(self, bContext = None,):
  747. try:
  748. bContext.collection.objects.link(self.bObject)
  749. except RuntimeError: #already in; but a dangerous thing to pass.
  750. pass
  751. self.has_shape_keys = False
  752. # putting this in bTransformPass simply prevents it from being run more than once.
  753. # maybe I should do that with the rest of bPrepare, too.
  754. props_sockets = {
  755. 'hide_viewport' : ("Hide in Viewport", False),
  756. 'hide_render' : ("Hide in Render", False),
  757. }
  758. evaluate_sockets(self, self.bObject, props_sockets)
  759. self.executed = True
  760. def bFinalize(self, bContext = None):
  761. # now we need to set the object instance up.
  762. from bpy import data
  763. trace = trace_single_line(self, "Source Object")
  764. for node in trace[0]:
  765. if node is self: continue # lol
  766. if (node.node_type == 'XFORM'):
  767. source_ob = node.bGetObject(); break
  768. modifier = self.bObject.modifiers.new("Object Instance", type='NODES')
  769. ng = data.node_groups.get("Object Instance")
  770. if ng is None:
  771. from .geometry_node_graphgen import gen_object_instance_node_group
  772. ng = gen_object_instance_node_group()
  773. modifier.node_group = ng
  774. modifier["Socket_0"] = source_ob
  775. modifier["Socket_1"] = self.evaluate_input("As Instance")
  776. # custom properties
  777. self.setup_custom_props(self.bGetObject(), "Custom Properties")
  778. for i, (driver_key, driver_item) in enumerate(self.drivers.items()):
  779. print (wrapGreen(i), wrapWhite(self), wrapPurple(driver_key))
  780. prOrange(driver_item)
  781. finish_drivers(self)
  782. def bGetObject(self, mode = 'POSE'):
  783. return self.bObject
  784. class xFormCurvePin(xFormNode):
  785. """An xForm pinned to a specific location on a curve."""
  786. def __init__(self, signature, base_tree):
  787. super().__init__(signature, base_tree,xFormCurvePinSockets)
  788. self.init_parameters(additional_parameters={"Matrix":None})
  789. setup_custom_property_inputs_outputs(self)
  790. def prep_driver_values(self, constraint):
  791. from .misc_nodes import UtilityDriver, UtilitySwitch
  792. for socket_name in ["Curve Pin Factor", "Forward Axis","Up Axis",]:
  793. if self.inputs.get(socket_name) is None: continue # in case it has been bypassed
  794. if self.inputs[socket_name].is_linked:
  795. node_line, _last_socket = trace_single_line(self, socket_name)
  796. driver = None
  797. for other_node in node_line:
  798. if other_node.node_type == 'DRIVER':
  799. driver = other_node; break
  800. if isinstance(driver, UtilityDriver):
  801. prop_amount = driver.evaluate_input("Property")
  802. elif isinstance(driver, UtilitySwitch):
  803. xf=driver.GetxForm()
  804. prop_amount = xf.evaluate_input(driver.evaluate_input('Parameter'))
  805. else:
  806. return
  807. for template in self.socket_templates:
  808. if template.name == socket_name: break
  809. setattr(constraint, template.blender_property, prop_amount )
  810. def bPrepare(self, bContext = None,):
  811. from bpy import data
  812. if not bContext: # lol
  813. import bpy
  814. bContext = bpy.context
  815. ob = data.objects.get(self.evaluate_input("Name"))
  816. if not ob:
  817. ob = data.objects.new(self.evaluate_input("Name"), None)
  818. ob.lock_location = [True, True, True]
  819. ob.lock_rotation = [True, True, True]
  820. ob.lock_scale = [True, True, True]
  821. ob.lock_rotation_w = True
  822. ob.empty_display_type = 'CONE'
  823. ob.empty_display_size = 0.10
  824. self.bObject = ob
  825. reset_object_data(ob)
  826. node_line = trace_single_line(self, "Parent Curve")[0][1:] # slice excludes self
  827. for other_node in node_line:
  828. if other_node.node_type == 'XFORM':
  829. break
  830. else:
  831. raise GraphError(f"ERROR: {self} is not connected to a parent curve")
  832. if isinstance(other_node, (xFormArmature, xFormBone, xFormObjectInstance,)):
  833. raise GraphError(f"ERROR: {self} must be connected to curve,"
  834. " not {other_node.__class__.__name__}")
  835. curve=other_node.bGetObject()
  836. if curve.type != 'CURVE':
  837. raise GraphError(f"ERROR: {self} must be connected to curve,"
  838. " not {curve.type}")
  839. # we'll limit all the transforms so we can parent it
  840. # because it is annoying to have a cluttered outliner.
  841. #
  842. # always do this so that everything stays consistent.
  843. spline_index = self.evaluate_input("Spline Index")
  844. from .utilities import get_extracted_spline_object
  845. curve = get_extracted_spline_object(curve, spline_index, self.mContext)
  846. # Link to Scene:
  847. for link_me in [ob, curve]:
  848. if (link_me.name not in bContext.view_layer.active_layer_collection.collection.objects):
  849. bContext.view_layer.active_layer_collection.collection.objects.link(link_me)
  850. c = ob.constraints.new("LIMIT_LOCATION")
  851. for max_min in ['max','min']:
  852. for axis in "xyz":
  853. setattr(c, "use_"+max_min+"_"+axis, True)
  854. setattr(c, max_min+"_"+axis, 0.0)
  855. c = ob.constraints.new("LIMIT_ROTATION")
  856. for axis in "xyz":
  857. setattr(c, "use_limit_"+axis, True)
  858. setattr(c, max_min+"_"+axis, 0.0)
  859. c = ob.constraints.new("LIMIT_SCALE")
  860. for max_min in ['max','min']:
  861. for axis in "xyz":
  862. setattr(c, "use_"+max_min+"_"+axis, True)
  863. setattr(c, max_min+"_"+axis, 1.0)
  864. c = ob.constraints.new("FOLLOW_PATH")
  865. c.target = curve
  866. c.use_fixed_location = True
  867. c.use_curve_radius = True
  868. c.use_curve_follow = True
  869. c.name = "Curve Pin"
  870. props_sockets = self.gen_property_socket_map()
  871. constraint_props_sockets = props_sockets.copy()
  872. del constraint_props_sockets['name']; del constraint_props_sockets['empty_display_size']
  873. del props_sockets['offset_factor']; del props_sockets['forward_axis']
  874. del props_sockets['up_axis']
  875. evaluate_sockets(self, c, constraint_props_sockets)
  876. evaluate_sockets(self, self.bObject, props_sockets)
  877. # this isn't usually run on xForm nodes so for now I need to set the
  878. # driver's default values manually if I want a matrix now.
  879. # because the drivers may not have initialized yet.
  880. self.prep_driver_values(c)
  881. # now if all goes well... the matrix will be correct.
  882. dg = bContext.view_layer.depsgraph
  883. dg.update()
  884. # and the matrix should be correct now - copy because it may be modified
  885. self.parameters['Matrix'] = ob.matrix_world.copy()
  886. ob.parent=curve
  887. print( wrapGreen("Created Curve Pin: ") + wrapOrange(self.bObject.name) )
  888. parent_xForm_info = get_parent_xForm_info(self)
  889. root_armature = parent_xForm_info.root_armature
  890. my_info = xForm_info(
  891. object_type='object',
  892. root_armature= root_armature,
  893. parent_pose_name=curve.name,
  894. parent_edit_name=curve.name,
  895. self_pose_name=self.bObject.name,
  896. self_edit_name=self.bObject.name,
  897. )
  898. self.parameters['xForm Out'] = my_info
  899. self.prepared = True; self.executed = True
  900. def bFinalize(self, bContext = None):
  901. # custom properties
  902. self.setup_custom_props(self.bGetObject(), "Custom Properties")
  903. finish_drivers(self)
  904. def bGetObject(self, mode = 'POSE'):
  905. return self.bObject