-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
snippets/math/plane_coords_from_position_and_normal_vectors.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |