-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathops_paintbrush.py
More file actions
171 lines (129 loc) · 6.62 KB
/
ops_paintbrush.py
File metadata and controls
171 lines (129 loc) · 6.62 KB
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
# 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 #####
import mathutils
import math
from .functions import *
from .database import *
from .draw import draw_callback_brush_px
class ModalDrawBrushOperator(bpy.types.Operator):
"""Draw props on scene"""
bl_idname = "ropy.modal_draw_brush"
bl_label = "Draw Props on surfaces"
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def add_prop(self,context,location,rotation):
groupName = self.group_list[self.current_var][1]
e = add_prop_instance(context,groupName)
loc_mat = Matrix.Translation(location)
scale_mat = Matrix.Scale(1,4,(1,0,0)) * Matrix.Scale(1,4,(0,1,0)) * Matrix.Scale(1,4,(0,0,1))
if context.scene.build_props.paint_random_scale:
factor = random.uniform(context.scene.build_props.paint_random_min_max[0], context.scene.build_props.paint_random_min_max[1])
scale_mat = Matrix.Scale(factor,4,(1,0,0)) * Matrix.Scale(factor,4,(0,1,0)) * Matrix.Scale(factor,4,(0,0,1))
v0 = Vector(( 0.0,0,1.0 ))
orig_rot_mat = v0.rotation_difference(rotation).to_matrix().to_4x4()
mat_rot = mathutils.Matrix.Rotation(0, 4, rotation)
if context.scene.build_props.paint_random_rotation:
factor = random.uniform(-context.scene.build_props.paint_random_max_angle,context.scene.build_props.paint_random_max_angle)
mat_rot = mathutils.Matrix.Rotation(factor, 4, rotation)
e.matrix_world = loc_mat * mat_rot * orig_rot_mat * scale_mat
self.temp_obj.append(e)
#we need to update the index for next addition
self.current_var += 1
self.current_var = self.current_var % len(self.group_list)
def recalculate_drawing_circle(self,context,location,rotation):
#todo, on calcule une liste de point autoir du point d'impact.
self.construction_points = []
for i in range(36):
mat_rot = mathutils.Matrix.Rotation(math.radians(i*10), 4, rotation)
new_loc = location+ Vector(( context.scene.build_props.brush_distance,0,0.0 )) *mat_rot
self.construction_points.append(construction_point(new_loc))
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
best_hit,best_obj,best_normal,best_face_index = get_ray_cast_result(context,coord_mouse,context.scene.build_props.paint_on_all_objects)
self.surface_found = False
if best_hit is not None:
self.surface_found = True
self.surface_normal = best_hit + best_normal
self.surface_hit = best_hit
self.recalculate_drawing_circle(context,best_hit,best_normal)
if self.lmb: #if the left button is pressed
self.delta += (self.previous_impact - self.surface_hit).length
#add a object if the distance between the previous one is too short
if self.delta > context.scene.build_props.brush_distance:
self.add_prop(context,best_hit,best_normal)
self.delta = 0.0
self.previous_impact = self.surface_hit
elif event.type == 'LEFTMOUSE':
if event.value == 'PRESS':
self.lmb = True
coord_mouse = Vector((event.mouse_region_x, event.mouse_region_y))
# get the ray from the viewport and mouse
best_hit,best_obj,best_normal,best_face_index = get_ray_cast_result(context,coord_mouse,context.scene.build_props.paint_on_all_objects)
if best_hit is not None:
self.previous_impact = best_hit
self.delta = 0
self.add_prop(context,best_hit,best_normal)
elif event.value == 'RELEASE':
self.lmb = False
elif event.type == 'RIGHTMOUSE':
if event.value == 'PRESS':
try:
last_obj = self.temp_obj[-1]
self.temp_obj.pop()
delete_temp_objects(context,[last_obj.name])
except:
print('List is Empty !')
elif event.type in {'S'} and event.value == 'PRESS':
context.scene.build_props.brush_distance += 0.1
elif event.type in {'R'} and event.value == 'PRESS':
context.scene.build_props.brush_distance += -0.1
elif event.type in {'ESC','RET'}:
context.area.header_text_set()
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
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':
self.report({'WARNING'}, "View3D not found, cannot run operator")
return {'CANCELLED'}
args = (self, context)
self._handle = bpy.types.SpaceView3D.draw_handler_add(
draw_callback_brush_px, args, 'WINDOW', 'POST_PIXEL')
self.construction_points = []
self.depth_location = Vector((0.0, 0.0, 0.0))
self.surface_found = False
self.lmb = False
self.delta = 0
self.temp_obj= []
self.current_var = 0
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)
self.help_string = 'R/S to change distance between two props'
context.area.header_text_set(self.help_string)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}