bnp.objects.base

batch_identity(batch_num, size, dtype=np.float32) → np.ndarray

Return batched identity matrices

Parameters:
  • batch_num (int) – number of the batch
  • size (int) – size of each matrix
  • dtype – dtype
Returns:

np.ndarray (batch_num, size, size)

vec2np(vec, dtype=np.float32) → np.ndarray

Convert a vector such as mathutils.Vector to np.ndarray.

Parameters:
  • vec – vector (mathutils.Vector or np.ndarray)
  • dtype – dtype
Returns:

np.ndarray (vector_size)

mat2np(mat, dtype=np.float32) → np.ndarray

Convert a matrix such as mathutils.Matrix to np.ndarray.

Parameters:
  • vec – matrix (mathutils.Matrix or np.ndarray)
  • dtype – dtype
Returns:

Row-major np.ndarray (matrix_size)

vertices2np(vertices, dtype=np.float32) → np.ndarray

Convert vertices (bpy.types.bpy_prop_collection or bmesh.types.BMVertSeq) to np.ndarray

Parameters:
  • verticesbpy.types.bpy_prop_collection or bmesh.types.BMVertSeq
  • dtype – dtype
Returns:

np.ndarray (vtx_num, 3)

collection2np(obj, dtype=np.float32) → np.ndarray

Convert vertices (bpy.types.bpy_prop_collection) to np.ndarray

Parameters:
  • objbpy.types.bpy_prop_collection
  • dtype – dtype
Returns:

np.ndarray (vtx_num, 3)

world_matrix2np(obj, dtype=np.float32, frame=bpy.context.scene.frame_current) → np.ndarray

Get world matrix of bpy.types.Object as np.ndarray (row major). This function is equal to mat2np(obj.matrix_world).

Parameters:
  • obj (bpy.types.Object) – object
  • dtype – dtype
  • frame (int) – frame when you want to read (default: current frame)
Returns:

np.ndarray (worldmatrix; row major)

location2np(obj, dtype=np.float32, to_matrix=False, frame=bpy.context.scene.frame_current) → np.ndarray

Get location of bpy.types.Object as np.ndarray.

Parameters:
  • obj (bpy.types.Object) – object
  • dtype – dtype
  • to_matrix (bool) – whether to convert a location vector to a translation matrix
  • frame (int) – frame when you want to read (default: current frame)
Returns:

np.ndarray (4, 4) if to_matrix else (3)

rotation2np(obj, dtype=np.float32, to_matrix=False, frame=bpy.context.scene.frame_current, convert_axis_4to3=False) → np.ndarray

Get rotation of bpy.types.Object as np.ndarray.

Parameters:
  • obj (bpy.types.Object) – object
  • dtype – dtype
  • to_matrix (bool) – whether to convert a rotation vector to a translation matrix
  • frame (int) – frame when you want to read (default: current frame)
  • convert_axis_4to3 (bool) – whether to convert the dimension of axis angles from 4 to 3 (same as SMPL context)
Returns:

np.ndarray (4, 4) if to_matrix else {(3) (euler angle) or (4) (quaternion or axis angle)}

scale2np(obj, dtype=np.float32, to_matrix=False, frame=bpy.context.scene.frame_current) → np.ndarray

Get scale of bpy.types.Object as np.ndarray.

Parameters:
  • obj (bpy.types.Object) – object
  • dtype – dtype
  • to_matrix (bool) – whether to convert a scale vector to a translation matrix
  • frame (int) – frame when you want to read (default: current frame)
Returns:

np.ndarray (4, 4) if to_matrix else (3)

normalize_quaternion(q, eps=1e-10) → np.ndarray

Normalize input quaternions

Parameters:
  • np.ndarray – quaternion: (4) or (num_of_quaternion, 4)
  • eps (float) – epsilon to avoid zero-division
Returns:

np.ndarray normalized quaternions (1, 4) or (num_of_quaternion, 4)

