Skip to content

Instantly share code, notes, and snippets.

@schell
Created September 8, 2010 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save schell/569929 to your computer and use it in GitHub Desktop.
Save schell/569929 to your computer and use it in GitHub Desktop.
Exports the selected blender object to a javascript object (through the clipboard)
#!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]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment