Custom Snaping Menu

Hi everyone, I’m Mar10.

I’m trying to make a custom menu to snap the 3d cursor to the active camera, then select the light and snapped to the 3d cursor, the lamp will be changed into a sun with strength of 0.6, The sun will be parented to the active camera so it can move anywhere the camera moves, the active camera will be aligned to view.

I try to use the code generated when this actions are performed, but when I try to run the script I get an error of " context is not correct "

I understand that that error is caused because the script needs to be run when the 3d cursor is in the 3d view window, but if I try to run then script in the 3d view the script do no run.

Can somebody help me pease?

This is the code:

import bpy

Here the 3d camera is selecte, so the line of code below snaps the 3d cursor to the selected camera.

bpy.ops.view3d.snap_cursor_to_selected()

Here the 3d cursor is snapped to the active camera. and the light is selected and will be snapped to the 3d cursor.

bpy.ops.view3d.snap_selected_to_cursor(use_offset=False)

Here I switched to the light settings tab to change the light to a sun.

bpy.context.space_data.context = ‘DATA’

Here the point light is changed to a sun

bpy.context.object.data.type = ‘SUN’

Here I rotate the sun to point at the center of the scene.

bpy.ops.transform.rotate(value=-0.258031, axis=(-0.683394, -0.446214, -0.577811), constraint_axis=(False, False, False), constraint_orientation=‘GLOBAL’, mirror=False, proportional=‘DISABLED’, proportional_edit_falloff=‘SMOOTH’, proportional_size=1)

Here I parent the sun light to the active camera.

bpy.ops.object.parent_set(type=‘OBJECT’, keep_transform=False)

Here I move the sun light up above the active camera.

bpy.ops.transform.translate(value=(0, -0.0685539, 0), constraint_axis=(False, True, False), constraint_orientation=‘GLOBAL’, mirror=False, proportional=‘DISABLED’, proportional_edit_falloff=‘SMOOTH’, proportional_size=1, release_confirm=True, use_accurate=False)

Here I align the active camera to view.

bpy.ops.view3d.camera_to_view()

Here I check the check mark to keep the camera alight to view " in the properties panel.

bpy.context.space_data.lock_camera = True

When you get time, paste here the complete error report, together with the problematic line. From ‘Traceback:…’
That helps users help you.

Hi, Okavango, thank you for the reply.

I try to copy the error but its not possible from the console.

to get the error please run the first line of code.

import bpy

bpy.ops.view3d.snap_cursor_to_selected()

once you run this code you will get the error, Thanks again.

The “context is not correct” error is because you are trying to run a view3d operator from outside the 3D View. This is not possible without using an override or registering your script to a custom operator.

I am a little confused about what your goal is. Are you trying to have a sun moved to the “origin” of the active camera before parenting it? If this is the case, you can do that directly without using the 3D cursor. Something like this would work:

import bpy
import mathutils

def test_function():
    active_camera = bpy.context.scene.camera
    if active_camera is None:
        if 'Camera' in bpy.context.scene.objects:
            active_camera = bpy.context.scene.objects['Camera']
        else:
            # no camera found, do nothing...
            return

    sel_lamp = bpy.context.selected_objects[0]
    # for moving "up" 0.5 units on Z axis
    height_offset = mathutils.Vector((0.0, 0.0, 0.5))
    sel_lamp.location = active_camera.location + height_offset


test_function()

This is more of a “quick and dirty” way of changing the lamp’s position. The proper way would be to use a translation matrix from Blender’s mathutils module:

mathutils.Matrix.Translation()