This is an oerator for a blender addon that scatters asset instances across a mesh…
- User
This is an oerator for a blender addon that scatters asset instances across a mesh terrain. Since e no have an efficient scattering engine in python as oppose to geometry nodes, hat are some advanced features e could no implement that ould not be possible ith geometry nodes?
class TF_OT_Scatter(bpy.types.Operator): bl_label = "Scatter" bl_idname = "TF.scatter" bl_description = "Scatter objects across terrain mesh"
def execute(self, context): try: obj = get_terrain_object() if not obj: print("Cannot find valid terrain object") return {'CANCELLED'} scene_props = obj.TF_scatter_data if not scene_props.layers: self.report({'WARNING'}, "No layers to scatter.") return {'CANCELLED'} for layer in scene_props.layers: print(f"Scattering Layer: {layer.name}") self.scatter_layer(context, layer) self.report({'INFO'}, "Scattering completed.") return {'FINISHED'} except Exception as e: self.report({'ERROR'}, f"Error during scattering: {e}") print(f"Error during scattering: {e}") return {'CANCELLED'} def scatter_layer(self, context, layer): veg_directory = context.scene.TF_vegetation_path model_path = os.path.join(veg_directory, layer.model) imported_object = self.import_fbx(model_path, layer.model) if not imported_object: print(f"Failed to import model: {layer.model}") return # Hide the imported object to prevent duplicate rendering imported_object.hide_render = True imported_object.hide_viewport = True target_object = get_terrain_object() if not target_object or target_object.type != 'MESH': print("No valid target object selected for scattering.") return # Ensure we have up-to-date mesh data depsgraph = context.evaluated_depsgraph_get() eval_obj = target_object.evaluated_get(depsgraph) mesh = eval_obj.to_mesh() # Get face and vertex data faces = np.array([face.vertices for face in mesh.polygons]) verts = np.array([vert.co for vert in mesh.vertices]) normals = np.array([face.normal for face in mesh.polygons]) face_areas = np.array([face.area for face in mesh.polygons]) num_points = layer.density # Sample points on the mesh surface sampled_points, sampled_normals = self.sample_points_on_mesh(verts, faces, normals, face_areas, num_points) # Adjust points based on terrain characteristics weights = self.calculate_weights(sampled_points, sampled_normals, layer) accepted_indices = self.filter_points(weights) filtered_points = sampled_points[accepted_indices] # Enforce minimum spacing (resource competition) final_points, final_normals = self.enforce_min_spacing(filtered_points, sampled_normals, layer.min_spacing) print(f"Placing {len(final_points)} instances for layer '{layer.name}'") # Scatter the objects for loc, normal in zip(final_points, final_normals): obj = self.create_instance(imported_object, loc, normal, layer) obj.TF_SID = layer.TF_SID context.collection.objects.link(obj) # Clean up eval_obj.to_mesh_clear() def import_fbx(self, filepath, model): try: bpy.ops.import_scene.fbx(filepath=filepath) name = str(model).split(".")[0] imported_object = bpy.data.objects[name] #imported_objects = bpy.context.selected_object print(f"Imported model: {filepath}") return imported_object except Exception as e: print(f"Failed to import FBX: {e}") return None def create_instance(self, source_obj, location, normal, layer): #obj_copy = source_obj.copy() #obj_copy.data = source_obj.data obj_copy = bpy.data.objects.new(name=source_obj.name, object_data=source_obj.data) obj_copy.location = location scale_rand = ((np.random.rand() * layer.scale_rand) + (1 - (np.random.rand() * layer.scale_rand))) obj_copy.scale *= (layer.scale * scale_rand) rotation_rand = ((2 * np.pi) - ((layer.rot_rand * np.random.rand()) * 2 * np.pi)) + (layer.rot * 2 * np.pi) obj_copy.rotation_euler.rotate_axis('Z', rotation_rand) self.align_object(obj_copy, normal, layer) print(f"Created instance at {location}") return obj_copy def align_object(self, obj, normal, layer): if layer.align_to_normal: # Align object to the normal up = Vector((0, 0, 1)) direction = normal.normalized() rotation = up.rotation_difference(direction).to_euler() obj.rotation_euler = rotation else: # Default orientation pass def sample_points_on_mesh(self, verts, faces, normals, face_areas, num_points): # Normalize face areas to probabilities face_probs = face_areas / face_areas.sum() # Randomly select faces based on area chosen_faces = np.random.choice(len(faces), size=num_points, p=face_probs) # Generate random barycentric coordinates us = np.random.rand(num_points, 1) vs = np.random.rand(num_points, 1) ws = 1 - us - vs mask = ws.flatten() < 0 us[mask], vs[mask] = vs[mask], us[mask] ws = 1 - us - vs # Get vertices of the chosen faces v0 = verts[faces[chosen_faces, 0]] v1 = verts[faces[chosen_faces, 1]] v2 = verts[faces[chosen_faces, 2]] # Calculate sampled points sampled_points = us * v0 + vs * v1 + ws * v2 sampled_normals = normals[chosen_faces] return sampled_points, sampled_normals def calculate_weights(self, points, normals, layer): # Calculate slope based on normal vectors slopes = 1 - np.abs(normals[:, 2]) # Assuming Z-up axis slope_influence = layer.slope_influence # Calculate altitude altitudes = points[:, 2] altitude_influence = layer.altitude_influence # Normalize altitudes altitudes_norm = (altitudes - altitudes.min()) / (altitudes.max() - altitudes.min() + 1e-6) # Calculate weights weights = np.ones(len(points)) weights *= np.exp(-slope_influence * slopes) weights *= np.exp(-altitude_influence * altitudes_norm) return weights def filter_points(self, weights): # Normalize weights to probabilities probabilities = weights / weights.max() # Accept or reject points random_values = np.random.rand(len(weights)) accepted_indices = np.where(random_values < probabilities)[0] return accepted_indices def enforce_min_spacing(self, points, normals, min_spacing): if len(points) == 0: return points, normals tree = kdtree(len(points)) for i, co in enumerate(points): tree.insert(co, i) tree.balance() accepted_indices = [] rejected = set() for i, co in enumerate(points): if i in rejected: continue accepted_indices.append(i) # Find points within min_spacing nearby = tree.find_range(co, min_spacing) for (co_found, index, dist) in nearby: if index > i: rejected.add(index) accepted_points = points[accepted_indices] accepted_normals = normals[accepted_indices] print(f"After enforcing min spacing, {len(accepted_points)} points remain.") return accepted_points, accepted_normals - Assistant