Add a camera and a spotlight to a comp

Moderator: Paul Tuersley

Post Reply
Posts: 1
Joined: June 9th, 2004, 4:55 am

This script gives you a quick head start in working with 3d elements (although it doesn't toggle the 3d setting on layers.) As in many scripts the comp you want to affect must be selected in the project window.

What this script will do is add a Camera and a Light to a selected Composition. It will also set their position and point of interest to defined values.
Even though this script doesen't have any specific use (unless you actually wanted to use these exact values!), it is a good example of the syntax and concepts you need to do this type of thing.

//This part of the script checks if you have selected a comp in the Project Window.
//If you haven't it will return an error message.
//Check page 123 of the Scripting Guide for app.project.activeItem

var activeItem = app.project.activeItem;
if (activeItem == null || !(activeItem instanceof CompItem)){
alert("Please establish a comp as the active item and run the script again");
} else {

//This part of the script creates a light and a camera.
//It calls the light "Spotlight 001" and places its "center point" (anchor point) at 0,0.
//Check page 110 to 112 of the scripting guide for addLight().
//Same thing for the camera.
//What has to be noticed here in the setting of the center point is that we have a floating value.
//Which means that you can use decimal values to place your items.
//You can thus be very precise in your positioning.

var newLight = activeItem.layers.addLight("Spotlight 001",[0,0]);
var newCamera = activeItem.layers.addCamera("3d camera 001",[360.0,243.0]);

//This part of the script will now set the position and anchor point for the light and the camera using the setValue method.
//For 3D items, you obviously need an array of three values. So here we are setting the position of the Camera for X at 360, for Y at 243 and for Z at -900, etc...
//The final line of this paragraph turns the light layer's "Cast Shadow" property to True, which means, well, on."position").setValue([ 360, 243, -900]);"anchorPoint").setValue([ 360, 243, 0]);"anchorPoint").setValue([ 480, 175, -140]);"position").setValue([ 680, 120, -360]);


This script is a good starting poin to understand several things about creating layers with scripting.
The first is that unless you want to use the first item in a Project Window, you need to make sure that the comp you will be working on is selected.
The activeItem attribute is what you need to understand at this point.

The second is that to create lights and cameras, or any other object, you need to use activeItem.layers.addLight (or addCamera/addText,...)
addLight() and addCamera() require a name and a center point that has to be set to a floating point array (thus using a decimal value, even if it is 0, as in the case of this example.)

The third thing is that once you have done this, you can set their position, anchor point, and other things.

Finally, you can access a layer's property and modify it. This script turned on a Light's ability to cast shadows, but you could also work on a camera's Depth of field, Zoom, Aperture, ....

NOTE : You may notice something weird about this script. The center point that is created in the first part of the script only has two dimensions. This is a current limitation of scripting that hopefully will soon be fixed. If that wasn't the case, you could clearly bypass the part of the script where you have to set its position in 3D
Post Reply