John Carmack

Set Origin to Cursor

John Carmack

打开原始分享对话

用户

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

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 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.") ``` 4. 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.

用户

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

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 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: 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.

用户

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

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 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: 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.