-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathops_linebrush.py
213 lines (171 loc) · 7.98 KB
/
ops_linebrush.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
# BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# END GPL LICENSE BLOCK #####
from .functions import *
from .database import *
from .draw import draw_callback_line_px
class ModalDrawLineOperator(bpy.types.Operator):
"""Draw a line with the mouse"""
bl_idname = "ropy.modal_draw_line"
bl_label = "Line filled Assets"
def update_mouse_action(self,context):
delete_temp_objects(context,self.allobjs)
self.allobjs = []
vert_0 = None
for i in range(len(self.list_construction_points)):
vert_0 = self.list_construction_points[i].point
try:
vert_1 = self.list_construction_points[i+1].point
except:
break
edge_length = (vert_0-vert_1).length
direction_x = vert_1-vert_0
v0 = Vector(( 1.0,0,0 ))
#TODO get correct rotation angle !!!!!
rot = v0.rotation_difference( direction_x ).to_euler()
propsOrder = get_props_order(context,edge_length,self.group_list)
curent_distance = 0.0
scale_factor = context.scene.build_props.line_scale_factor
for dimX,groupName, scaleValue, offsetX in propsOrder:
dupli = add_prop_instance(context,groupName)
dupli.scale[0] = scaleValue* scale_factor
dupli.scale[1] = scale_factor
dupli.scale[2] = scale_factor
dupli.rotation_euler = rot
dupli.location = vert_0 + direction_x.normalized()*(curent_distance +(offsetX*scaleValue*scale_factor))
curent_distance += dimX*scaleValue
self.allobjs.append(dupli.name)
def execute(self, context):
# Create a mesh object to store polygon line
mesh_obj = bpy.data.meshes.new("meshLine")
# create a object data for mesh object
obj_crt = bpy.data.objects.new("meshLine", mesh_obj)
# link object to scene
context.scene.objects.link(obj_crt)
context.scene.objects.active = obj_crt
# Now copy the mesh object to bmesh
bme = bmesh.new()
bme.from_mesh(obj_crt.data)
matx = obj_crt.matrix_world.inverted()
# Add vertices
list_verts = []
for i in self.list_construction_points:
bme.verts.new(matx * i.point)
bme.verts.index_update()
bme.verts.ensure_lookup_table()
list_verts.append(bme.verts[-1])
# Add edges to bmesh
total_edge = len(list_verts)
for j in range(total_edge - 1):
bme.edges.new((list_verts[j], list_verts[(j + 1) % total_edge]))
bme.edges.index_update()
# add this data to bmesh object
bme.to_mesh(obj_crt.data)
bme.free()
# intialize all variables zero Value
self.list_construction_points[:] = []
list_verts[:] = []
self.depth_location = Vector((0.0, 0.0, 0.0))
return {'FINISHED'}
def modal(self, context, event):
context.area.tag_redraw()
region = context.region
rv3d = context.space_data.region_3d
if event.type == 'MOUSEMOVE':
coord_mouse = Vector((event.mouse_region_x, event.mouse_region_y))
self.mouse_path = coord_mouse
# get the ray from the viewport and mouse
hit,obj,normal,face_index = get_ray_cast_result(context,coord_mouse,
use_all_objects=context.scene.build_props.paint_on_all_objects)
self.surface_found = False
if hit is not None:
self.surface_found = True
self.surface_normal = hit + normal
self.surface_hit = hit
elif event.type == 'LEFTMOUSE':
if event.value == 'PRESS':
coord_mouse = Vector((event.mouse_region_x, event.mouse_region_y))
hit,obj,normal,face_index = get_ray_cast_result(context,
coord_mouse,
use_all_objects=context.scene.build_props.paint_on_all_objects)
new_point = None
if obj is not None:
mouse_loc_3d = view3d_utils.region_2d_to_location_3d(
region, rv3d, coord_mouse, hit)
new_point = construction_point(mouse_loc_3d,normal)
else:
mouse_loc_3d = view3d_utils.region_2d_to_location_3d(
region, rv3d, coord_mouse, self.depth_location)
new_point = construction_point(mouse_loc_3d,None)
self.list_construction_points.append(new_point)
self.depth_location = self.list_construction_points[-1].point
self.update_mouse_action(context)
elif event.value == 'RELEASE':
pass
elif event.type == 'RIGHTMOUSE':
if event.value == 'PRESS':
if not self.list_construction_points:
self.depth_location = Vector((0.0, 0.0, 0.0))
elif len(self.list_construction_points) == 1:
self.list_construction_points.pop()
self.depth_location = Vector((0.0, 0.0, 0.0))
else:
self.list_construction_points.pop()
self.depth_location = self.list_construction_points[-1].point
self.update_mouse_action(context)
elif event.value == 'RELEASE':
pass
elif event.type in {'S'} and event.value == 'PRESS':
context.scene.build_props.seed += 1
self.update_mouse_action(context)
elif event.type in {'R'} and event.value == 'PRESS':
context.scene.build_props.seed += -1
self.update_mouse_action(context)
elif event.type in {'RET'}:
context.area.header_text_set()
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
self.execute(context)
return {'FINISHED'}
elif event.type in {'ESC'}:
context.area.header_text_set()
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
self.list_construction_points[:] = []
self.depth_location = Vector((0.0, 0.0, 0.0))
return {'CANCELLED'}
return {'PASS_THROUGH'}
def invoke(self, context, event):
if context.area.type == 'VIEW_3D':
args = (self, context)
self._handle = bpy.types.SpaceView3D.draw_handler_add(
draw_callback_line_px, args, 'WINDOW', 'POST_PIXEL')
#We iniatialize all the variables we are going to use
self.mouse_path = []
self.list_construction_points = []
self.depth_location = Vector((0.0, 0.0, 0.0))
self.surface_found = False
self.allobjs = []
dbPath = get_db_path(context)
catId = context.scene.build_props.assets_categories
link_category_to_file(context)
self.group_list = get_group_list_in_category(dbPath,catId)
context.window_manager.modal_handler_add(self)
self.help_string = 'R/S to get change random seed'
context.area.header_text_set(self.help_string)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "View3D not found, cannot run operator")
return {'CANCELLED'}