-
Notifications
You must be signed in to change notification settings - Fork 70
/
fc_cloth_op.py
215 lines (160 loc) · 6.87 KB
/
fc_cloth_op.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import bpy
from bpy.types import Operator
from .utils.unit_util import *
from .utils.fc_cloth_util import *
from .widgets . bl_ui_draw_op import *
from .widgets . bl_ui_label import *
from .widgets . bl_ui_button import *
from .widgets . bl_ui_slider import *
from .widgets . bl_ui_textbox import *
from .widgets . bl_ui_drag_panel import *
from .widgets . bl_ui_draw_op import *
class FC_ClothOperator(BL_UI_OT_draw_operator):
bl_idname = "object.cloth_op"
bl_label = "Clothify"
bl_description = "Add cloth modifier to an object"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if context.window_manager.modal_running:
return False
return len(context.selected_objects) > 0
def __init__(self):
y_top = 35
x_left = 100
self._current = ""
super().__init__()
self.panel = BL_UI_Drag_Panel(0, 0, 280, 170)
self.panel.bg_color = (0.1, 0.1, 0.1, 0.9)
self.lbl_Pressure = BL_UI_Label(20, y_top, 50, 15)
self.lbl_Pressure.text = "Pressure:"
self.lbl_Pressure.text_size = 14
self.lbl_Pressure.text_color = (0.9, 0.9, 0.9, 1.0)
self.txt_pressure = BL_UI_Textbox(x_left, y_top - 2, 100, 24)
self.txt_pressure.is_numeric = True
input_keys = self.txt_pressure.get_input_keys()
input_keys.remove('RET')
input_keys.remove('ESC')
self.txt_pressure.set_text_changed(self.on_pressure_changed)
y_top += 50
self.lbl_shrink = BL_UI_Label(20, y_top, 50, 15)
self.lbl_shrink.text = "Shrinking:"
self.lbl_shrink.text_size = 14
self.lbl_shrink.text_color = (0.9, 0.9, 0.9, 1.0)
self.sl_shrink = BL_UI_Slider(x_left, y_top, 100, 24)
self.sl_shrink.color = (0.3, 0.56, 0.94, 1.0)
self.sl_shrink.hover_color = (0.3, 0.56, 0.94, 0.8)
self.sl_shrink.min = -1.0
self.sl_shrink.max = 1.0
self.sl_shrink.decimals = 3
self.sl_shrink.show_min_max = False
self.sl_shrink.tag = 0
self.sl_shrink.set_value_change(self.on_shrinking_changed)
self.lbl_close = BL_UI_Label(195, y_top - 85, 50, 15)
self.lbl_close.text = "Escape to Close"
self.lbl_close.text_size = 10
self.lbl_close.text_color = (0.9, 0.9, 0.9, 1.0)
self.btn_start_stop = BL_UI_Button(20, y_top + 45, 80, 25)
self.btn_start_stop.bg_color = (0.3, 0.56, 0.94, 1.0)
self.btn_start_stop.hover_bg_color = (0.3, 0.56, 0.94, 0.8)
self.btn_start_stop.text_size = 14
self.btn_start_stop.set_mouse_down(self.on_btn_start_stop_down)
self.set_start_stop_text()
self.btn_apply = BL_UI_Button(105, y_top + 45, 80, 25)
self.btn_apply.bg_color = (0.3, 0.56, 0.94, 1.0)
self.btn_apply.hover_bg_color = (0.3, 0.56, 0.94, 0.8)
self.btn_apply.text_size = 14
self.btn_apply._textoffset = [0, 2]
self.btn_apply.text = "Apply"
self.btn_apply.set_mouse_down(self.on_btn_apply_down)
self.btn_close = BL_UI_Button(190, y_top + 45, 80, 25)
self.btn_close.bg_color = (0.3, 0.56, 0.94, 1.0)
self.btn_close.hover_bg_color = (0.3, 0.56, 0.94, 0.8)
self.btn_close.text_size = 14
self.btn_close.text = "Close"
self.btn_close.set_mouse_down(self.on_btn_close_down)
def modal(self, context, event):
active = context.view_layer.objects.active
if active:
if(self._current != "" and self._current != active.name):
self.init_widget_values()
self._current = active.name
return super().modal(context, event)
def get_pressure(self):
return float(self.txt_pressure.text)
def get_shrinking(self):
return float(self.sl_shrink.get_value())
def on_btn_close_down(self, widget):
self.finish()
def set_start_stop_text(self):
btn = self.btn_start_stop
btn._textoffset = [0,0]
if bpy.context.screen.is_animation_playing:
btn.text = "Stop"
btn._textoffset = [0,2]
else:
btn.text = "Start"
def on_btn_start_stop_down(self, widget):
scn = bpy.context.scene
if bpy.context.screen.is_animation_playing:
frame = scn.frame_current
bpy.ops.screen.animation_cancel()
scn.frame_set(frame)
else:
self.apply_changes()
scn.frame_set(0)
bpy.ops.screen.animation_play()
self.set_start_stop_text()
def on_btn_apply_down(self, widget):
mod_cloth = self.get_cloth_modifier()
if(mod_cloth):
bpy.ops.object.modifier_apply(modifier=mod_cloth.name)
self.finish()
def apply_changes(self):
pressure = self.get_pressure()
shrinking = self.get_shrinking()
mod_cloth = self.get_cloth_modifier()
if mod_cloth is not None:
mod_cloth.settings.uniform_pressure_force = pressure
mod_cloth.settings.shrink_min = shrinking
def on_shrinking_changed(self, slider, value):
self.apply_changes()
def on_pressure_changed(self, textbox, context, event):
self.apply_changes()
def on_finish(self, context):
context.window_manager.modal_running = False
super().on_finish(context)
def on_invoke(self, context, event):
bpy.ops.object.mode_set(mode="OBJECT")
self.add_cloth(context)
# Add new widgets here
widgets_panel = [self.lbl_Pressure, self.txt_pressure, self.lbl_shrink, self.sl_shrink, self.lbl_close]
widgets_panel.append(self.btn_start_stop)
widgets_panel.append(self.btn_apply)
widgets_panel.append(self.btn_close)
widgets = [self.panel]
widgets += widgets_panel
self.init_widgets(context, widgets)
self.panel.add_widgets(widgets_panel)
# Open the panel at the mouse location
self.panel.set_location(context.area.height / 2.0,
context.area.height / 2.0)
self.init_widget_values()
context.window_manager.modal_running = True
def get_cloth_modifier(self):
active_obj = bpy.context.view_layer.objects.active
if active_obj is not None:
return active_obj.modifiers.get("Cloth")
return None
def init_widget_values(self):
mod_cloth = self.get_cloth_modifier()
if mod_cloth is not None:
pressure = mod_cloth.settings.uniform_pressure_force
self.txt_pressure.text = "{:.2f}".format(pressure)
shrinking = mod_cloth.settings.shrink_min
self.sl_shrink.set_value(shrinking)
def add_cloth(self, context):
active_object = bpy.context.view_layer.objects.active
mod_cloth = self.get_cloth_modifier()
if mod_cloth is None:
execute_cloth(context.active_object)