Problem Insert Keyframe and move objects

:evilgrin:
Hi there!!!
I have this simple problem. I have 3 sphere and I want to create and animate. I have problems to do that.

import bpy


vertici = [(0,0,0),(0,1,0),(1,1,1)]
for i in range(0,len(vertici)):
    bpy.ops.mesh.primitive_uv_sphere_add(segments=32,size=0.3,location=(vertici[i]))
    bpy.ops.object.shade_smooth()





movimenti = [(0,0,2),(0,1,3),(1,1,2)]
for i in  movimenti:
    #orig_frame =bpy.context.scene.frame_current
    
    for ob in bpy.data.objects:
        portos = ob.name
        bpy.ops.object.select_name(name = "portos")
        
        bpy.context.scene.frame_set(+10)
        bpy.context.active_object.location = i
        
        bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)

#error Traceback (most recent call last):
  File "/Text", line 24, in <module>
AttributeError: 'NoneType' object has no attribute 'location'


I want to move my object to keyframe 10 and move the position of the object and insert a keyframe.
Thanks in advance for any help

i too would like to know this, here is the same script corrected and modified to be more concise:


import bpy


vertici = [(0,0,0),(0,1,0),(1,1,1)]
for i in vertici:
    bpy.ops.mesh.primitive_uv_sphere_add(segments=32,size=0.3,location=i)
    bpy.ops.object.shade_smooth()


movimenti = [(0,0,2),(0,1,3),(1,1,2)]
for i in movimenti:
    #orig_frame =bpy.context.scene.frame_current
    
    for ob in bpy.data.objects:
        portos = ob.name
        bpy.ops.object.select_name(name = portos)
        
        bpy.context.scene.frame_set(+10)
        bpy.context.active_object.location = i
        
        bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)

maybe you can figure out the rest!

here’s a slightly simplified version, which places keyframes for one sphere. start simple :slight_smile:


import bpy

start_pos = (0,0,0)
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, size=0.3, location=start_pos)
bpy.ops.object.shade_smooth()

# if you have more objects, you may want to select them by name
# objname = bpy.context.active_object.name 

# no [] required for simple iteration
positions = (0,0,2),(0,1,2),(3,2,1),(3,4,1),(1,2,1)
frame_num = 0
for position in positions:
    bpy.context.scene.frame_set(frame_num)
    bpy.context.active_object.location = position
    bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
    frame_num += 10
    

Thanks for your suggestion…
So I’m in this stage… I have a list with all my coordinate and I want to change one by one the position of my object… but I have problems on the select the object…someone have a suggestions?

import string
obl = bpy.data.objects
for i in obl:
    if i.type == 'MESH':
        for ro in gico:
            i.location = ro
            bpy.context.scene.frame_set(frame_num)
            bpy.context.active_object.location = ro
            bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
            frame_num += 10
    else:
        pass

gico is a list with all my coordinates of the objects!!
thanks in advance for any help!

hi jarod81

i would suggest that you post code that is more complete
the variables gico, frame_num are not initialized

so post something we can run
and deal with exceptions that matter

regards
sc3*2

ok… So here you can found the file
http://www.pasteall.org/22060
and this is my script. the problems is the activation of object and move for create a simple animation


import bpy


filename="/home/ppa.txt"

f = open(filename,'r')
l = [i[:-1].strip() for i in f.readlines()]
#l=[]
#for i in f.readlines():
#    print(i[:-1].strip())
#    l.append( i[:-1].strip())
d ={}
tippe =[] #list name elements
dic = {} # dictionary of data and coordinate
for i in l:  
    key = i[0:3]  
    name_v = i[5:7]
    ax = float(i[9:13]) 
    ay = float(i[15:20]) 
    az = float(i[21:25])
    a_coord = (ax,  ay, az)
    dic[name_v] = a_coord
    d[key] = dic
    
    
noto = d.keys()
nomi = []
for i in noto:
    nomi.append(i)
    
model1 = nomi[0]
dati =  []
valse = d[model1]
for t in valse.values():
    dati.append(t)

altri_dat= []

for i in nomi:
    if i == model1:
        pass
    else:
        print(i)
        altri_dat.append(d[i].values())
