Created
September 27, 2020 03:42
-
-
Save lancelet/906c38e452cb9496bc530a61235600ae to your computer and use it in GitHub Desktop.
Attempt at a Utah Teapot loader
This file contains hidden or 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
| # Crappy Utah Teapot loader script for Blender 2.90.1 | |
| import bpy | |
| from mathutils import Vector | |
| def create4x4patch(points): | |
| surface_data = bpy.data.curves.new('wook', 'SURFACE') | |
| surface_data.dimensions = '3D' | |
| # set points per segments (U * V) | |
| for i in range(0, 16, 4): | |
| spline = surface_data.splines.new(type='NURBS') | |
| spline.points.add(3) # already has a default vector | |
| for p, new_co in zip(spline.points, points[i:i+4]): | |
| p.co = new_co | |
| surface_object = bpy.data.objects.new('NURBS_OBJ', surface_data) | |
| bpy.context.collection.objects.link(surface_object) | |
| splines = surface_object.data.splines | |
| for s in splines: | |
| for p in s.points: | |
| p.select = True | |
| bpy.context.view_layer.objects.active = surface_object | |
| bpy.ops.object.mode_set(mode = 'EDIT') | |
| bpy.ops.curve.make_segment() | |
| bpy.ops.object.mode_set(mode = 'OBJECT') | |
| bpy.context.object.data.splines[0].use_endpoint_u = True | |
| bpy.context.object.data.splines[0].use_endpoint_v = True | |
| # File from: http://www.holmes3d.net/graphics/teapot/teapotCGA.bpt | |
| with open('<insert-filename.bpt>', 'r') as infile: | |
| str_data = infile.read().splitlines(True) | |
| # Drop the first line | |
| del str_data[0] | |
| # Create points | |
| while len(str_data) > 0: | |
| coords = [] | |
| del str_data[0] | |
| for line in str_data[0:16]: | |
| items = list(map(lambda s: float(s.strip()), line.split())) | |
| coords.append(Vector((items[0], items[1], items[2], 1.0))) | |
| del str_data[0:16] | |
| create4x4patch(coords) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another version with embedded GLUT teapot data: