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