normalize_axis_angle(a, eps=1e-10) → np.ndarray

Normalize input axis angles

Parameters:
  • np.ndarray – axis angles: (4) or (num_of_quaternion, 4)
  • eps (float) – epsilon to avoid zero-division
Returns:

np.ndarray normalized axis angles (1, 4) or (num_of_axis_angles, 4)

axis_angle_4to3(a, eps=1e-10) → np.ndarray

Convert the dimension of axis angles from 4 to 3 (same as SMPL context)

Parameters:
  • np.ndarray – axis angles: (4) or (num_of_quaternion, 4)
  • eps (float) – epsilon to avoid zero-division
Returns:

np.ndarray normalized axis angles (1, 3) or (num_of_axis_angles, 3)

axis_angle_3to4(a, eps=1e-10) → np.ndarray

Convert the dimension of axis angles from 3 to 4 (same as SMPL context)

Parameters:
  • np.ndarray – axis angles: (3) or (num_of_quaternion, 3)
  • eps (float) – epsilon to avoid zero-division
Returns:

np.ndarray normalized axis angles (1, 4) or (num_of_axis_angles, 4)

quaternion2R(q, dtype=np.float32, eps=1e-10) → np.ndarray

Convert quaternions to rotation matrices

Parameters:
  • q (np.ndarray) – quaternion (num_of_quaternion, 4)
  • dtype – dtype
  • eps (float) – epsilon to avoid zero-division
Returns:

np.ndarray rotation matrices (num_of_quaternion, 4, 4)

axis_angle2R(a, dtype=np.float32, eps=1e-10) → np.ndarray

Convert axis angles to rotation matrices

Parameters:
  • a (np.ndarray) – axis angles (num_of_axis_angles, 4)
  • dtype – dtype
  • eps (float) – epsilon to avoid zero-division
Returns:

np.ndarray rotation matrices (num_of_axis_angles, 4, 4)

euler2R(e, dtype=np.float32, eps=1e-10) → np.ndarray

Convert euler angles to rotation matrices

Parameters:
  • e (np.ndarray) – euler angles (num_of_euler_angles, 3)
  • dtype – dtype
  • eps (float) – epsilon to avoid zero-division
Returns:

np.ndarray rotation matrices (num_of_euler_angles, 4, 4)

change_rotation_mode(obj, rotation_mode, normalized=True)

Change current rotation mode of obj

Parameters:
  • obj (bpy.types.Object) – object
  • rotation_mode (str) – “rotation_axis_angle” (equal to “AXIS_ANGLE”), “rotation_quaternion” (equal to “QUATERNION”) or “rotation_euler” (equal to “XYZ”)
  • normalized (bool) – whether to normalize axis_angle or quaternion
get_keyframe_list(obj)

Get a sorted list which contains keyframes of obj. If there is no keyframes, return an empty list.

Parameters:obj (bpy.types.Object) – obj
Returns:list sorted with keyframes (no duplication)
insert_keyframe(obj, vec: np.ndarray, datapath: str, frame=bpy.context.scene.frame_current) → np.ndarray

Insert keyframe to datapath in the frame

Parameters:
  • obj (bpy.types.Object) – obj
  • vec (np.ndarray) – location: (3), “rotation”: (4) or (3) (radian), “scale”: (3)
  • datapath (str) – “location”, “rotation”, “scale”, “rotation_euler”, “rotation_quaternion” or “rotation_axis_angle”. In “rotation”, this method inserts the vec to current rotation mode.
  • frame (int) – frame
remove_keyframe(obj, frame)

Remove the specified keyframe from obj

Parameters:
  • objbpy.types.Object or bpy.types.PoseBone
  • frame (int) – the frame
remove_keyframes(obj, frames)

Remove the specified keyframes from obj

Parameters:
  • objbpy.types.Object or bpy.types.PoseBone
  • frames (list) – frame list