Exports the selected blender object to a javascript object (through the clipboard)

  • Download Gist
export blender model to javascript object
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#!BPY
 
"""
Name: 'isom object'
Blender: 250
Group: 'Export'
Tooltip: 'Exports currently selected object as js file for use with WebGL'
Author: Schell Scivally (efnx.com)
"""
 
import bpy
import subprocess
 
def convert_obj(obj):
"""writes a function to create this obj"""
name = obj.name
mesh = obj.data
i = 0
out = "if (typeof blenderModels == 'undefined') {\n"
out += " blenderModels = {};\n"
out += "}\n"
out += "blenderModels."+name+" = function () {\n"
out += " var name = '"+name+"', faces = [], face;\n"
for face in mesh.faces:
meshtex = mesh.active_uv_texture.data[face.index];
i += 1
out += " //face #" + str(i) + "\n"
out += " face = {};\n"
out += " face.texture = '"+meshtex.image.name+"';\n"
out += " face.verts = [];\n"
out += " face.uvs = [];\n"
out += " face.normal = [\n"
out += " "+str(face.normal.x)+",\n"
out += " "+str(face.normal.y)+",\n"
out += " "+str(face.normal.z)+"\n"
out += " ];\n"
for vert in face.verts:
out += " face.verts.push(["+str(mesh.verts[vert].co.x)+","
out += str(mesh.verts[vert].co.y)+","
out += str(mesh.verts[vert].co.z)+"]);\n"
for uvpt in meshtex.uv:
out += " face.uvs.push(["+str(uvpt[0])+", "+str(uvpt[1])+"]);\n"
out += " faces.push(face);\n\n"
out += " return {\n"
out += " name : name,\n"
out += " faces : faces\n"
out += " }\n"
out += "}();\n"
return out
def setClipboardData(data):
p = subprocess.Popen(["pbcopy"], stdin=subprocess.PIPE)
p.stdin.write(data.encode("ascii"))
p.stdin.close()
retcode = p.wait()
setClipboardData(convert_obj(bpy.context.selected_editable_objects[0]));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.