How can I get set spotlight size to update when I change the camera lens angle? The goal is to preview wich camera can see in scene and configure them trought custom panel. Need update in viewport spotlight and camera in the viewport

  1. With driver: driver is not updated automatically, and I need to update manually
  2. By Script: How can I call a function when camera angle value change? I try tu use custom prop with update function but I don't know how to make it work

This simple script:

#bpy.data.lamps['Spot'].spot_size = bpy.data.cameras['Camera'].angle

import bpy

# Define property.
class MyLensProp(bpy.types.PropertyGroup):  
bpy.types.Object.Lens_ValueL = bpy.props.FloatProperty(name = "FOV     angle", default = 0.5, min = 0, max = 360,update= Update_Lampe_Ange)

def Update_Lampe_Ange(self, context):
    C= bpy.data.cameras['Camera'].angle
    bpy.data.lamps['Spot'].Lens_ValueL =  C

    PL= bpy.data.lamps['Spot'].Lens_ValueL
    bpy.data.lamps['Spot'].spot_size = PL 

def register():

def unregister():

if __name__ == "__main__":
  • $\begingroup$ In blender.stackexchange.com/a/90723/29586 the driven value is only automatically updated on change of frame. Does animating the active camera trigger the update of the driver on frame change? $\endgroup$ Feb 3 '18 at 21:36
  • $\begingroup$ Frame change, update the Spot size but not change the preview lighting cone. $\endgroup$
    – Ellt
    Feb 3 '18 at 21:57

Here's a solution that certainly works for me on Blender 2.76b.

I believe your problem is that the driver values aren't propagating through correctly. I have had problems with certain drivers types in the past - for example, with drivers on values within materials. However, I have always had consistent results with drivers on Object Custom Properties and this forms the basis of this solution.

Start by creating your spotlight and add constraints to Copy Location and Copy Rotation to the camera. This will ensure the lamp always resides at the point of the camera.

lamp constraints

Select the camera and go to its Object properties. Create a new Custom Property to drive the camera attributes. By using a property to drive the value (rather than manipulating the camera attribute itself) we can ensure we hook in to trigger our drivers whenever the property is changed.

camera property

Note : Ensure to include the decimal part of the property if you want it to remain a floating-point number when setting the property initial value - ie, '35.00' rather than '35' - otherwise you will lose the ability to set it to fractional values.

If there are multiple camera attributes that you would want to adjust that would each affect the camera viewing angle then add multiple properties - one for each attribute. In my example I am only adjusting Focal Length, hence only one property 'focallength'.

Right-click the camera attribute (in my case Focal Length) and Add Driver.

add driver

Swap to a Graph Editor window and set up the driver so that it has a variable of type Single Property with Prop set to the object Camera and Path set to ["name_of_property"] - ie, in my case '["focallength"]. Set the Driver Type to 'Scripted Expression' and the Expression to 'var'.

driver variable

Now, varying the value of the Property 'focallength' should automatically adjust the camera's focal length.

Note : Cycles provides different sets of Custom Properties for different aspects of each object - for example, in the Camera properties there is also a Custom Properties tab. It's very tempting to use these properties rather than the main 'Object' properties - since when adjusting the camera attributes they'll be closer to hand. However, I have found that those properties do not play as nicely with drivers as the Object properties so they are generally best avoided. By all means give it a try - your version of Blender may well handle it better - but bear in mind that if the driver values are not propagating properly that that could be a cause.

Now for the lamp. Select the lamp, got to its Lamp properties and Add Driver to its Size.

lamp angle driver

Swap to the Graph Editor again and find the driver. Again, in the Variables section we need to add our Camera Property as a Single Property set to '["focallength"]'. This will ensure that the driver is triggered on change of that property.

However, in the Scripted Expression expression, instead of using the variable (var), set the expression to take the camera angle directly from the camera. This will set the lamp Size to the same angle as the camera whenever the 'focallength' property changes.

lamp angle driver details

Note : If you want to be able to alter multiple camera attributes - eg, Focal Length, Sensor Size, etc. - then simply add multiple Custom Properties, one for each. When setting up the driver on the lamp add multiple Variables - one for each property. This way, whenever any camera attribute is amended (via the property) it will also trigger the driver on the lamp to capture the camera angle.

Now adjusting the camera (via its custom properties, not the driven camera attributes - note the property at the bottom-right of the following image) should propagate the angle to the associated lamp (note how the lamp in the top-left window always matches the camera width).

adjusting focal length

Note : My particular version of Blender only supports circular lamps - the same principle applies to non-circular spot lamps.

