Skip to content

Commit

Permalink
Merge branch 'v1.2' without Gradient Tool/Sharpen
Browse files Browse the repository at this point in the history
Adds filter mask/weights changes
Adds conversion of normals to vertex colors and vice versa
Adds panel location changing from N panel to Header option
Switch mac drawing name to alt drawing to avoid confusion of only working on mac systems
Rename Self to Modal for code clarity
  • Loading branch information
CodyWinch committed May 16, 2022
1 parent 4de98e8 commit 34820a9
Show file tree
Hide file tree
Showing 26 changed files with 10,090 additions and 5,224 deletions.
9 changes: 7 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from . import operators_modal
from . import operators
from . import properties
from . import ui
from . import keymap
Expand All @@ -7,9 +8,9 @@
bl_info = {
"name": "Abnormal",
"author": "Cody Winchester (codywinch)",
"version": (1, 1, 1),
"version": (1, 1, 2),
"blender": (2, 80, 0),
"location": "3D View > Object and Properties > Object tab",
"location": "3D View > N Panel/Header > BNPR Abnormal Tab",
"description": "BNPR Normal Editing Tools",
"warning": "",
"wiki_url": "",
Expand All @@ -27,20 +28,24 @@
importlib.reload(properties)
if "operators_modal" in locals():
importlib.reload(operators_modal)
if "operators" in locals():
importlib.reload(operators)


def register():
ui.register()
keymap.register()
properties.register()
operators_modal.register()
operators.register()


def unregister():
ui.unregister()
keymap.unregister()
properties.unregister()
operators_modal.unregister()
operators.unregister()


if __name__ == "__main__":
Expand Down
149 changes: 118 additions & 31 deletions classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@


class ABNContainer:
def __init__(self, mat, mac_shader=False):
self.mac_shader = mac_shader
def __init__(self, mat, alt_shader=False):
self.alt_shader = alt_shader

if mac_shader:
self.create_mac_shader()
if alt_shader:
self.create_alt_shader()
else:
self.create_shader()
self.create_point_shader()
Expand All @@ -25,6 +25,7 @@ def __init__(self, mat, mac_shader=False):

self.draw_tris = False
self.draw_only_selected = False
self.draw_weights = True
self.scale_selection = True

# NP ARRAYS
Expand All @@ -45,15 +46,18 @@ def __init__(self, mat, mac_shader=False):
self.sel_status = None
self.act_status = None
self.filter_weights = None
self.filter_mask = None

self.vert_link_vs = None
self.vert_link_ls = None
self.face_link_vs = None
self.face_link_ls = None
self.face_link_eds = None
self.edge_link_vs = None
self.edge_link_fs = None
self.loop_faces = None
self.loop_verts = None
self.loop_edges = None

self.color_tri = (0.16, 0.55, 0.7, 0.2)
self.color_tri_sel = (0.16, 0.7, 0.9, 0.5)
Expand All @@ -63,14 +67,17 @@ def __init__(self, mat, mac_shader=False):
self.color_po_sel = (0.1, 0.7, 0.9, 1.0)
self.color_po_act = (0.0, 0.0, 1.0, 1.0)

self.color_po_zero_weight = (0.66, 1.0, 1.0, 1.0)
self.color_po_full_weight = (0.0, 1.0, 1.0, 1.0)

self.color_normal = (0.83, 0.3, 0.4, 1.0)
self.color_normal_sel = (0.83, 0.7, 0.9, 1.0)
self.color_normal_act = (0.0, 0.0, 0.9, 1.0)

self.update_color_render()
return

def create_mac_shader(self):
def create_alt_shader(self):
self.shader = gpu.shader.from_builtin('3D_FLAT_COLOR')
return

Expand Down Expand Up @@ -137,7 +144,7 @@ def create_point_shader(self):
return

def clear_batches(self):
if self.mac_shader:
if self.alt_shader:
self.batch_po = batch_for_shader(
self.shader, 'POINTS', {"pos": [], "color": []})
self.batch_po_sel = batch_for_shader(
Expand Down Expand Up @@ -166,6 +173,8 @@ def update_active(self):
po_cos = self.loop_coords[self.sel_status]
po_norms = self.new_norms[self.sel_status]
act_status = self.act_status[self.sel_status]
filt_mask = self.filter_mask[self.sel_status]
weight_mask = self.filter_weights[self.sel_status][filt_mask]

if self.scale_selection:
world_norms = po_cos + \
Expand All @@ -186,20 +195,42 @@ def update_active(self):
cols.shape = [po_cos.shape[0], 4]
cols[act_status] = self.rcol_normal_act

# Draw filter weights on norms
if self.draw_weights:
w_cols = np.zeros(filt_mask.nonzero()[0].size * 4,
dtype=np.float32).reshape(-1, 4)
f_cols = w_cols.copy()

w_cols[:] = self.color_po_zero_weight
f_cols[:] = self.color_po_full_weight

w_cols = w_cols * (1.0 - weight_mask.reshape(-1, 1)) + \
f_cols * weight_mask.reshape(-1, 1)

w_cols = hsv_to_rgb_array(w_cols)

cols[filt_mask] = w_cols

norm_colors = np.array(list(zip(cols, cols)))
norm_colors.shape = [po_cos.shape[0] * 2, 4]

if self.alt_shader:
norm_colors[:, [0, 1, 2]] *= self.brightness

#

self.batch_active_normal = batch_for_shader(
self.shader, 'LINES', {"pos": list(norm_lines), "color": list(norm_colors)})

return

def update_static(self, exclude_active=False):
# POINTS
# all points are static
sel_mask = self.sel_status[~self.hide_status]
act_mask = self.act_status[~self.hide_status]
filt_mask = self.filter_mask[~self.hide_status]
weight_mask = self.filter_weights[~self.hide_status][filt_mask]

points = self.loop_coords[~self.hide_status]

Expand All @@ -212,7 +243,25 @@ def update_static(self, exclude_active=False):
po_colors[sel_mask] = self.rcol_po_sel
po_colors[act_mask] = self.rcol_po_act

if self.mac_shader:
# Draw filter weights on points
if self.draw_weights:
w_cols = np.zeros(filt_mask.nonzero()[0].size * 4,
dtype=np.float32).reshape(-1, 4)
f_cols = w_cols.copy()

w_cols[:] = self.color_po_zero_weight
f_cols[:] = self.color_po_full_weight

w_cols = w_cols * (1.0 - weight_mask.reshape(-1, 1)) + \
f_cols * weight_mask.reshape(-1, 1)

w_cols = hsv_to_rgb_array(w_cols)

po_colors[filt_mask] = w_cols

if self.alt_shader:
po_colors[:, [0, 1, 2]] *= self.brightness

self.batch_po = batch_for_shader(
self.shader, 'POINTS', {"pos": list(points[~sel_mask]), "color": list(po_colors[~sel_mask])})
self.batch_po_sel = batch_for_shader(
Expand All @@ -224,6 +273,8 @@ def update_static(self, exclude_active=False):
self.point_shader, 'POINTS', {"pos": list(points), "size": list(sizes), "color": list(po_colors)})

#
#
#

# LOOP TRIS
# all loop tris are static if used
Expand All @@ -239,15 +290,24 @@ def update_static(self, exclude_active=False):
t_colors[sel_mask] = self.rcol_tri_sel
t_colors[act_mask] = self.rcol_tri_act

# Draw filter weights on loop tris
if self.draw_weights:
t_colors[filt_mask] = w_cols

tri_colors = np.array(list(zip(t_colors, t_colors, t_colors)))
tri_colors.shape = [tris.shape[0]*3, 4]

tris.shape = [tris.shape[0]*3, 3]

if self.alt_shader:
tri_colors[:, [0, 1, 2]] *= self.brightness

self.batch_tri = batch_for_shader(
self.shader, 'TRIS', {"pos": list(tris), "color": list(tri_colors)})

#
#
#

# NORMALS
# only non selected loop normals are static if exclude_active is true otherwise all loop normals are static
Expand All @@ -256,6 +316,9 @@ def update_static(self, exclude_active=False):
non_sel_status[self.sel_status] = False
non_sel_status = non_sel_status[~self.hide_status]

w_cols = w_cols[non_sel_status[filt_mask]]
filt_mask = filt_mask[non_sel_status]

po_cos = self.loop_coords[~self.hide_status][non_sel_status]
po_norms = self.new_norms[~self.hide_status][non_sel_status]
sel_mask = []
Expand Down Expand Up @@ -286,6 +349,10 @@ def update_static(self, exclude_active=False):
n_colors[sel_mask] = self.rcol_normal_sel
n_colors[act_mask] = self.rcol_normal_act

# Draw filter weights on normals
if self.draw_weights:
n_colors[filt_mask] = w_cols

if self.draw_only_selected:
po_cos = po_cos[sel_mask]
world_norms = world_norms[sel_mask]
Expand All @@ -297,6 +364,9 @@ def update_static(self, exclude_active=False):
norm_colors = np.array(list(zip(n_colors, n_colors)))
norm_colors.shape = [n_colors.shape[0] * 2, 4]

if self.alt_shader:
norm_colors[:, [0, 1, 2]] *= self.brightness

self.batch_normal = batch_for_shader(
self.shader, 'LINES', {"pos": list(norms), "color": list(norm_colors)})

Expand All @@ -319,16 +389,12 @@ def update_color_render(self):
def draw(self):
matrix = bpy.context.region_data.perspective_matrix

if self.draw_tris:
# Static Tris
self.shader.bind()
self.shader.uniform_float("viewProjectionMatrix", matrix)
self.shader.uniform_float("brightness", self.brightness)
# self.shader.uniform_float("opacity", self.opacity)
# self.shader.uniform_float("color", tri_color)
self.batch_tri.draw(self.shader)
if self.alt_shader:
if self.draw_tris:
# Static Tris
self.shader.bind()
self.batch_tri.draw(self.shader)

if self.mac_shader:
bgl.glPointSize(5*self.size)
# Static Non Sel Points
self.shader.bind()
Expand All @@ -344,7 +410,24 @@ def draw(self):
self.shader.bind()
self.batch_po_act.draw(self.shader)

# Static Normals
self.shader.bind()
self.batch_normal.draw(self.shader)

# Active Normals
self.shader.bind()
self.batch_active_normal.draw(self.shader)

else:
if self.draw_tris:
# Static Tris
self.shader.bind()
self.shader.uniform_float("viewProjectionMatrix", matrix)
self.shader.uniform_float("brightness", self.brightness)
# self.shader.uniform_float("opacity", self.opacity)
# self.shader.uniform_float("color", tri_color)
self.batch_tri.draw(self.shader)

# Static Points
self.point_shader.bind()
self.point_shader.uniform_float("viewProjectionMatrix", matrix)
Expand All @@ -353,21 +436,21 @@ def draw(self):
# self.point_shader.uniform_float("color", po_color)
self.batch_po.draw(self.point_shader)

# Static Normals
self.shader.bind()
self.shader.uniform_float("viewProjectionMatrix", matrix)
self.shader.uniform_float("brightness", self.brightness)
# self.shader.uniform_float("opacity", self.opacity)
# self.shader.uniform_float("color", line_color)
self.batch_normal.draw(self.shader)

# Active Normals
self.shader.bind()
self.shader.uniform_float("viewProjectionMatrix", matrix)
self.shader.uniform_float("brightness", self.brightness)
# self.shader.uniform_float("opacity", self.opacity)
# self.shader.uniform_float("color", line_color)
self.batch_active_normal.draw(self.shader)
# Static Normals
self.shader.bind()
self.shader.uniform_float("viewProjectionMatrix", matrix)
self.shader.uniform_float("brightness", self.brightness)
# self.shader.uniform_float("opacity", self.opacity)
# self.shader.uniform_float("color", line_color)
self.batch_normal.draw(self.shader)

# Active Normals
self.shader.bind()
self.shader.uniform_float("viewProjectionMatrix", matrix)
self.shader.uniform_float("brightness", self.brightness)
# self.shader.uniform_float("opacity", self.opacity)
# self.shader.uniform_float("color", line_color)
self.batch_active_normal.draw(self.shader)

return

Expand Down Expand Up @@ -398,6 +481,10 @@ def set_draw_only_selected(self, status):
self.draw_only_selected = status
return

def set_draw_weights(self, status):
self.draw_weights = status
return

def set_draw_tris(self, status):
self.draw_tris = status
return
Expand Down
Loading

0 comments on commit 34820a9

Please sign in to comment.