Sun Position: Fix crash when Blender was started in background
[blender-addons.git] / curve_tools / auto_loft.py
blobb14aaf3b84b69b88c23dd3326da4886728c2a471
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 from bpy.props import BoolProperty
5 from bpy.types import Operator, Panel
6 from . import surfaces
7 from . import curves
8 from . import util
11 class OperatorAutoLoftCurves(Operator):
12 bl_idname = "curvetools.create_auto_loft"
13 bl_label = "Loft"
14 bl_description = "Lofts selected curves"
15 bl_options = {'UNDO'}
17 @classmethod
18 def poll(cls, context):
19 return util.Selected2Curves()
21 def execute(self, context):
22 #print("### TODO: OperatorLoftcurves.execute()")
23 mesh = bpy.data.meshes.new("LoftMesh")
25 curve0 = context.selected_objects[0]
26 curve1 = context.selected_objects[1]
28 ls = surfaces.LoftedSurface(curves.Curve(curve0), curves.Curve(curve1), "AutoLoft")
30 ls.bMesh.to_mesh(mesh)
32 loftobj = bpy.data.objects.new(self.name, mesh)
34 context.collection.objects.link(loftobj)
35 loftobj["autoloft"] = True
36 ui_data = loftobj.id_properties_ui("autoloft")
37 ui_data.update(description="Auto loft from %s to %s" % (curve0.name, curve1.name))
38 loftobj["autoloft_curve0"] = curve0.name
39 loftobj["autoloft_curve1"] = curve1.name
41 return {'FINISHED'}
43 class AutoLoftModalOperator(Operator):
44 """Auto Loft"""
45 bl_idname = "curvetools.update_auto_loft_curves"
46 bl_label = "Update Auto Loft"
47 bl_description = "Update Lofts selected curves"
49 _timer = None
50 @classmethod
51 def poll(cls, context):
52 # two curves selected.
53 return True
55 def execute(self, context):
56 scene = context.scene
57 lofters = [o for o in scene.objects if "autoloft" in o.keys()]
58 # quick hack
59 #print("TIMER", lofters)
61 for loftmesh in lofters:
62 curve0 = scene.objects.get(loftmesh['autoloft_curve0'])
63 curve1 = scene.objects.get(loftmesh['autoloft_curve1'])
64 if curve0 and curve1:
65 ls = surfaces.LoftedSurface(curves.Curve(curve0), curves.Curve(curve1), loftmesh.name)
66 ls.bMesh.to_mesh(loftmesh.data)
67 return {'FINISHED'}
69 def register():
70 bpy.utils.register_class(AutoLoftModalOperator)
71 bpy.utils.register_class(OperatorAutoLoftCurves)
72 bpy.types.WindowManager.auto_loft = BoolProperty(default=False,
73 name="Auto Loft")
74 bpy.context.window_manager.auto_loft = False
76 def unregister():
77 bpy.utils.unregister_class(AutoLoftModalOperator)
78 bpy.utils.unregister_class(OperatorAutoLoftCurves)
80 if __name__ == "__main__":
81 register()