gico = altri_dat[0] 
sce = bpy.context.scene        
frame_num = 0
for i in range(len(dati)):
    bpy.ops.mesh.primitive_uv_sphere_add(size=0.3,location=(dati[i]))
    bpy.ops.object.shade_smooth()
    mat = bpy.data.materials.new('ss')
    mat.diffuse_color = (0,i,0.8)
    bpy.ops.object.material_slot_add()
    bpy.context.object.material_slots[bpy.context.object.material_slots.__len__() - 1].material = mat
    poppo = bpy.context.active_object
    poppo.name = 'Picco'
    #selects the object(obj_name)
    sce.objects.active = poppo
    bpy.context.scene.frame_set(frame_num)
    
obl = bpy.data.objects

for i in obl:
    if i.type == 'MESH':
        print(i.name)
        for ro in gico:
            i.location = ro
            bpy.context.scene.frame_set(frame_num)
            bpy.context.active_object.location = ro
            bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
            frame_num += 10
    else:
        pass
 



thanks for any help!!!:eek:

I understand your error. It means that the bpy module is not recognizing your variables…I prefer to use:

Blender.Object.Get("…")

that is blender 2.49 api, jarod81 seems to be using blender 2.5 api
bpy.ops.object.select_name so something like bpy.ops.object.select_name(name=“Cube”)



for i in obl:
    if i.type == 'MESH':
        print(i.name)
        bpy.ops.object.select_name(name=i.name)
        frame_num = 0
        for ro in gico:
            bpy.context.active_object.location = ro
            bpy.context.scene.frame_set(frame_num)
            bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
            frame_num += 10
    else:
        pass

should probably work.

you don’t need the else: pass unless you have plans for objects that aren’t of type MESH

thanks for this suggestions…
The script now move all the data in one coordinate. However after press alt+a the element doesn’t move…

well, let’s generalize the problem, you started too complicated before knowing how it works.
make a scene with

  • 3 objects (“sphere01”,“sphere02”,“sphere03”)
    then create 3 coordinate lists and a multidimensional list out of them
  • 10 unique coordinates each. (1 column, 3 rows, each row is a list of 10 coordinates)
multi_list = [coordinate_list1,coordinate_list2,........,coordinate_list3]
import bpy
data1 = [(0,0,0),(1,1,1),(0,2,3)]

for i in data1:
    bpy.ops.mesh.primitive_uv_sphere_add(size=0.3,location= i)
    
#coordinate of object


position = [[(0,1,1),(0,2,1),(0,3,1),(0,1,4),(0,2,5), (1,3,1),(1,2,1),(-0.1,3,1),(-0.5,1,4),(-3,2,5) ], [(9,10,10),(6,22,10),(0,3,1),(0,1,4),(0,9,5), (18,3,1),(12,2,1),(-21,23,1),(-0.5,1,14),(-3,12,5) ],[(5,1,10),(0,20,1),(0,3,12),(0,12,4),(0,-2,5), (-1,3,-11),(1,-2,1),(-8,1,-1),(-0.5,1,-4),(-3,-2,5) ]]

obl = bpy.data.objects
for i in obl:
    if i.type == 'MESH':
        print(i.name)
        bpy.ops.object.select_name(name=i.name)
        frame_num = 0
        
        for ro in position:
            for  t in ro:
                print(t)
           
            bpy.context.active_object.location = t
            bpy.context.scene.frame_set(frame_num)
            bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
            frame_num += 10

:frowning:

ok



import bpy
data1 = [(0,0,0),(1,1,1),(0,2,3)]

for i in data1:
    bpy.ops.mesh.primitive_uv_sphere_add(size=0.3,location= i)
    
#coordinate of object
position_list = [[(0,1,1),(0,2,1),(0,3,1),(0,1,4),(0,2,5)],
            [(1,3,1),(1,2,1),(-0.1,3,1),(-0.5,1,4),(-3,2,5)],
            [(9,10,10),(6,22,10),(0,3,1),(0,1,4),(0,9,5)]
            ]

obl = bpy.data.objects
mesh_objects = []
for i in obl:
    if i.type == 'MESH':
        mesh_objects.append(i.name)

# print(mesh_objects)

iter = 0
for obj in mesh_objects:
    bpy.ops.object.select_name(name=obj)
    
    frame_num = 0
    # print(obj)
    for coordinates in position_list[iter]:
        # print(coordinates)
        bpy.context.scene.frame_set(frame_num)        
        bpy.context.active_object.location = coordinates
        bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
        frame_num += 10
    
    iter+=1

