-
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
102 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,65 @@ | ||
## Get viewport's current view vector or camera view vector | ||
|
||
import bpy | ||
from mathutils import Vector | ||
|
||
# Standalone functions | ||
|
||
def get_viewport_view_vector(): | ||
'''return current viewport view vector (normalized direction)''' | ||
view_vector = Vector((0,0,-1)) | ||
view_vector.rotate(bpy.context.space_data.region_3d.view_rotation) | ||
return view_vector | ||
|
||
def get_camera_view_vector(): | ||
'''return active camera view vector (normalized direction) | ||
return None if no active camera | ||
''' | ||
view_vector = Vector((0,0,-1)) | ||
if not bpy.context.scene.camera: | ||
return | ||
view_vector.rotate(bpy.context.scene.camera.matrix_world) | ||
return view_vector | ||
|
||
|
||
# Mixed function (with camera as option) | ||
|
||
def get_view_vector(camera=False): | ||
'''return current viewport view vector (normalized direction) | ||
camera: if True return active camera view vector (None if no active camera) | ||
''' | ||
view_vector = Vector((0,0,-1)) | ||
if camera: | ||
if not bpy.context.scene.camera: | ||
return | ||
view_vector.rotate(bpy.context.scene.camera.matrix_world) | ||
|
||
else: | ||
view_vector.rotate(bpy.context.space_data.region_3d.view_rotation) | ||
|
||
return view_vector | ||
|
||
## ---- | ||
|
||
## For camera | ||
cam_view_vec = get_camera_view_vector() | ||
|
||
|
||
## For view vector | ||
## In view 3D context | ||
# view_vec = get_view_vector() | ||
# view_vec = get_viewport_view_vector() | ||
|
||
## Here with context override for use from text editor | ||
def get_viewport_override(): | ||
for window in bpy.context.window_manager.windows: | ||
screen = window.screen | ||
for area in screen.areas: | ||
if area.type == 'VIEW_3D': | ||
return {'window': window, 'screen': screen, 'area': area} | ||
|
||
with bpy.context.temp_override(**get_viewport_override()): | ||
view_vec = get_view_vector() | ||
|
||
print("camera view vector:", cam_view_vec) | ||
print("view vector:", view_vec) |
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,37 @@ | ||
## Rotate object by some angle on defined axis | ||
## if angle value is in degree, use: math.radians(angle) | ||
|
||
import bpy | ||
from math import pi, radians | ||
from mathutils import Matrix, Vector | ||
|
||
def rotate_on_axis(obj, axis, angle): | ||
'''Rotate passed object on axis by an angle | ||
obj: Object to rotate | ||
axis: an axis as string, ex: "X", or a Vector, ex: Vector((0,0,1)) | ||
angle: An angle value in radians (signed) | ||
''' | ||
|
||
## Create rotation matrix | ||
rot_matrix = Matrix.Rotation(angle, 4, axis) | ||
## create a working copy of current matrix | ||
mat = obj.matrix_world.copy() | ||
|
||
## Reset translation, rotate, re-apply translation | ||
mat.translation = Vector((0,0,0)) | ||
mat = rot_matrix @ mat | ||
mat.translation = obj.matrix_world.translation | ||
|
||
# Assign new rotated matrix | ||
obj.matrix_world = mat | ||
|
||
|
||
## Rotate active object by 90 degrees (in radians: pi/2)... | ||
|
||
## ... on global Z axis | ||
rotate_on_axis(bpy.context.object, "Z", pi/2) | ||
|
||
## ... on current view axis (need to be executed in viewport context, using a context override from script editor) | ||
# view_vector = Vector((0,0,-1)) | ||
# view_vector.rotate(bpy.context.space_data.region_3d.view_rotation) | ||
# rotate_on_axis(bpy.context.object, view_vector, pi/2) |