exporting to a python Add script

Hi,

I want to make some python scripts that will show up in the add > mesh menu. I’ve made a few by hand and they work ok, but it’s a bit slow. What I’m trying to do know is make a script that generates an add script. The idea is that I’ll have a mesh in blender, export it to a python script and then that python script will appear as an option in the add > mesh menu.

Currently I’m just trying to make a script that will export a list of vertices, then the face info. This is what i’ve got so far…

http://furver.se/~hyp3rfocus/scripts/python_export/python_export_00001.blend

the vert stuff is exported ok, like so…

    for v in me.verts:
        x=v.co[0]
        y=v.co[1]
        z=v.co[2]
        print x, y, z

…but i don’t know how to do the same for faces. currently I’ve got this…

    for f in me.faces:
        print f

…which gives me this output…

[MFace (0 1 2 3) 0]

… i guess i could take that and then use string stuff to dig out the 4 numbers, but i’m hoping there is something equivalent to the x=v.co[0] stuff i used for the verts.

Anyone know what this might be? Also, can anyone point me in the direction of suitable tutorials for this kind of stuff?

Thanks.

http://www.opendimension.org/blender_en/pymesh.php
two scripts exist, the main one doesnt work in latest versions. and another which I have. I can up if you want.

mrjynx - yes, that script you linked to is an add script, but i’m not trying to write an add script, i’m trying to write an export script that makes add scripts.

what i’m trying to do is pull all the vert and face data out of a mesh i have in blender. once i can do that i can make my export script generate a text file that would be something like add_thing.py. which would itself be an add script.

Oh… okay… I hve this one, that creates python scripts of objects, if thats not what you mean, then I dont know >.<

#!BPY
“”"
Name: ‘Script Generator Alpha’
Blender: 248
Group: ‘Object’
Tip: ‘Convert mesh object to python script’
“”"
#Santa-licensed ! Merry Christmas to benevolent Blenderheads !
import bpy
import Blender
from Blender import *
import Blender
import os
SAVE=True #Set to False if you don’t want to save the new script
NL=os.linesep
sce=bpy.data.scenes.active
ob_act=sce.objects.active
nom_obj=ob_act.name
txt=Text.New(nom_obj+".py")
#WRITES THE BLENDER STANDARD HEADER TO SCRIPT
HEADER = “#!BPY”+NL
+""“3+NL
+“Name: '”+nom_obj+"’"+NL
+“Blender : 248”+NL
+“Group: ‘AddMesh’”+NL
+“Tooltip : add a new '”+nom_obj+"’ mesh"+NL
+""“3+2NL
+“import Blender”+NL
+“import bpy”+NL
+“from Blender import "+2NL
txt.write(HEADER)
#WRITES THE DATA INITIALIZATION TO SCRIPT
txt.write(‘scn=bpy.data.scenes.active’+NL)
txt.write(“me=bpy.data.meshes.new(’”+nom_obj+”’)”+NL)
txt.write(“ob=scn.objects.new(me,me.name)”+NL
2)
#COPIES THE MESH DATA TO NEW SCRIPT (ONLY FACES & VERTICES)
me=ob_act.getData(mesh=True)
#List of vertices
txt.write(‘vert_list = [’+NL)
for i in me.verts:
txt.write(” “+str(list(i.co))+”,"+NL)
txt.write(" ]"+2NL)
#list of faces
facesList=[]
for i in me.faces:
j=[k.index for k in i]
facesList.append(j)
txt.write(‘faces_list = [’+NL)
for i in facesList:
txt.write(" “+str(i)+”,"+NL)
txt.write(" ]"+2
NL)
#commands
txt.write(“me.verts.extend(vert_list)”+NL)
txt.write(“me.faces.extend(faces_list)”+NL)
#SAVES NEW SCRIPT TO SCRIPTS DIRECTORY
if SAVE :
scr_directory=Get(“scriptsdir”)
scr_name=scr_directory+os.sep+txt.name
content=txt.asLines()
f=open(scr_name,‘w’)
for i in content:
f.write(i+NL)
f.close()

hope that works.

it exports them to the application data/blender/scripts directory.

cool. looks very interesting. :slight_smile:

any chance you could put the script in code tags? it looks like all the tabs have been stripped out.

I dont know how to do that >.O care to tell me?

 #!BPY
"""
Name: 'Script Generator Alpha'
Blender: 248
Group: 'Object'
Tip: 'Convert mesh object to python script'
"""
#Santa-licensed ! Merry Christmas to benevolent Blenderheads !
import bpy
import Blender
from Blender import *
import Blender
import os
SAVE=True #Set to False if you don't want to save the new script
NL=os.linesep
sce=bpy.data.scenes.active
ob_act=sce.objects.active
nom_obj=ob_act.name
txt=Text.New(nom_obj+".py")
#WRITES THE BLENDER STANDARD HEADER TO SCRIPT
HEADER = "#!BPY"+NL\
+"\""*3+NL\
+"Name: '"+nom_obj+"'"+NL\
+"Blender : 248"+NL\
+"Group: 'AddMesh'"+NL\
+"Tooltip : add a new '"+nom_obj+"' mesh"+NL\
+"\""*3+2*NL\
+"import Blender"+NL\
+"import bpy"+NL\
+"from Blender import *"+2*NL
txt.write(HEADER)
#WRITES THE DATA INITIALIZATION TO SCRIPT
txt.write('scn=bpy.data.scenes.active'+NL)
txt.write("me=bpy.data.meshes.new('"+nom_obj+"')"+NL)
txt.write("ob=scn.objects.new(me,me.name)"+NL*2)
#COPIES THE MESH DATA TO NEW SCRIPT (ONLY FACES & VERTICES)
me=ob_act.getData(mesh=True)
#List of vertices
txt.write('vert_list = ['+NL)
for i in me.verts:
    txt.write("	"+str(list(i.co))+","+NL)
txt.write("	]"+2*NL)
#list of faces
facesList=[]
for i in me.faces:
    j=[k.index for k in i]
    facesList.append(j)
txt.write('faces_list = ['+NL)        
for i in facesList:
    txt.write("	"+str(i)+","+NL)
txt.write("	]"+2*NL)
#commands
txt.write("me.verts.extend(vert_list)"+NL)
txt.write("me.faces.extend(faces_list)"+NL)
#SAVES NEW SCRIPT TO SCRIPTS DIRECTORY
if SAVE :
    scr_directory=Get("scriptsdir")
    scr_name=scr_directory+os.sep+txt.name
    content=txt.asLines()
    f=open(scr_name,'w')
    for i in content:
        f.write(i+NL)
    f.close() 

NVM I figured it out by quoting your post.

nice one. that’s pretty much exactly what i was looking for. :smiley:

worth adding, it takes a couple of reloads of blender to show up for some reason. but that may have been because i didnt rename the mesh, it confused it, as cube primative already exists.

you might also want to take a look at the write_mesh_script() function in the BpyAddMesh module (distributed with blender in scripts/bpymodules)