blender: add progress report

This commit is contained in:
Julian Daube 2019-07-02 19:39:34 +02:00
parent 4565879ffb
commit 99b4d73c24
2 changed files with 56 additions and 10 deletions

View File

@ -29,7 +29,6 @@ from bpy_extras.io_utils import (
axis_conversion, axis_conversion,
) )
IOOBJOrientationHelper = orientation_helper_factory("IOOBJOrientationHelper", axis_forward='-Z', axis_up='Y') IOOBJOrientationHelper = orientation_helper_factory("IOOBJOrientationHelper", axis_forward='-Z', axis_up='Y')

View File

@ -8,6 +8,49 @@ import pathlib
import bpy import bpy
from bpy_extras.wm_utils.progress_report import ProgressReport from bpy_extras.wm_utils.progress_report import ProgressReport
class Progressor:
def __init__(self, wm, precision = 0.001):
self.wm = wm
self.report = 0
self.precision = precision
self.next_update = 0
@property
def total(self):
return self.report
@total.setter
def total(self, t):
if self.report >= t:
return
if self.report:
self.wm.progress_end()
self.wm.progress_begin(0, t)
self.report = t
@property
def current(self):
return 0
@current.setter
def current(self, c):
prog = float(c)/self.total
if prog >= self.next_update:
self.next_update = prog + self.precision
self.wm.progress_update(c)
def end(self):
if self.report:
self.report = 0
self.wm.progress_end()
def __call__(self, update):
self.total = update.total
self.current = update.current
def load(context, def load(context,
filepath, filepath,
*, *,
@ -24,16 +67,19 @@ def load(context,
): ):
path = pathlib.Path(filepath) path = pathlib.Path(filepath)
lib = None
print(lib)
with ProgressReport(context.window_manager) as progress: with ProgressReport(context.window_manager) as progress:
progress.enter_substeps(1, "Importing GDS %r..." % filepath) progress.enter_substeps(2, "Importing GDS %r..." % filepath)
prog = Progressor(context.window_manager)
with open(filepath, "rb") as f: with open(filepath, "rb") as f:
lib = gds.parse_file(f) lib = gds.parse_file(f, progress_func=prog)
progress.step("Done, linking references") progress.step("Done, linking references")
lib.link_refs() lib.link_refs(prog)
progress.step("Done, building meshes") progress.step("Done, building meshes")
prog.end()
layergroups = {} layergroups = {}
@ -41,6 +87,8 @@ def load(context,
if not len(value.elements): if not len(value.elements):
continue continue
print("structure name: {}".format(name))
verts = {} verts = {}
faces = {} faces = {}
@ -60,14 +108,14 @@ def load(context,
indices.append(len(verts[element.layer])-1) indices.append(len(verts[element.layer])-1)
faces[element.layer].append(tuple(indices)) faces[element.layer].append(tuple(indices))
# build structure object
for layer in verts.keys(): for layer in verts.keys():
if layer not in layergroups: if layer not in layergroups:
layergroups[layer] = bpy.data.groups.new("layer{}".format(layer)) layergroups[layer] = bpy.data.groups.new("layer{}".format(layer))
meshname = "{}[{}]".format(name.replace("\x00", ""), layer) meshname = "{}[{}]".format(name, layer)
print(layer, meshname)
mesh = bpy.data.meshes.new(meshname) mesh = bpy.data.meshes.new(meshname)
mesh.from_pydata(verts[layer], [], faces[layer]) mesh.from_pydata(verts[layer], [], faces[layer])
mesh.update() mesh.update()
@ -77,8 +125,7 @@ def load(context,
context.scene.objects.link(obj) context.scene.objects.link(obj)
layergroups[layer].objects.link(obj) layergroups[layer].objects.link(obj)
obj.parent = root obj.parent = root
progress.leave_substeps("Done") progress.leave_substeps("Done")
return {"FINISHED"} return {"FINISHED"}