you should gain more experience using lists and multi-dimensional lists :slight_smile:
and i should read better :slight_smile:

there are even more pythonic ways to do this, but for now it makes sense to do it like this… i think

:spin: thanks…

Now there is only the problem to trasform all the coordinate in a list of list

intuition tells me the latter is faster.


import bpy

data_directory = 'C:\	emo/' # windows
data_file = 'coords.txt'

data = open(data_directory+data_file, 'r')

uncat_list = []
for i in data:
    temp_list = i.split("	")
    uncat_list.append(temp_list)
    # print(i)

data.close() # to be safe.

for element in uncat_list:
    print(element)

then run that through the Sorting code, might look something like this :


import bpy

data_directory = 'C:\	emo/' # windows
data_file = 'coords.txt'

data = open(data_directory+data_file, 'r')

#### READING ##########################
uncat_list = []
for i in data:
    temp_list = i.split("	")
    uncat_list.append(temp_list)
    # print(i)
   
data.close() # to be safe.

#### SORTING ##########################
multi_list, temp_list = [],[]

# get name of first object, this is a one off necessity
object_num = uncat_list[0][0]

# start digging through the data
for i in range(len(uncat_list)):
    object_num_line = uncat_list[i][0]
    if not object_num_line == object_num:
        multi_list.append(temp_list) #we aren't adding more items for this track
        temp_list = [] # empty the storage list
        
    temp_list.append(uncat_list[i])
    object_num = object_num_line

    # if last line, then add templist to multilist
    if i == len(uncat_list)-1:
        multi_list.append(temp_list) #we aren't adding more items for this track

#### PROOF ############################
# you must convert these yourself to type float.        
for track in multi_list:
    print(track[0][0])
    for ref in range(len(track)):
        xfloat = float(track[ref][2])
        yfloat = float(track[ref][3])
        zfloat = float(track[ref][4])
        print(xfloat,yfloat,zfloat)

:eek:
thanks for this precious help…
I was wondering why when increase the number of objects the scripts fail…:spin:

import bpy

#in this list I have all my atom coordinate of single object...             
position =[[(-0.63, 15.929, 4.741), (0.676, 15.268, 4.992), (0.636, 13.793, 4.605), (0.347, 12.931, 5.435), (0.994, 15.42, 6.476), (-0.053, 16.352, 6.982), (-1.235, 16.208, 6.055), (1.436, 15.769, 4.411), (0.937, 14.456, 6.96), (1.983, 15.834, 6.596)]]
conf =[(-0.244, 12.116, 8.079), (0.442, 11.383, 6.981), (0.548, 12.23, 5.713), (-0.289, 13.095, 5.459), (-0.357, 10.112, 6.707), (-1.582, 10.257, 7.545), (-1.213, 11.182, 8.679), (1.434, 11.115, 7.311), (-0.599, 10.051, 5.655), (0.221, 9.248, 7.0)]


frame_num = 0
for i in conf:
    bpy.ops.mesh.primitive_uv_sphere_add(size=0.6,location=i)
    bpy.context.scene.frame_set(frame_num)
    bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
    obj_name = bpy.context.object


obl = bpy.data.objects
mesh_objects = []
for i in obl:
    if i.type == 'MESH':
        mesh_objects.append(i.name)
item = 0
for obj in mesh_objects:
    bpy.ops.object.select_name(name=obj)
    frame_num = 10
    
    for ro in position[0]:
        print(ro)
        bpy.context.active_object.location = ro
        bpy.context.scene.frame_set(frame_num)
        bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
        frame_num += 10

I have tryesìd also iter but gave me a error. IN this case I have a multidimensional list where each element of the list are object (10 objects)…
thanks for your suggestions!!!

so is it working for you now? because if your last post was a question, i’m not sure what it was.

Unfortunately, seems doesn’t work if you have more than 3 object. Last code I posted I have create a multi list of all my coordinates. I have 10 objects but if you try to run doesn’t work. thanks for the patience

then give the full data file. ‘It doesn’t work’ isn’t a useful statement.

I Posted all data in the posted above. If you try that you can appreciate the fact there is no movement of the objects.
Another information in conf there is the 10 object with first coordinate in the position there is the location I want to move all the objects.

your data is identical for the 3 objects, on mo3 it even repeats. i can’t help you.