Blend file included

  • $\begingroup$ Hi Rich, Thanks a lot, I'll try your blend and solution tomorow, It seems working as I want, I'have tested this solution but the difference I see, that you're using complete pathdata to custom propertie instead of "var" ... $\endgroup$
    – Ellt
    Feb 11 '18 at 23:00
  • $\begingroup$ I wonder if there’s an issue with the order that the two drivers are being applied - if the ‘angle’ driver is perhaps being evaluated before the focallength one. I don’t believe there is any way to indicate the order that drivers are applied - other than the order you create them, perhaps. If tr problem is specific to 2.79 then I’m afraid there’s not much else I can do as my system won’t run that version at the moment. $\endgroup$ Feb 13 '18 at 21:09
  • $\begingroup$ Hi, I have test it, I'm using the last 2.79, it seem not stable, switch internal, then it work only in cycle in render mode ...strange ... Is there a stable solution using animation node? or scripting? I don't understand well! I would like compare several method to choice the easiest, and robust one, perhaps it could be interesting for other ...Thanks for your help,...my dysfunction come perhaps from my custom ui I create... $\endgroup$
    – Ellt
    Feb 13 '18 at 21:14

Note : This answer relates to the interpretation of changing camera angle as being to change the actual camera, rather than the properties of the camera - which was not the actual intention of the question. I have left this answer in place since it may be of use to someone searching for related issues and it does describe an apparent issue in the OpenGL render.

After running some tests, it seems that there is a problem in the behaviour but I don't quite get the same results. Most notably, my render preview mode does correctly update and the actual render also appears to work correctly - the only issue I did find is in using OpenGL render where the light cone was not updated.

Here's my setup :


I've created two cameras and added markers to the timeline - one at frame 1 and another at frame 20. These markers are bound to the relevant camera so that the view starts at the first camera and changes to the other on frame 20.

I've set the Pass Index on Camera #1 to 1 and Camera #2 to 2 and the driver on the spot lamp size is set to 'bpy.context.scene.camera.pass_index /2' - since it's in radians this will be 0.5 radians (28.6 degrees) for the first camera and 1.0 radians (57.3 degrees) for the second camera.

If I render the animation I get this up to and including frame 19 :

first camera view

And this on frame 20 and beyond :

second camera view

Manually skipping through frames produces similar results in the 3d Editor window, whether in Render Preview mode, Solid or Wireframe :

wireframe animated

Where I do get strange results is if I do an OpenGL Render - in which case the lamp size wasn't being updated (note the lamp cone lines not changing their spread) :

opengl not working

However, if I moved the timeline frame to be after frame 19 then the OpenGL animation did work correctly! (note the change in spread of the light cone - only change is the 'current' frame in the timeline at the time the render is triggered) :

opengl worked

So, in summary, there does appear to be a problem - and a problem that is dependent on the start frame in the timeline at the point the OpenGL render is actioned. I don't know if the same issue could potentially occur in 'full render' - perhaps I just didn't hit the same situation. More extensive testing required.

Note that my tests were all performed on Linux using Blender 2.76b.

EDIT I repeated the test using 'full' render and the lamp size in the final render did correctly change - so this looks like the issue is restricted to OpenGL render (at least, in my case). It would be interesting if you could repeat the tests. I'll upload my Blend file so you can test with that if desired.

  • $\begingroup$ Thanks Rich for your tests and research ..., but it doesn't seem to do what I' searching , your set up seems to not update ... I thouhgt it can be a simple thing but ... its looks like harder than i was thinking... $\endgroup$
    – Ellt
    Feb 4 '18 at 12:49
  • $\begingroup$ That's weird. If I load it (need to ensure auto-running of scripts is abled - it should prompt at the top-right of the window, or enable it in User Preferences - to enable the driver (otherwise you end up with a 1 degree spot lamp)), move the mouse over the 3d View and press <kbd>Shift</kbd>+<kbd>Z</kbd> to go into render preview mode, and then press right-cursor twice to skip forward two frames (from 18 to 20), my preview is updated fine (to the image included above). I suppose the next consideration is what version of Blender are you using - could that be a difference? $\endgroup$ Feb 4 '18 at 14:00
  • $\begingroup$ Rich, no version blender problem, it work (great) as you say ... but Your solution need to configure lens camera and then with conversion passIndex (I can't add driver definitly to passIndex), so in fact for me it's like configure lens cam then angle size spotlight? I'm wrong? $\endgroup$
    – Ellt
    Feb 4 '18 at 14:25
  • $\begingroup$ Hi @Ellt. Looking at your edit I realise I didn’t fully understand your issue. I’ll try and setup your situation and try and replicate it. $\endgroup$ Feb 4 '18 at 22:40
  • $\begingroup$ Thanks, I think perhaps trought custom properties and update fonction it could be done, but I m new with coding ...sorry if I wasn't clear with my need. The idea is to see quickly in "real time" what, each camera could see in a scene ... $\endgroup$
    – Ellt
    Feb 5 '18 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.