geometry_node_graphgen.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from .utilities import (prRed, prGreen, prPurple, prWhite, prOrange,
  2. wrapRed, wrapGreen, wrapPurple, wrapWhite,
  3. wrapOrange,)
  4. def gen_morph_target_nodes(mod_name, mod_ob, targets, context, use_offset=True):
  5. from bpy import data
  6. modifier = mod_ob.modifiers.new(mod_name, type='NODES')
  7. mod_ob.add_rest_position_attribute = True
  8. ng = data.node_groups.new(mod_name, "GeometryNodeTree")
  9. modifier.node_group = ng
  10. ng.interface.new_socket("Geometry", in_out="INPUT", socket_type="NodeSocketGeometry")
  11. ng.interface.new_socket("Geometry", in_out="OUTPUT", socket_type="NodeSocketGeometry")
  12. inp = ng.nodes.new("NodeGroupInput")
  13. out = ng.nodes.new("NodeGroupOutput")
  14. # TODO CLEANUP here
  15. if (position := ng.nodes.get("Position")) is None: position = ng.nodes.new("GeometryNodeInputPosition")
  16. if (index := ng.nodes.get("Index")) is None: index = ng.nodes.new("GeometryNodeInputIndex")
  17. rest_position = ng.nodes.new("GeometryNodeInputNamedAttribute")
  18. rest_position.inputs["Name"].default_value="rest_position"
  19. rest_position.data_type = 'FLOAT_VECTOR'
  20. if use_offset == False:
  21. rest_position = position
  22. add_these = []
  23. props_sockets={}
  24. object_map = {}
  25. for i, t in enumerate(targets):
  26. mt_node = t.links[0].from_node
  27. mt_ob = mt_node.GetxForm().bGetObject()
  28. if mt_ob is None: # create it
  29. mt_ob = data.objects.new(mt_node.evaluate_input("Name"), data.meshes.new_from_object(self_ob))
  30. context.collection.objects.link(mt_ob)
  31. prOrange(f"WARN: no object found for f{mt_node}; creating duplicate of current object ")
  32. mt_name = mt_ob.name
  33. vg = mt_node.parameters["Morph Target"]["vertex_group"]
  34. if vg: mt_name = mt_name+"."+vg
  35. try:
  36. ob_relative = t.links[0].from_node.inputs["Relative to"].links[0].from_node.bGetObject()
  37. except IndexError:
  38. ob_relative = None
  39. ng.interface.new_socket(mt_name, in_out = "INPUT", socket_type="NodeSocketObject")
  40. ng.interface.new_socket(mt_name+" Value", in_out = "INPUT", socket_type="NodeSocketFloat")
  41. ob_node = ng.nodes.new("GeometryNodeObjectInfo")
  42. sample_index = ng.nodes.new("GeometryNodeSampleIndex"); sample_index.data_type = 'FLOAT_VECTOR'
  43. subtract = ng.nodes.new("ShaderNodeVectorMath"); subtract.operation="SUBTRACT"
  44. scale1 = ng.nodes.new("ShaderNodeVectorMath"); scale1.operation="SCALE"
  45. ng.links.new(input=inp.outputs[mt_name], output=ob_node.inputs["Object"])
  46. ng.links.new(input=index.outputs["Index"], output=sample_index.inputs["Index"])
  47. ng.links.new(input=position.outputs["Position"], output=sample_index.inputs["Value"])
  48. ng.links.new(input=sample_index.outputs["Value"], output=subtract.inputs[0])
  49. ng.links.new(input=ob_node.outputs["Geometry"], output=sample_index.inputs["Geometry"])
  50. if ob_relative: # TODO: this should also be exposed as an input
  51. ob_node1 = ng.nodes.new("GeometryNodeObjectInfo"); ob_node1.inputs["Object"].default_value = ob_relative
  52. sample_index1 = ng.nodes.new("GeometryNodeSampleIndex"); sample_index1.data_type = 'FLOAT_VECTOR'
  53. ng.links.new(input=index.outputs["Index"], output=sample_index1.inputs["Index"])
  54. ng.links.new(input=position.outputs["Position"], output=sample_index1.inputs["Value"])
  55. ng.links.new(input=ob_node1.outputs["Geometry"], output=sample_index1.inputs["Geometry"])
  56. ng.links.new(input=sample_index1.outputs["Value"], output=subtract.inputs[1])
  57. else:
  58. # ng.links.new(input=rest_position.outputs["Attribute"], output=subtract.inputs[1])
  59. ng.links.new(input=rest_position.outputs[0], output=subtract.inputs[1])
  60. ng.links.new(input=subtract.outputs["Vector"], output=scale1.inputs[0])
  61. # TODO: this should be exposed as a node tree input
  62. if vg:= mt_node.evaluate_input("Vertex Group"): # works
  63. vg_att = ng.nodes.new("GeometryNodeInputNamedAttribute"); vg_att.inputs["Name"].default_value=vg
  64. multiply = ng.nodes.new("ShaderNodeMath"); multiply.operation = "MULTIPLY"
  65. ng.links.new(input=vg_att.outputs["Attribute"], output=multiply.inputs[1])
  66. ng.links.new(input=inp.outputs[mt_name+" Value"], output=multiply.inputs[0])
  67. ng.links.new(input=multiply.outputs[0], output=scale1.inputs["Scale"])
  68. else:
  69. ng.links.new(input=inp.outputs[mt_name+" Value"], output=scale1.inputs["Scale"])
  70. add_these.append(scale1)
  71. object_map["Socket_"+str((i+1)*2)]=mt_node.GetxForm().bGetObject()
  72. props_sockets["Socket_"+str((i+1)*2+1)]= ("Value."+str(i).zfill(3), 1.0)
  73. set_position = ng.nodes.new("GeometryNodeSetPosition")
  74. bake = ng.nodes.new("GeometryNodeBake")
  75. ng.links.new(inp.outputs["Geometry"], output=set_position.inputs["Geometry"])
  76. ng.links.new(set_position.outputs["Geometry"], output=bake.inputs[0])
  77. ng.links.new(bake.outputs[0], output=out.inputs[0])
  78. if use_offset == True:
  79. prev_node = ng.nodes.new("FunctionNodeInputVector")
  80. else:
  81. prev_node = position
  82. for i, node in enumerate(add_these):
  83. add = ng.nodes.new("ShaderNodeVectorMath"); add.operation="ADD"
  84. ng.links.new(prev_node.outputs[0], output=add.inputs[0])
  85. ng.links.new(node.outputs[0], output=add.inputs[1])
  86. prev_node = add
  87. if use_offset == True:
  88. ng.links.new(add.outputs[0], output=set_position.inputs["Offset"])
  89. else:
  90. ng.links.new(add.outputs[0], output=set_position.inputs["Position"])
  91. try:
  92. from .utilities import SugiyamaGraph
  93. SugiyamaGraph(ng, 12)
  94. except ImportError:
  95. pass # this is unlikely to fail since I package the wheel but if it does it shouldn't crash out.
  96. for socket, ob in object_map.items():
  97. modifier[socket]=ob
  98. return modifier, props_sockets
  99. def gen_object_instance_node_group():
  100. from bpy import data
  101. ng = data.node_groups.new("Object Instance", "GeometryNodeTree")
  102. ng.interface.new_socket("Object", in_out = "INPUT", socket_type="NodeSocketObject")
  103. ng.interface.new_socket("As Instance", in_out = "INPUT", socket_type="NodeSocketBool")
  104. ng.interface.new_socket("Object Instance", in_out="OUTPUT", socket_type="NodeSocketGeometry")
  105. inp = ng.nodes.new("NodeGroupInput")
  106. ob_node = ng.nodes.new("GeometryNodeObjectInfo")
  107. out = ng.nodes.new("NodeGroupOutput")
  108. ng.links.new(input=inp.outputs["Object"], output=ob_node.inputs["Object"])
  109. ng.links.new(input=inp.outputs["As Instance"], output=ob_node.inputs["As Instance"])
  110. ng.links.new(input=ob_node.outputs["Geometry"], output=out.inputs["Object Instance"])
  111. inp.location = (-200, 0)
  112. out.location = ( 200, 0)
  113. return ng