Skip to content

Commit

Permalink
add GP canvas matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
Pullusb committed Dec 11, 2024
1 parent 1780dcc commit d60feab
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
46 changes: 46 additions & 0 deletions snippets/gp/get_gp_drawing_plane_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Get Grease pencil object's canvas matrix

def get_gp_draw_plane_matrix(context):
'''return matrix representing the drawing plane of the grease pencil object'''

settings = context.scene.tool_settings
orient = settings.gpencil_sculpt.lock_axis # 'VIEW', 'AXIS_Y', 'AXIS_X', 'AXIS_Z', 'CURSOR'
loc = settings.gpencil_stroke_placement_view3d # 'ORIGIN', 'CURSOR', 'SURFACE', 'STROKE'
mat = context.object.matrix_world if context.object else None

draw_plane_mat = Matrix().to_3x3()

# -> placement
if loc == "CURSOR":
plane_co = context.scene.cursor.location
else: # ORIGIN (also on origin if set to 'SURFACE', 'STROKE')
if not context.object:
plane_co = None
else:
plane_co = context.object.matrix_world.to_translation() # context.object.location

if not plane_co:
return

# -> orientation
if orient == 'VIEW':
draw_plane_mat.rotate(context.space_data.region_3d.view_rotation)

elif orient == 'AXIS_Y': # front (X-Z) - Vector((0,1,0))
draw_plane_mat = Matrix.Rotation(math.radians(90), 3, 'X')
draw_plane_mat.rotate(mat)

elif orient == 'AXIS_X': # side (Y-Z) - Vector((1,0,0))
draw_plane_mat = Matrix.Rotation(math.radians(-90), 3, 'Y')
draw_plane_mat.rotate(mat)

elif orient == 'AXIS_Z': # top (X-Y) - Vector((0,0,1))
draw_plane_mat.rotate(mat)

elif orient == 'CURSOR':
draw_plane_mat.rotate(context.scene.cursor.matrix)

draw_plane_mat = draw_plane_mat.to_4x4()
draw_plane_mat.translation = plane_co

return draw_plane_mat
23 changes: 23 additions & 0 deletions snippets/math/plane_coords_from_position_and_normal_vectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Get a plane in world space from a coordiante and normal (create and apply a matrix)
## /!\ matrix will have unpredicted rotation along normal axis

plane_co, plane_no
## Define the half size of the plane

half_size = 1.0
## Calculate the corners of the plane in local space
local_corners = [
Vector((-half_size, -half_size, 0)),
Vector((half_size, -half_size, 0)),
Vector((half_size, half_size, 0)),
Vector((-half_size, half_size, 0))
]

# Calculate the orientation matrix from the plane normal
z_axis = plane_no.normalized()
x_axis = z_axis.cross(Vector((0, 1, 0))).normalized()
y_axis = x_axis.cross(z_axis).normalized()
orientation_matrix = Matrix((x_axis, y_axis, z_axis)).transposed()

# Transform the local corners to world space
coords = [orientation_matrix @ corner + plane_co for corner in local_corners]

0 comments on commit d60feab

Please sign in to comment.