Created
December 4, 2015 14:20
-
-
Save zeffii/5553b0777b2a28999082 to your computer and use it in GitHub Desktop.
something_like_this.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Loft curves given by files into surface | |
# | |
# INPUT | |
# p0.txt, p1.txt | |
# | |
# OUTPUT | |
# loftsurf.ply | |
# | |
# USAGE | |
# | |
# Commandline | |
# blender --background --python blender_loft.py p0.txt p1.txt | |
# b3d --background --python blender_loft.py p0.txt p1.txt | |
# | |
# Inside blender console | |
# | |
# filename = "PWD/blender_loft.py" | |
# exec(compile(open(filename).read(), filename, 'exec')) | |
# | |
# AUTHOR | |
# Ricardo Fabbri <rfabbri@gmail.com>, 3 Dec 2015 | |
# | |
import sys | |
import bpy | |
import bpy_extras | |
import numpy | |
from mathutils import Matrix | |
import addon_utils | |
addon_utils.enable('mesh_bsurfaces') | |
# Create a spline/bezier from a list of points | |
def new_curve_from_points(p0, name_prefix): | |
npts = len(p0) | |
c0 = bpy.data.curves.new(name_prefix + 'CurveToLoft', 'CURVE') | |
o0 = bpy.data.objects.new(name_prefix + 'CurveToLoft', c0) | |
bpy.context.scene.objects.link(o0) | |
c0.dimensions = "3D" | |
spline = o0.data.splines.new('BEZIER') | |
spline.bezier_points.add(npts - 1) | |
# ^-- less one because one point is added when the spline is created. | |
for p in range(0, npts): | |
spline.bezier_points[p].co = p0[p] | |
# spline.bezier_points[p].co = [p0[p][0], p0[p][1], p0[p][2]] | |
return c0, o0, spline | |
def read_files(p0_fname, p1_fname): | |
p0 = numpy.loadtxt(p0_fname) | |
p0.tolist() | |
p1 = numpy.loadtxt(p1_fname) | |
p1.tolist() | |
print(p0, p1) | |
return p0, p1 | |
def test1(file1, file2): | |
# cleanup() | |
p0, p1 = read_files(file1, file2) | |
loft(p0, p1) | |
bpy.ops.export_mesh.ply(filepath='loftsurf.ply') | |
def loft(p0, p1): | |
c0, o0, s0 = new_curve_from_points(p0, 'first') | |
c1, o1, s1 = new_curve_from_points(p1, 'second') | |
# join curves into a group | |
bpy.data.objects[o0.name].select = True | |
bpy.data.objects[o1.name].select = True | |
bpy.context.scene.objects.active = bpy.data.objects[o1.name] | |
bpy.ops.object.join('INVOKE_REGION_WIN') | |
# create a mesh to store the final surface | |
me = bpy.data.meshes.new("outputLoft") | |
ob = bpy.data.objects.new("outputLoft", me) | |
scn = bpy.context.scene | |
scn.objects.link(ob) | |
scn.objects.active = ob | |
ob.select = True | |
# curves + object should be selected | |
# call lofting | |
bpy.ops.object.mode_set('INVOKE_REGION_WIN', mode='EDIT', toggle=True) | |
bpy.ops.mesh.reveal() | |
bpy.ops.gpencil.surfsk_add_surface('INVOKE_DEFAULT') | |
if __name__ == "__main__": | |
file1, file2 = sys.argv[-2:] | |
test1(file1, file2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
p0.txt
p1.txt