Utilisateur
Write a blender script that will set "cursor to selected" then set the origin of that mesh to cursor.
John Carmack
Write a blender script that will set "cursor to selected" then set the origin of that mesh to cursor.
Creating a script for Blender to set the cursor to the selected object and then set the origin of that mesh to the cursor involves using Blender's Python API. Here's a basic script that achieves this:
import bpy
# Ensure there is an active object
if bpy.context.active_object is not None:
# Set the mode to OBJECT if it's not already
if bpy.context.object.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
# Set the cursor to the active object's location
bpy.context.scene.cursor.location = bpy.context.active_object.location
# Set the origin of the active object to the cursor's location
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
else:
print("No active object found.")
This script checks for an active object and changes its mode to 'OBJECT' if it's not already. It then moves the cursor to the active object's location and sets the object's origin to the cursor's location. If no object is selected, it prints a message in the console.
Please make sure to have the right object selected before running this script, as it operates on the currently active object in Blender.
All it did was put me in object mode from edit mode. It should never take me out of the mode im in unless specified by me. It did not do any of the other functionalities
To modify the script so it does not change the mode you're in, and correctly sets the cursor to the selected part of the mesh and then sets the origin to the cursor, you can try the following approach:
Here's the revised script:
import bpy
import bmesh
# Ensure there is an active object and it's in EDIT mode
obj = bpy.context.active_object
if obj is not None and obj.mode == 'EDIT':
# Store the current mode
current_mode = bpy.context.object.mode
# Get the active mesh
mesh = bmesh.from_edit_mesh(obj.data)
# Calculate the median of selected vertices
selected_verts = [v.co for v in mesh.verts if v.select]
if selected_verts:
median = sum(selected_verts, bmesh.geometry.Vector()) / len(selected_verts)
# Convert the median to global coordinate
global_median = obj.matrix_world @ median
# Switch to OBJECT mode to set cursor and origin
bpy.ops.object.mode_set(mode='OBJECT')
bpy.context.scene.cursor.location = global_median
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
# Switch back to the original mode
bpy.ops.object.mode_set(mode=current_mode)
else:
print("No vertices selected in edit mode.")
else:
print("Either no active object or not in edit mode.")
To use this script:
Please make sure you have the correct object and vertices selected before running this script.
Python: Traceback (most recent call last): File "C:\Users\lilkewk\AppData\Roaming\Blender Foundation\Blender\4.0\scripts\addons\dynamic_script_loader.py", line 39, in execute exec(compile(open(filepath).read(), filepath, 'exec')) File "E:\blender\scripts\scripts\cursor to selected + origin set.py", line 16, in median = sum(selected_verts, bmesh.geometry.Vector()) / len(selected_verts) AttributeError: module 'bmesh.geometry' has no attribute 'Vector'
The error you're encountering is due to the incorrect reference to the Vector class in the bmesh module. The Vector class should be accessed from the mathutils module, not bmesh.geometry. Let's correct that and update the script.
Here's the revised script:
import bpy
import bmesh
from mathutils import Vector
# Ensure there is an active object and it's in EDIT mode
obj = bpy.context.active_object
if obj is not None and obj.mode == 'EDIT':
# Store the current mode
current_mode = bpy.context.object.mode
# Get the active mesh
mesh = bmesh.from_edit_mesh(obj.data)
# Calculate the median of selected vertices
selected_verts = [v.co for v in mesh.verts if v.select]
if selected_verts:
median = sum(selected_verts, Vector()) / len(selected_verts)
# Convert the median to global coordinate
global_median = obj.matrix_world @ median
# Switch to OBJECT mode to set cursor and origin
bpy.ops.object.mode_set(mode='OBJECT')
bpy.context.scene.cursor.location = global_median
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
# Switch back to the original mode
bpy.ops.object.mode_set(mode=current_mode)
else:
print("No vertices selected in edit mode.")
else:
print("Either no active object or not in edit mode.")
To use this updated script:
This corrected script should resolve the AttributeError you encountered.