[python script] autocreate path from multiple cams

Hi all, i’m really new to Blender and python so excuse me if my question seems so noob :slight_smile:

I’d like to create an auto path (poly) from still camera positions that will be then constrainted to a object. I red a lot from the Blender’s wiki to get functions but i’m missing something and some help will be really appreciated. Here’s my first script i wrote. And of course i doesn’t work :slight_smile: But i don’t understant why. Included a snapshot of my pb.


import bpy
from mathutils import Vector

scene = bpy.context.scene

weight for curve

w = 1

for ob in scene.objects:
if ob.type == ‘CAMERA’:
bpy.context.scene.camera = ob

    # declare & print vectors (location) of current cam
    cList = [ob.location]
    # print (cList)
   
    curvedata = bpy.data.curves.new(name='Curve', type='CURVE')  
    curvedata.dimensions = '3D'  
    
    objectdata = bpy.data.objects.new("ObjCurve", curvedata)  
    objectdata.location = bpy.data.objects['Camera'].location # origin is first cam location
    bpy.context.scene.objects.link(objectdata)  
    
    polyline = curvedata.splines.new('POLY')  
    polyline.points.add(len(cList)-1)  
    
    for num in range(len(cList)):  
        x, y, z = cList[num]  
        polyline.points[num].co = (x, y, z, w)

Hi pyp22,

You must paste your code with the advanced button “GO ADVANCED” and use # button, please!
If not I can’t use your script because the indentation disappear!

Hi Spirou, thanks for getting back to me. I work a bit more and detected a pb whith the coords format i was using. I almost managed to get what i’d like to get, but still meeting some difficulties with the locations. I’d like to make start poly at first cam location towards the cam + 1 location. Here is what i’ve done, but still looking how to handle the cams.loc +1 :

import bpy
from mathutils import Vector

select scene

scene = bpy.context.scene

w = 0.2 # weight

number of cameras in scene

ob_list = bpy.data.cameras.items()
total_cams = len(ob_list)
print (“total number of cams:”, total_cams) # check

Poly

type of curve

curvedata = bpy.data.curves.new(name=‘Curve’, type=‘CURVE’)
curvedata.dimensions = ‘3D’

set origin of future curve to first cam location

origin = bpy.data.objects[‘Viewpoint.001’].location

first cam item number

i = total_cams / total_cams

ob_cam_list_position

for objcams in scene.objects:
if objcams.type == ‘CAMERA’:
bpy.context.scene.camera = objcams
x, y, z = objcams.location
# print (x, y, z) # check
i = int(i + 1)
#print (i) # check
# Poly
# link to scene
objectdata = bpy.data.objects.new(“ObjCurve”, curvedata)
objectdata.location = (x, y, z) #object origin is current cam
bpy.context.scene.objects.link(objectdata)
# add & draw
polyline = curvedata.splines.new(‘POLY’)
#print (i - 1)
polyline.points.add(i)
polyline.points[i].co = (x, y, z, w)

Thanks for helping, regards,

Pierre

Pierre tu ne comprends pas ce que je te demande:
Colle ton code python avec le bouton “code” -> # en écrivant ton message avec le bouton “GO ADVANCED”, stp

car il n’y a plus les indentation dans ce que tu fais actuellement, stp!!! Ton message est inutile!

import bpy  
from mathutils import Vector  

# select scene
scene = bpy.context.scene

w = 0.2 # weight 

# number of cameras in scene
ob_list = bpy.data.cameras.items()
total_cams = len(ob_list)
print ("total number of cams:", total_cams) # check

# Poly
# type of curve
curvedata = bpy.data.curves.new(name='Curve', type='CURVE')
curvedata.dimensions = '3D'

# set origin of future curve to first cam location
origin = bpy.data.objects['Viewpoint.001'].location    

# first cam item number
i = total_cams / total_cams

# ob_cam_list_position
for objcams in scene.objects:
    if objcams.type == 'CAMERA':
        bpy.context.scene.camera = objcams
        x, y, z = objcams.location
        # print (x, y, z) # check
        i = int(i + 1)
        #print (i) # check
        # Poly
        # link to scene
        objectdata = bpy.data.objects.new("ObjCurve", curvedata)
        objectdata.location = (x, y, z) #object origin is current cam
        bpy.context.scene.objects.link(objectdata)
        # add & draw
        polyline = curvedata.splines.new('POLY')
        #print (i - 1)
        polyline.points.add(i)
        polyline.points[i].co = (x, y, z, w)
        

Ok Thanks Pierre! I try it…
At soon.

thanks :slight_smile: still working on it. Best way to learn :slight_smile: i’m sure this can be easely done and i might be missing a single obscure fonction :slight_smile: