Blender 2.25/2.26/2.27... 
Python :
Changing the active camera instantly

(version française)

    Main  Index
Previous  Python Script 
script: Mouse pointer Next
<<Matrix>> Version
Raw Version
Blender 2.27 GUI and former.

#-------------------------
# Changing the active camera during
# an animation 
# jm soler 04/06/2003

# The Matrix effect python supported
#
# 1/  Notice that the camera need to be correctly assigned
# with "ctrl-0" before launching the animation. 
# 2/  The animation loop that you obtain with "alt-a" won't relaunch 
# the script automatically.
#-------------------------
import Blender
VERSION=Blender.Get('version')
#-------------------------
# We fill the name list 
# of camera objects.
#-------------------------
c=[]
for n in range(1,26):
    c.append("Camera.%03d"%n)
print c
rythmedechangement=4
maxframe=(len(c)-1)*rythmedechangement
print maxframe
#-------------------------
# Picking up the current 
# frame number
#-------------------------
if VERSION >=228:
   frame=Blender.Get('curframe') 
else:
   frame=Blender._Blender.Get('curframe')

#-------------------------
# testing what must be done
# for the current frame 
#-------------------------
print frame%rythmedechangement, c[frame/rythmedechangement]
if frame%rythmedechangement==0:
       Blender.Scene.getCurrent().setCurrentCamera(Blender.Object.Get(c[frame/rythmedechangement]))
 


Try the Blender file connected with this page : Changer de camera
 

#-------------------------
# Changing the active camera
# during an animation (Raw version) 
# jm soler 06/2003
#-------------------------
import Blender
VERSION= Blender.Get('version')
#-------------------------
# We fill up the list with the camera
# objects names
#-------------------------
c=["Camera.001",
                 "Camera.06",
                 "Camera.012",
                 "Camera.018",
                 "Camera.024"]
#-------------------------
# Picking up the current 
# frame number
#-------------------------
if VERSION < 228 :
   frame=Blender._Blender.Get('curframe')
else:
   frame=Blender.Get('curframe')

#-------------------------
# Picking up the current scene object 
#-------------------------
  Sc=Blender.Scene.getCurrent()

#-------------------------
# testing what must be done
# for the current frame 
#-------------------------
if frame==20:
                 Sc.setCurrentCamera(Blender.Object.Get(c[1]))
elif frame==40:
                 Sc.setCurrentCamera(Blender.Object.Get(c[2]))
elif frame==80:
                 Sc.setCurrentCamera(Blender.Object.Get(c[3]))
elif frame==100:
                 Sc.setCurrentCamera(Blender.Object.Get(c[4]))
elif frame==1:
                 Sc.setCurrentCamera(Blender.Object.Get(c[0]))


 


Graphic User Interface (reserved Blender 2.27 and former)

The Creation of a GUI for a script <<linked>> need a more complex organization than the one used for the classic scripts.

Actually, It will be necessary to create two scripts : one for the GUI and the second that will linked to the scene. We can easily fancy that a script that would ask for the redraw of an interactive window at each frame would set up a few problems, at least the slow down of the redrawing.

The main difficulty will be to pass information from one script to the other because each time the script is computed, the python space for variable names is cleaned. Thus we can not keep any value in a global variable  from one script to the other. The only name space which is not updated is the one of the Blender  module itself. The trick is to hang the needed information to it.

To understand what is done here, You have to split the script. Here is the simplest expression of it :
 

import Blender
c=[ # nom  d'objet , frame
    ["Camera.001"  ,   1 ],
    ["Camera.002"  ,   10],
    ["Camera.001"  ,   20],
    ["Camera.002"  ,   50],
 ]
frame=Blender._Blender.Get('curframe')
Sc=Blender.Scene.getCurrent()
for c_ in c:
  if frame==c_[1]:
     Sc.setCurrentCamera(Blender.Object.Get(c_[0]))

On one side the data : the name of the object and the frame number.
 
 

c=[ # nom  d'objet , frame
    ["Camera.001"  ,   1 ],
    ["Camera.002"  ,   10],
    ["Camera.001"  ,   20],
    ["Camera.002"  ,   50],
 ]

On the other side the processes applied to these data.
 

import Blender
frame=Blender._Blender.Get('curframe')
Sc=Blender.Scene.getCurrent()
for c_ in c:
  if frame==c_[1]:
     Sc.setCurrentCamera(Blender.Object.Get(c_[0]))

The data must be constructed apart. That's the work done through the graphical interface. As we pointed out, a list (named c) is connected to the Blender main module ::
 

#----------------------------------------
# GUI to Change the camera during
#----------------------------------------
import Blender
...
Blender.c=[]
...

