Set Origin to Cursor

Anonymous

Write a blender script that will set "cursor to selected" then set the origin of that mesh to cursor.

John Carmack logo
John Carmack

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:

  1. First, ensure that you have Blender open and an object selected.
  2. Open a new text editor window in Blender.
  3. Paste the following script into the text editor.
python
1import bpy 2 3# Ensure there is an active object 4if bpy.context.active_object is not None: 5 # Set the mode to OBJECT if it's not already 6 if bpy.context.object.mode != 'OBJECT': 7 bpy.ops.object.mode_set(mode='OBJECT') 8 9 # Set the cursor to the active object's location 10 bpy.context.scene.cursor.location = bpy.context.active_object.location 11 12 # Set the origin of the active object to the cursor's location 13 bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN') 14else: 15 print("No active object found.")
  1. After pasting this script, you can run it by clicking on the "Run Script" button in the text editor.

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.

Anonymous

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

John Carmack logo
John Carmack

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:

  1. This script assumes you are in Edit Mode and have selected part of a mesh.
  2. It will store your current mode, then switch to Object Mode to change the origin, and finally switch back to your original mode.

Here's the revised script:

python
1import bpy 2import bmesh 3 4# Ensure there is an active object and it's in EDIT mode 5obj = bpy.context.active_object 6if obj is not None and obj.mode == 'EDIT': 7 # Store the current mode 8 current_mode = bpy.context.object.mode 9 10 # Get the active mesh 11 mesh = bmesh.from_edit_mesh(obj.data) 12 13 # Calculate the median of selected vertices 14 selected_verts = [v.co for v in mesh.verts if v.select] 15 if selected_verts: 16 median = sum(selected_verts, bmesh.geometry.Vector()) / len(selected_verts) 17 # Convert the median to global coordinate 18 global_median = obj.matrix_world @ median 19 20 # Switch to OBJECT mode to set cursor and origin 21 bpy.ops.object.mode_set(mode='OBJECT') 22 bpy.context.scene.cursor.location = global_median 23 bpy.ops.object.origin_set(type='ORIGIN_CURSOR') 24 25 # Switch back to the original mode 26 bpy.ops.object.mode_set(mode=current_mode) 27 else: 28 print("No vertices selected in edit mode.") 29else: 30 print("Either no active object or not in edit mode.")

To use this script:

  1. Make sure you are in Edit Mode and have part of a mesh selected.
  2. Paste this script into the Blender text editor and run it.
  3. The script will calculate the median of the selected vertices, set the cursor to this position, change the origin of the object to the cursor, and then return you to your original mode.

Please make sure you have the correct object and vertices selected before running this script.

Anonymous

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 <module> median = sum(selected_verts, bmesh.geometry.Vector()) / len(selected_verts) AttributeError: module 'bmesh.geometry' has no attribute 'Vector'

John Carmack logo
John Carmack

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:

python
1import bpy 2import bmesh 3from mathutils import Vector 4 5# Ensure there is an active object and it's in EDIT mode 6obj = bpy.context.active_object 7if obj is not None and obj.mode == 'EDIT': 8 # Store the current mode 9 current_mode = bpy.context.object.mode 10 11 # Get the active mesh 12 mesh = bmesh.from_edit_mesh(obj.data) 13 14 # Calculate the median of selected vertices 15 selected_verts = [v.co for v in mesh.verts if v.select] 16 if selected_verts: 17 median = sum(selected_verts, Vector()) / len(selected_verts) 18 # Convert the median to global coordinate 19 global_median = obj.matrix_world @ median 20 21 # Switch to OBJECT mode to set cursor and origin 22 bpy.ops.object.mode_set(mode='OBJECT') 23 bpy.context.scene.cursor.location = global_median 24 bpy.ops.object.origin_set(type='ORIGIN_CURSOR') 25 26 # Switch back to the original mode 27 bpy.ops.object.mode_set(mode=current_mode) 28 else: 29 print("No vertices selected in edit mode.") 30else: 31 print("Either no active object or not in edit mode.")

To use this updated script:

  1. Ensure you are in Edit Mode and have part of a mesh selected.
  2. Paste this script into the Blender text editor and run it.
  3. The script will calculate the median of the selected vertices, set the cursor to this position, change the origin of the object to the cursor, and then return you to your original mode.

This corrected script should resolve the AttributeError you encountered.