-
-
Save anonymous/87cea7953d72026049ea0592cde6882a to your computer and use it in GitHub Desktop.
Sketchfab
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
# Blender 2.77 | |
# https://sketchfab.com/models/926acd7aed8a47779685bf3058128b47 | |
import bpy | |
import os | |
class ADDCurveFromList: | |
def __init__(self, curvename): | |
self.curvename = curvename | |
def add_curve_from_list(self, list): | |
length = len(list) | |
objname = self.curvename | |
curvedata = bpy.data.curves.new(name=self.curvename, type='CURVE') | |
curvedata.dimensions = '3D' | |
objectdata = bpy.data.objects.new(objname, curvedata) | |
objectdata.location = (0,0,0) | |
bpy.context.scene.objects.link(objectdata) | |
polyline = curvedata.splines.new('POLY') | |
polyline.points.add(length - 1) | |
i = 0 | |
while i < length: | |
polyline.points[i].co = list[i] | |
i = i + 1 | |
class ADDCurveAttractor: | |
def add_curve_attractor(self, MAX_ITERATION, SCALE=None): | |
self.MAX_ITERATION = MAX_ITERATION | |
if SCALE is None: | |
SCALE = 1 | |
point = [] | |
x, y, z = self.initialize() | |
i = 0 | |
while i < self.MAX_ITERATION: | |
dx, dy, dz = self.iteration(x, y, z) | |
x = x + dx | |
y = y + dy | |
z = z + dz | |
point.append((x*SCALE, y*SCALE, z*SCALE, 1)) | |
i = i + 1 | |
self.add_curve_from_list(point) | |
class WangSun(ADDCurveFromList, ADDCurveAttractor): | |
def initialize(self): | |
x = 0.15 | |
y = 0.1 | |
z = 0.2 | |
return x, y, z | |
DT = 0.05 | |
A = 0.2 | |
B = -0.01 | |
C = 1.0 | |
D = -0.4 | |
E = -1.0 | |
F = -1.0 | |
def iteration(self, x, y, z): | |
dx = (self.A*x + self.C*y*z)*self.DT | |
dy = (self.B*x + self.D*y - x*z)*self.DT | |
dz = (self.E*z + self.F*x*y)*self.DT | |
return dx, dy, dz | |
class ConvertMeshFromCurve: | |
def __init__(self, curvename): | |
self.curvename = curvename | |
def fill(self, fill=None): | |
if fill is None: | |
fill = 'BACK' | |
bpy.ops.object.select_all(action='DESELECT') | |
bpy.data.objects[self.curvename].select=True | |
bpy.context.scene.objects.active = bpy.data.objects[self.curvename] | |
bpy.context.object.data.fill_mode = fill | |
bpy.context.object.data.bevel_depth = 0.05 | |
bpy.ops.object.shade_flat() | |
bpy.ops.object.convert(target='MESH') | |
bpy.data.objects[self.curvename].data.name = self.curvename | |
def solid(self): | |
self.fill() | |
bpy.ops.object.modifier_add(type='SOLIDIFY') | |
bpy.context.object.modifiers["Solidify"].thickness = 0.07 | |
bpy.context.object.modifiers["Solidify"].use_quality_normals = True | |
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Solidify") | |
class ADDMeshStopmotion: | |
def __init__(self, meshname): | |
self.meshname = meshname | |
def stopmotion(self, nth_sel): | |
checker_sel = [nth_sel, 1] | |
bpy.ops.object.select_all(action='DESELECT') | |
bpy.data.objects[self.meshname].select=True | |
bpy.context.scene.objects.active = bpy.data.objects[self.meshname] | |
for h in range(0, checker_sel[0]): | |
bpy.ops.object.mode_set(mode = 'EDIT') | |
bpy.ops.mesh.select_mode(type="FACE") | |
bpy.ops.mesh.select_all(action='DESELECT') | |
bpy.ops.mesh.select_all(action='SELECT') | |
bpy.ops.mesh.select_nth( | |
nth=checker_sel[0], | |
skip=checker_sel[1], | |
offset=h, | |
) | |
bpy.ops.mesh.duplicate() | |
bpy.ops.mesh.separate(type='SELECTED') | |
bpy.ops.object.mode_set(mode = 'OBJECT') | |
bpy.ops.object.select_all(action='DESELECT') | |
bpy.data.objects[self.meshname].select=True | |
bpy.context.scene.objects.active = bpy.data.objects[self.meshname] | |
bpy.ops.object.delete(use_global=False) | |
def ExportPlyAll(path): | |
if os.path.isdir(path) is False: | |
# os.makedirs(path) | |
print("'path' is missing") | |
return 0 | |
bpy.ops.object.select_all(action='DESELECT') | |
bpy.ops.object.select_by_type(type='MESH') | |
objList = [obj.name for obj in bpy.context.selected_objects] | |
objList = sorted(objList) | |
for i in objList: | |
_objList = i.replace('.', '_') | |
bpy.ops.object.select_all(action='DESELECT') | |
bpy.data.objects[i].select=True | |
bpy.context.scene.objects.active = bpy.data.objects[i] | |
bpy.data.objects[i].data.name = _objList | |
bpy.context.active_object.name = _objList | |
bpy.ops.export_mesh.ply( | |
filepath=path+_objList+".ply", | |
check_existing=True, | |
axis_forward='Y', | |
axis_up='Z', | |
filter_glob="*.ply", | |
use_mesh_modifiers=True, | |
use_normals=True, | |
use_uv_coords=True, | |
use_colors=True, | |
global_scale=1.0 | |
) | |
def ExportTXTSketchfabTimeframe(path, SEC): | |
if os.path.isdir(path) is True: | |
bpy.ops.object.select_all(action='DESELECT') | |
bpy.ops.object.select_by_type(type='MESH') | |
objList = [obj.name for obj in bpy.context.selected_objects] | |
objList = sorted(objList) | |
timeframe = open(path + 'sketchfab.timeframe', 'w') | |
with timeframe as text: | |
for i in objList: | |
text.write(SEC + " " + i + ".ply" + "\n") | |
bpy.ops.object.select_all(action='DESELECT') | |
print("writing '" + path + "'sketchfab.timeframe done") | |
objname = "Wang-Sun" | |
max_iteration = 20000 | |
frame = 10 | |
sec = "0.1" | |
path = "/path/to/" | |
# "~/Desktop/attractor/test/" | |
# "/Users/your-name/Desktop/attractor/test/" | |
# "C:/Users/your-name/Desktop/attractor/test/" | |
WangSun(objname).add_curve_attractor(max_iteration) | |
ConvertMeshFromCurve(objname).fill() | |
ADDMeshStopmotion(objname).stopmotion(frame) | |
ExportPlyAll(path) | |
ExportTXTSketchfabTimeframe(path, sec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment