cPickle EDIT: and setting vertex XYZ

Hi everyone,

I have a script which sets XYZ positions of a 5x5 grid according to OSC data being fed from max/msp. this runs fine in the GameEngine but i want to export the meshes i make in the engine into Blender so ican then export as .stls and make them on a lathe. ok so that was the background!

ok so the grid data is stored in 5lists which correspond to the 5 rows, each list has 5 sublists of each vertex’s XYZ.

ive figured out how to export these to a text file with cPickle, using the following code.



import cPickle as pickle


if own.dump==1:
	
#	file = open("4.txt","w")
#	ta_x = (str(time_a_xyz)+ "
")
#	tb_x = (str(time_b_xyz)+ "
")
#	tc_x = (str(time_c_xyz)+ "
")
#	td_x = (str(time_d_xyz)+ "
")
#	te_x = (str(time_e_xyz)+ "
")
	
#	file.write("time_a_xyz")
#	file.write(ta_x)
#	file.write("time_b_xyz")
##	file.write(tb_x)	
#	file.write("time_c_xyz")
#	file.write(tc_x)
#	file.write("time_d_xyz")
#	file.write(td_x)
#	file.write("time_e_xyz")
#	file.write(te_x)
	
	pickle.dump(time_a_xyz, file)
	pickle.dump(time_b_xyz, file)
	pickle.dump(time_c_xyz, file)
	pickle.dump(time_d_xyz, file)
	pickle.dump(time_e_xyz, file)
	
	file.close()
	own.dump=0

now that wites fine to the text file but im having trouble reading it back in “normal” Blender… using:



file = open("4.txt","r")

	
time_a_xyz = pickle.load(time_a_xyz, file)
time_b_xyz = pickle.load(time_b_xyz, file)
time_c_xyz = pickle.load(time_c_xyz, file)
time_d_xyz = pickle.load(time_d_xyz, file)
time_e_xyz = pickle.load(time_e_xyz, file)
	
file.close()


i get the error: name time_a_xyz is not defined…

i figure maybe i have to get cPickle to move to the next line(??) each before setting the next variable…

any help would be very much appreciated(otherwise i guess its writing a seperate .txt file for each variable)

#########################################

EDIT:

ok so ive got data in using cPickle but cant figure out how to set individual vertexes according to variables…

heres the code im playing about with, as you can see its late :smiley: if only the GE api and the Blender API were a littlel similar :smiley:


import Blender
from Blender import NMesh, Material
import cPickle as pickle




#### get vertex lists ####

file = open("7.txt","r")

fullfile = pickle.load(file)

file.close()

time_a_xyz  = fullfile[0]
time_b_xyz  = fullfile[1]
time_c_xyz  = fullfile[2]
time_d_xyz  = fullfile[3]
time_e_xyz  = fullfile[4]



	
me = NMesh.GetRaw("Grid")       # get the mesh data called "Plane"

if not me.materials:             # if there are no materials ...
   newmat = Material.New()        # create one ...
   me.materials.append(newmat)    # and append it to the mesh's list of mats

print me.materials               # print the list of materials
mat = me.materials[0]            # grab the first material in the list

for v in me.verts:               # loop the list of vertices
  #v.co[0] *= 2.5                 # multiply the coordinates CO IS COORDINATES
  #v.co[1] *= 5.0
  #v.co[2] *= 2.5
	#print v,v.co,v.index
	#print me.verts[1].co
	#me.verts[0].co = time_a_xyz[0]
	v.co[0] = time_a_xyz[0][0]
	v.co[1] = time_a_xyz[0][1]
	v.co[2] = time_a_xyz[0][2]
	#v.co[3] = time_b_xyz[0][0]
	#v.co[2] = time_a_xyz[2]
	#v.co[3] = time_a_xyz[3]
	#v.co[4] = time_a_xyz[4]
	me.update()
#me.verts[1].co = time_a_xyz[0]
print time_a_xyz[1]


any ideas? cheers

will

use text file instead, easiest and you can modify the content with a simple text processor .

thnaks jms, ill give that a try but pickle is working ok for me now, its just setting the xyz of the verts that i cant figure?

will

Also easiest to import in an another soft .

ok so this is the code i used in the end to set the co-ords: [ if anyones interested]


#### get grid(~) ####
	
me = NMesh.GetRaw("Grid.001")       # get the mesh data called "Plane"

if not me.materials:             # if there are no materials ...
   newmat = Material.New()        # create one ...
   me.materials.append(newmat)    # and append it to the mesh's list of mats

##### gayboy giorgio code #####