In this list, we store the object name and the frame number in the shape of a new list  made of these two exclusive elements!
 

       try: 
          name=Blender.Object.getSelected()[0].name
          Blender.c.append([name,frame.val])

Once in the Blender.c list, these data are available, for reading and writing, for all the other scripts that would be simultaneously launched (or simply after) with the GUI.
 

#-------------------------
#Changing the active camera during
# an animation : GUI version.
# Caution this file must
# be << linked >> to the scene with
# the "framechanged" option.
#
# jm soler 05/06/2003
#-------------------------
import Blender
#-------------------------
# We fill up the list with the camera 
# objects names. 
#-------------------------
try:
  c=Blender.c
except:
  pass
...
try:
  for c_ in c:

If the Graphical interface is relaunched, in its actual shape, the list is cleared out.
Nothing prevent you from adding the necessary controls for clearing processes, management and maintenance. This would be available in the 0.2 version.

 (Try the Blender file connected to these two scripts: The GUI : for Changing camera )

First part:
 

#-----------------------------
# GUI to change the active camera
# during an animation .
# jm soler 05/06/2003
#------------------------------
# Before you start, check
# that the "cancam4gui.py" script is really 
# linked to the scene, with the 
# "frameChanged" option. Then do 
# the following: 
#
# 1/ Launch the GUI  : "alt-p".
# 2/ select an object.
# 3/ push on view to 
#    display the name.
# 4/ change the frame value 
#    with the slider.
# 5/ push the "select object"
#    button to record.

# In the 3D window, go to 
# camera view and test the result
# with the key combination : "alt-a".
#------------------------------
import Blender
from Blender import Object
from Blender.Object import *
from Blender.Draw import *
from Blender.BGL import *

Blender.c=[]

objet_camera=Create(0)
frame=Create(0)
name=""
def draw():
    global objet_camera,frame,name
    glClear(GL_COLOR_BUFFER_BIT)
    glRasterPos2f(20, 210)
    Text("Python script for Changing the active Camera")
    glRasterPos2f(20, 190)
    Text("Jean-michel Soler, juin 2003")
    objet_camera=Button("Select objet : "+name, 3, 20, 160, 200, 18)
    objet_camera=Button("view", 2, 220, 160, 60, 18)

    frame=Slider("frame : ", 2, 20, 140, 240, 18, frame.val, 1, 256)

    #Exit button
    Button("Exit", 1, 40, 10, 80, 19)

def event(evt, val): 
    if (evt== QKEY and not val): Exit()

def bevent(evt):
    global objet_camera,objet_camera, name
    if   (evt== 1):
        Exit()
    if   (evt== 2):
          name=Blender.Object.getSelected()[0].name
          Blender.Redraw() 
    elif (evt== 3):
        try: 
          name=Blender.Object.getSelected()[0].name
          Blender.c.append([name,frame.val])
          Blender.Redraw() 
        except:
          pass 
    elif (evt==5):
        Blender.Redraw()

Register(draw, event, bevent)

Second part:

#-------------------------
# Changing active camera during
# an animation : GUI version. 
# Caution, this file must 
# be << linked >> to the scene with
# "framechanged" option .
#
# jm soler 05/06/2003
#-------------------------
import Blender
#-------------------------
# We fill up the list with the 
# camera objects names
#-------------------------
try:
  c=Blender.c
except:
  pass
#-------------------------
# Picking up the current frame 
# number.
#-------------------------
frame=Blender._Blender.Get('curframe')
#-------------------------
# Picking up the object : current scene.
#-------------------------
Sc=Blender.Scene.getCurrent()
#-------------------------
# testing what must be done
# for the current frame. 
#-------------------------
try:
  for c_ in c:
     print c_ 
     if frame==c_[1]:
        Sc.setCurrentCamera(Blender.Object.Get(c_[0]))
except:
 pass

GUI V 0.2


Previous :  Python Script 
 script : mouse pointer Next
To the  Top of the page

Questions related to this page can be asked on  :
 news://news.zoo-logique.org/3D.Blender


 

 

Les questions concernant cette page  peuvent être posées sur  :
news://news.zoo-logique.org/3D.Blender

Livre en français
Blender : apprenez, pratiquez, Créez, livre, Ed. Campus Press, coll. Starter Kit
Blender Starter Kit

Forum
FAQ
Lexique
Didacticiels
Compilations
Blender2KT
Débuter
Modelage
Blender python
Materiaux
Lumière
Animation
API python (eng)
Archives nzn
Statistiques
Doc flash Sculptris
Galerie Sculptris

mon site de démos sur youtube