Skip to main content
deleted 23 characters in body
Source Link
Robert Gützkow
  • 26k
  • 3
  • 48
  • 82

I would recommend writing a little function that copies the cursor's location and optionally rotation an sets it for the active object.

import bpy


def snap_active_to_cursor(obj: bpy.types.Object, copy_rotation=False):
    cursor = bpy.context.scene.cursor
    active_object = bpy.context.view_layer.objects.active
    active_object.location = cursor.location
    
    if copy_rotation:
        active_object.rotation_euler = cursor.rotation_euler

obj = bpy.context.scene.objects["Cube"]         
snap_active_to_cursor(obj)

If you want to move all selected objects individually your function could look like this:

def snap_selected_to_cursor(copy_rotation=False):
    cursor = bpy.context.scene.cursor
    
    for obj in bpy.context.view_layer.objects.selected:
        obj.location = cursor.location
    
        if copy_rotation:
            obj.rotation_euler = cursor.rotation_euler  

Blender already has an operator with similar functionality, however it requires the 3D View as context. Since custom contexts are not worth the hassle for as simple task like this, I wouldn't use bpy.ops.view3d.snap_selected_to_cursor(use_offset=True) within scripts/add-ons when you only want to move the active object.

I would recommend writing a little function that copies the cursor's location and optionally rotation an sets it for the active object.

import bpy


def snap_active_to_cursor(obj: bpy.types.Object, copy_rotation=False):
    cursor = bpy.context.scene.cursor
    active_object = bpy.context.view_layer.objects.active
    active_object.location = cursor.location
    
    if copy_rotation:
        active_object.rotation_euler = cursor.rotation_euler

obj = bpy.context.scene.objects["Cube"]         
snap_active_to_cursor(obj)

If you want to move all selected objects individually your function could look like this:

def snap_selected_to_cursor(copy_rotation=False):
    cursor = bpy.context.scene.cursor
    
    for obj in bpy.context.view_layer.objects.selected:
        obj.location = cursor.location
    
        if copy_rotation:
            obj.rotation_euler = cursor.rotation_euler  

Blender already has an operator with similar functionality, however it requires the 3D View as context. Since custom contexts are not worth the hassle for as simple task like this, I wouldn't use bpy.ops.view3d.snap_selected_to_cursor(use_offset=True) within scripts/add-ons when you only want to move the active object.

I would recommend writing a little function that copies the cursor's location and sets it for the active object.

import bpy


def snap_active_to_cursor(obj: bpy.types.Object, copy_rotation=False):
    cursor = bpy.context.scene.cursor
    active_object = bpy.context.view_layer.objects.active
    active_object.location = cursor.location
    
    if copy_rotation:
        active_object.rotation_euler = cursor.rotation_euler

obj = bpy.context.scene.objects["Cube"]         
snap_active_to_cursor(obj)

If you want to move all selected objects individually your function could look like this:

def snap_selected_to_cursor(copy_rotation=False):
    cursor = bpy.context.scene.cursor
    
    for obj in bpy.context.view_layer.objects.selected:
        obj.location = cursor.location
    
        if copy_rotation:
            obj.rotation_euler = cursor.rotation_euler  

Blender already has an operator with similar functionality, however it requires the 3D View as context. Since custom contexts are not worth the hassle for as simple task like this, I wouldn't use bpy.ops.view3d.snap_selected_to_cursor(use_offset=True) within scripts/add-ons when you only want to move the active object.

Source Link
Robert Gützkow
  • 26k
  • 3
  • 48
  • 82

I would recommend writing a little function that copies the cursor's location and optionally rotation an sets it for the active object.

import bpy


def snap_active_to_cursor(obj: bpy.types.Object, copy_rotation=False):
    cursor = bpy.context.scene.cursor
    active_object = bpy.context.view_layer.objects.active
    active_object.location = cursor.location
    
    if copy_rotation:
        active_object.rotation_euler = cursor.rotation_euler

obj = bpy.context.scene.objects["Cube"]         
snap_active_to_cursor(obj)

If you want to move all selected objects individually your function could look like this:

def snap_selected_to_cursor(copy_rotation=False):
    cursor = bpy.context.scene.cursor
    
    for obj in bpy.context.view_layer.objects.selected:
        obj.location = cursor.location
    
        if copy_rotation:
            obj.rotation_euler = cursor.rotation_euler  

Blender already has an operator with similar functionality, however it requires the 3D View as context. Since custom contexts are not worth the hassle for as simple task like this, I wouldn't use bpy.ops.view3d.snap_selected_to_cursor(use_offset=True) within scripts/add-ons when you only want to move the active object.