me.verts[0].co[0] = time_a_xyz[0][0]
me.verts[0].co[1] = time_a_xyz[0][1]
me.verts[0].co[2] = time_a_xyz[0][2]

me.verts[1].co[0] = time_a_xyz[1][0]
me.verts[1].co[1] = time_a_xyz[1][1]
me.verts[1].co[2] = time_a_xyz[1][2]

me.verts[2].co[0] = time_a_xyz[2][0]
me.verts[2].co[1] = time_a_xyz[2][1]
me.verts[2].co[2] = time_a_xyz[2][2]

me.verts[3].co[0] = time_a_xyz[3][0]
me.verts[3].co[1] = time_a_xyz[3][1]
me.verts[3].co[2] = time_a_xyz[3][2]

me.verts[4].co[0] = time_a_xyz[4][0]
me.verts[4].co[1] = time_a_xyz[4][1]
me.verts[4].co[2] = time_a_xyz[4][2]
###
me.verts[9].co[0] = time_b_xyz[0][0]
me.verts[9].co[1] = time_b_xyz[0][1]
me.verts[9].co[2] = time_b_xyz[0][2]

me.verts[8].co[0] = time_b_xyz[1][0]
me.verts[8].co[1] = time_b_xyz[1][1]
me.verts[8].co[2] = time_b_xyz[1][2]

me.verts[7].co[0] = time_b_xyz[2][0]
me.verts[7].co[1] = time_b_xyz[2][1]
me.verts[7].co[2] = time_b_xyz[2][2]

me.verts[6].co[0] = time_b_xyz[3][0]
me.verts[6].co[1] = time_b_xyz[3][1]
me.verts[6].co[2] = time_b_xyz[3][2]

me.verts[5].co[0] = time_b_xyz[4][0]
me.verts[5].co[1] = time_b_xyz[4][1]
me.verts[5].co[2] = time_b_xyz[4][2]
###
me.verts[10].co[0] = time_c_xyz[0][0]
me.verts[10].co[1] = time_c_xyz[0][1]
me.verts[10].co[2] = time_c_xyz[0][2]

me.verts[11].co[0] = time_c_xyz[1][0]
me.verts[11].co[1] = time_c_xyz[1][1]
me.verts[11].co[2] = time_c_xyz[1][2]

me.verts[12].co[0] = time_c_xyz[2][0]
me.verts[12].co[1] = time_c_xyz[2][1]
me.verts[12].co[2] = time_c_xyz[2][2]

me.verts[13].co[0] = time_c_xyz[3][0]
me.verts[13].co[1] = time_c_xyz[3][1]
me.verts[13].co[2] = time_c_xyz[3][2]

me.verts[14].co[0] = time_c_xyz[4][0]
me.verts[14].co[1] = time_c_xyz[4][1]
me.verts[14].co[2] = time_c_xyz[4][2]
###
me.verts[19].co[0] = time_d_xyz[0][0]
me.verts[19].co[1] = time_d_xyz[0][1]
me.verts[19].co[2] = time_d_xyz[0][2]

me.verts[18].co[0] = time_d_xyz[1][0]
me.verts[18].co[1] = time_d_xyz[1][1]
me.verts[18].co[2] = time_d_xyz[1][2]

me.verts[17].co[0] = time_d_xyz[2][0]
me.verts[17].co[1] = time_d_xyz[2][1]
me.verts[17].co[2] = time_d_xyz[2][2]

me.verts[16].co[0] = time_d_xyz[3][0]
me.verts[16].co[1] = time_d_xyz[3][1]
me.verts[16].co[2] = time_d_xyz[3][2]

me.verts[15].co[0] = time_d_xyz[4][0]
me.verts[15].co[1] = time_d_xyz[4][1]
me.verts[15].co[2] = time_d_xyz[4][2]


doesnt seem that the .co will accept a list fot X,Y and Z. you have to set each one seperately. I guess i could use a few range functions in their to save on typing but hey it works. :smiley:

Shortest :

  
me = NMesh.GetRaw("Grid.001")      
listlen=16
for i in range(listlen):
     for j in [0,1,2] :
          me.verts[i].co[j] = time_a_xyz[i][j]   

Mesh object has a default material thus there is no need to add a new material in blender data base .

Cheers jms,
ill give that a try although it might not be quite that simple vertice indexes go back and forth row to row and i have 5 lists of the x,y,z i am setting them to, i’ll have a play with it see what i can do.
Thanks a lot though, its a bit of a mission figuring out blender python when youre used to GE python.

Woll