Blender 4.2.3'de yani en yeni blender sürümümde Geometry Nodes kullanarak bir işlem yapmak istiyorum. Amacım şu:
Collection içindeki her geometriyi Convex Hull (Dışbükey Kabuk) ile kaplamak mümkün. Ancak, ben bu yöntemi biraz farklı bir şekilde uygulamak istiyorum.
Convex Hull Node'u kullanarak, koleksiyondaki her iki instance'ı (ki zaten Realize Instances yapıyoruz) birleştirecek şekilde düzenlemek istiyorum.
Yani, tüm mesh'lerin etrafını tek tek kontrol etmek yerine, isim sırasına göre üstten aşağıya her iki geometriyi birleştirip Convex Hull ile kaplamak istiyorum.
Aslında, bu işlemi yapan bir Python script'i yazmıştım ama aynısını Geometry Nodes ile yapmak istiyorum.
Script'imi burada paylaşıyorum, böylece neyi tam olarak yapmak istediğimi anlayabilirsiniz. Ardından, Geometry Nodes'da neyi nereye bağlayacağımı adım adım gösteren bir rehber yapmanı istiyorum fazla yorum eklemek yerine direkt şunu şuna bağla demeni tercih ederim.
Şuan ki setup'ım şöyle :
Collection info (+Seperate Children, -Reset Children) -> Store Named Attribute(Integer, Instance,name"sort_index",value(connected from Index)) -> Sort Elements (GroupID"connected from named Attribute(Integer,name"sort_index")") ->Group Output.
Şu anki hali ile böyle gözüküyor ve şuan ki hali ile zaten tüm var olan geometry'leri attribute olarak isimlendiriyoruz ama bunların sadece sırasal olarak 2 li lerin kabul olması lazım. Bu arada zaten geometry nodes tüm isimlendirme sırasını isime göre veriyor yani bunda bir sıkıntı yok sadece ihtiyacım olan 2 li grupların convex hull yapması! VE EMİNİM BUNU BLENDER'DA YAPABİLİRİZ ADAMLAR OYUN BİLE YAPIYORLAR!!!
İşte bahsettiğim script:
code
Assistant
import bpy
import bmesh
from mathutils import Vector
handler_running = False
previous_collection_state = None # Koleksiyonun önceki durumunu saklamak için global değişken
def get_boundary_vertices(obj):
obj_matrix = obj.matrix_world
mesh = obj.data
bm = bmesh.new()
bm.from_mesh(mesh)
boundary_verts = []
ret = bmesh.ops.convex_hull(bm, input=bm.verts[:])
hull_verts = [v for v in bm.verts if v.select]
for v in hull_verts:
co = obj_matrix @ v.co
boundary_verts.append(co)
bm.free()
return boundary_verts
def create_dynamic_mesh(vertices, faces):
mesh_name = "DynamicMesh"
if mesh_name in bpy.data.objects:
obj = bpy.data.objects[mesh_name]
mesh = obj.data
mesh.clear_geometry()
else:
mesh = bpy.data.meshes.new(mesh_name)
obj = bpy.data.objects.new(mesh_name, mesh)
bpy.context.scene.collection.objects.link(obj)
mesh.from_pydata(vertices, [], faces)
mesh.update()
def get_collection_state(collection):
"""Koleksiyonun mevcut durumunu temsil eden bir tuple döner."""
state = []
for obj in sorted(collection.objects, key=lambda o: o.name):
if obj.type == 'MESH':
state.append((
obj.name,
tuple(obj.matrix_world.decompose()), # Konum, rotasyon, ölçek
obj.data.name
))
return tuple(state)
def dynamic_mesh_creator_handler(scene):
global previous_collection_state
source_collection = bpy.data.collections.get("SourceMesh")
if not source_collection:
return
current_state = get_collection_state(source_collection)
if current_state == previous_collection_state:
# Koleksiyonda bir değişiklik yok, güncelleme yapma
return
else:
# Koleksiyon değişti, güncelle
previous_collection_state = current_state
mesh_objects = sorted([obj for obj in source_collection.objects if obj.type == 'MESH'], key=lambda o: o.name)
if not mesh_objects:
return
all_vertices = []
all_faces = []
vertex_offset = 0
for i in range(len(mesh_objects) - 1):
obj1 = mesh_objects[i]
obj2 = mesh_objects[i + 1]
boundary1 = get_boundary_vertices(obj1)
boundary2 = get_boundary_vertices(obj2)
bridge_vertices = boundary1 + boundary2
bm = bmesh.new()
for co in bridge_vertices:
bm.verts.new(co)
bm.verts.ensure_lookup_table()
bmesh.ops.convex_hull(bm, input=bm.verts)
bm.verts.ensure_lookup_table()
for face in bm.faces:
all_faces.append([v.index + vertex_offset for v in face.verts])
all_vertices.extend([v.co[:] for v in bm.verts])
vertex_offset += len(bm.verts)
bm.free()
if not all_vertices or not all_faces:
return
create_dynamic_mesh(all_vertices, all_faces)
def register_handler():
global handler_running, previous_collection_state
if not handler_running:
bpy.app.handlers.depsgraph_update_post.append(dynamic_mesh_creator_handler)
handler_running = True
# İlk durumda koleksiyonun durumunu sakla
source_collection = bpy.data.collections.get("SourceMesh")
if source_collection:
previous_collection_state = get_collection_state(source_collection)
def unregister_handler():
global handler_running, previous_collection_state
if handler_running:
bpy.app.handlers.depsgraph_update_post.remove(dynamic_mesh_creator_handler)
handler_running = False
previous_collection_state = None
mesh_name = "DynamicMesh"
if mesh_name in bpy.data.objects:
obj = bpy.data.objects[mesh_name]
bpy.data.objects.remove(obj, do_unlink=True)
class DynamicMeshCreatorPanel(bpy.types.Panel):
bl_label = "Dynamic Mesh Creator"
bl_idname = "VIEW3D_PT_dynamic_mesh_creator"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'DynamicMesh'
def draw(self, context):
layout = self.layout
if not handler_running:
layout.operator("wm.dynamic_mesh_creator", text="Aktif Hale Getir")
else:
layout.operator("wm.dynamic_mesh_creator", text="Devre Dışı Bırak")
class WM_OT_dynamic_mesh_creator(bpy.types.Operator):
bl_label = "Dynamic Mesh Creator Operator"
bl_idname = "wm.dynamic_mesh_creator"
def execute(self, context):
if not handler_running:
register_handler()
else:
unregister_handler()
return {'FINISHED'}
def register():
bpy.utils.register_class(DynamicMeshCreatorPanel)
bpy.utils.register_class(WM_OT_dynamic_mesh_creator)
def unregister():
bpy.utils.unregister_class(DynamicMeshCreatorPanel)
bpy.utils.unregister_class(WM_OT_dynamic_mesh_creator)
unregister_handler()
if name == "main":
register()
Sure, I can guide you through setting up your Geometry Nodes network to combine every two geometries in your collection (sorted by name) and apply the Convex Hull to each pair. Here's how you can do it step by step:
Node Setup Steps:
Add a Collection Info Node:
Action: Add a Collection Info node to your Geometry Nodes tree.
Settings:
Collection: Set this to the collection containing your geometries (e.g., SourceMesh).
Separate Children:Enabled (check the box).
Reset Children:Disabled (uncheck the box).
Add a Sort Instances Node:
Action: Add a Sort Instances node.
Connection: Connect the Instances output of Collection Info to the Instances input of Sort Instances.
Settings:
Sort By: Set to Name (this ensures the geometries are sorted by their names).
Add an Instance Index Node:
Action: Add an Instance Index node. This provides the index of each instance after sorting.
Calculate group_id for Each Pair:
Add a Math Node (Divide):
Action: Add a Math node and set it to Divide.
Connection: Connect the Instance Index output to the first input (Value).
Settings:
Second Input: Set to 2.
This will calculate the index divided by 2.
Add a Math Node (Floor):
Action: Add another Math node and set it to Floor.
Connection: Connect the output of the previous Divide node to this node's input (Value).
This will floor the division result to get integer group IDs (0, 1, 2, ...).
Store group_id as an Attribute:
Add a Store Named Attribute Node:
Action: Add a Store Named Attribute node.
Connection:
Geometry Input: Connect from the Instances output of the Sort Instances node.
Value Input: Connect from the output of the Floor node (which computes group_id).
Settings:
Data Type: Set to Integer.
Domain: Set to Instance.
Name: Set to "group_id".
Realize Instances:
Add a Realize Instances Node:
Action: Add a Realize Instances node.
Connection: Connect the Geometry output of the Store Named Attribute node to the Geometry input of the Realize Instances node.
This converts the instances into actual geometry for further processing.
Separate Geometries by group_id:
Since Geometry Nodes doesn't support dynamic loops over group IDs, we'll manually handle each group.
For group_id = 0:
Add a Compare Node:
Action: Add a Compare node (set to Equal).
Connection:
First Input (A): Use the Attribute node to get the "group_id" attribute from the geometry. Connect this to A.
Second Input (B): Set to 0.
Add a Separate Geometry Node:
Action: Add a Separate Geometry node.
Connection:
Geometry Input: Connect from the Geometry output of the Realize Instances node.
Selection Input: Connect from the output of the Compare node.
This node separates the geometry where group_id equals 0.
Add a Convex Hull Node:
Action: Add a Convex Hull node.
Connection: Connect the Geometry output (the selected geometry) of the Separate Geometry node to the Geometry input of the Convex Hull node.
This computes the Convex Hull for the first pair.
Repeat the Above Steps for Each group_id:
For group_id = 1, 2, 3, etc., repeat the steps by changing the Second Input (B) value in the Compare node to match the group_id.
Each time, you'll create a separate geometry for that group and compute its Convex Hull.
Combine All Convex Hulls:
Add a Join Geometry Node:
Action: Add a Join Geometry node.
Connection: Connect the Convex Hull outputs from each group to the Geometry inputs of the Join Geometry node.
This combines all the Convex Hull geometries into one.
Connect to Group Output:
Action: Connect the Geometry output of the Join Geometry node to the Geometry input of the Group Output node.
This outputs the final geometry that consists of Convex Hulls of each pair.
Visual Node Setup Overview:
code
Collection Info (Separate Children)
└──> Sort Instances (Sort By Name)
└──> Store Named Attribute ("group_id", from Floor(Index / 2))
└──> Realize Instances
└──> [Separate and process each group_id]
└──> Convex Hulls
└──> Join Geometry
└──> Group Output
Notes:
Manual Group Handling: This method requires manually setting up nodes for each group_id. If you have a large number of pairs, this can become cumbersome.
Dynamic Looping Limitations: As of Blender 4.2.3, Geometry Nodes may not support dynamic loops over attribute values directly within the node tree. This is why we're handling each group manually.
Alternative Approaches: For a more scalable solution, you might consider using scripting or waiting for future updates to Geometry Nodes that introduce more advanced looping or grouping functionalities.
Example with 3 Pairs (group_id = 0, 1, 2):
For group_id = 0:
Compare Node: B = 0
Separate Geometry: Separates first pair.
Convex Hull: Computes Convex Hull of the first pair.
For group_id = 1:
Compare Node: B = 1
Separate Geometry: Separates second pair.
Convex Hull: Computes Convex Hull of the second pair.
For group_id = 2:
Compare Node: B = 2
Separate Geometry: Separates third pair.
Convex Hull: Computes Convex Hull of the third pair.
Join Geometry: Combines all Convex Hull outputs into a single geometry.
Final Steps:
Ensure all nodes are connected correctly.
Test the setup with your collection to verify it works as expected.
Summary:
Collection Info (with Separate Children) →
Sort Instances (by Name) →
Store Named Attribute ("group_id" from Floor(Index / 2)) →
Realize Instances →
For each group_id (0, 1, 2, ...):
Compare group_id == N → Separate Geometry → Convex Hull
Join Geometry (Convex Hull results) →
Group Output
I hope this step-by-step guide helps you set up your Geometry Nodes network to combine every two geometries and apply the Convex Hull as you intended!