70ecc4c83de91218a8873e4de71721e9ca420df5
[blender-addons-contrib.git] / curve_tools / auto_loft.py
1 import bpy
2 from bpy.props import BoolProperty
3 from bpy.types import Operator, Panel
4 from curve_tools.Surfaces import LoftedSurface
5 from curve_tools.Curves import Curve
6 from curve_tools import Util
7
8
9 class OperatorAutoLoftCurves(Operator):
10     bl_idname = "curvetools2.create_auto_loft"
11     bl_label = "Loft"
12     bl_description = "Lofts selected curves"
13
14
15     @classmethod
16     def poll(cls, context):
17         return Util.Selected2Curves()
18
19
20     def execute(self, context):
21         #print("### TODO: OperatorLoftCurves.execute()")
22         scene = context.scene
23         mesh = bpy.data.meshes.new("LoftMesh")
24
25         curve0 = context.selected_objects[0]
26         curve1 = context.selected_objects[1]
27
28         ls = LoftedSurface(Curve(curve0), Curve(curve1), "AutoLoft")
29
30         ls.bMesh.to_mesh(mesh)
31
32         loftobj = bpy.data.objects.new(self.name, mesh)
33
34         scene.objects.link(loftobj)
35         loftobj["autoloft"] = True
36         if loftobj.get('_RNA_UI') is None:
37             loftobj['_RNA_UI'] = {}
38         loftobj['_RNA_UI']["autoloft"] = {
39                            "name": "Auto Loft",
40                            "description": "Auto loft from %s to %s" % (curve0.name, curve1.name),
41                            "curve0": curve0.name,
42                            "curve1": curve1.name}
43         print(loftobj['_RNA_UI'].to_dict())
44         self.report({'INFO'}, "OperatorAutoLoftCurves.execute()")
45
46         return {'FINISHED'}
47
48
49 class AutoLoftModalOperator(Operator):
50     """Auto Loft"""
51     bl_idname = "wm.auto_loft_curve"
52     bl_label = "Auto Loft"
53     bl_description = "Lofts selected curves"
54
55     _timer = None
56     @classmethod
57     def poll(cls, context):
58         # two curves selected.
59         return True
60
61     def modal(self, context, event):
62         scene = context.scene
63         wm = context.window_manager
64         if event.type in {'ESC'}:
65             wm.auto_loft = False
66
67         if not wm.auto_loft:
68             self.cancel(context)
69             return {'CANCELLED'}
70
71         if event.type == 'TIMER':
72             lofters = [o for o in scene.objects if "autoloft" in o.keys()]
73             # quick hack
74             #print("TIMER", lofters)
75
76             for loftmesh in lofters:
77                 loftmesh.hide_select = True
78                 rna = loftmesh['_RNA_UI']["autoloft"].to_dict()
79                 curve0 = scene.objects.get(rna["curve0"])
80                 curve1 = scene.objects.get(rna["curve1"])
81                 if curve0 and curve1:
82                     ls = LoftedSurface(Curve(curve0), Curve(curve1), loftmesh.name)
83                     ls.bMesh.to_mesh(loftmesh.data)
84
85         return {'PASS_THROUGH'}
86
87     def execute(self, context):
88         wm = context.window_manager
89         self._timer = wm.event_timer_add(0.1, context.window)
90         wm.modal_handler_add(self)
91         return {'RUNNING_MODAL'}
92
93     def cancel(self, context):
94         wm = context.window_manager
95         wm.event_timer_remove(self._timer)
96
97
98
99 def run_auto_loft(self, context):
100     if self.auto_loft:
101         bpy.ops.wm.auto_loft_curve()
102     return None
103
104 def register():
105     bpy.utils.register_class(AutoLoftModalOperator)
106     bpy.utils.register_class(OperatorAutoLoftCurves)
107     bpy.types.WindowManager.auto_loft = BoolProperty(default=False,
108                                                      name="Auto Loft",
109                                                      update=run_auto_loft)
110     bpy.context.window_manager.auto_loft = False
111
112 def unregister():
113     bpy.utils.unregister_class(AutoLoftModalOperator)
114     bpy.utils.unregister_class(OperatorAutoLoftCurves)
115
116 if __name__ == "__main__":
117     register()
118
119
120     # test call
121     #bpy.ops.wm.modal_timer_operator()