I remember a script that could ‘export’ OB:MyMesh to a ‘addMyMesh.py’.
The idea seemed funny – that’s why I tried it in the first place… then noobishly deleted it.
Now, I would like to find it again, because I have a project where it would be a lot more intuitive to execute the generated scripts than to open libraries to append the objects.
I have been searching on Google and in this forum, but I don’t remember how this magic script was called.
Since I wanted to add some primitives to work faster on a project, I have made this one :
#!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()
I am only learning Python, so the generated script only takes into account vertices and faces in the original mesh. I’ll be glad if anybody wants to improve it. If anyone wants to use it, it’s Santa-licenced (free toys for all nice children !), as stated in the code.