Skip to content

Instantly share code, notes, and snippets.

View 5agado's full-sized avatar

Alex Martinelli 5agado

View GitHub Profile
5agado /
Last active July 22, 2022 16:41
Synthetic-Data-Generation in Blender
import bpy
from numpy.random import uniform
def rand_camera_loc(camera, xloc_range, yloc_range, zloc_range):
""" Randomize camera location for each axis based on the provided ranges """
new_loc = camera.location
new_loc[0] = uniform(low=xloc_range[0], high=xloc_range[1])
new_loc[1] = uniform(low=yloc_range[0], high=yloc_range[1])
new_loc[2] = uniform(low=zloc_range[0], high=zloc_range[1])
camera.location = new_loc
5agado /
Created June 6, 2021 23:46
Blender boolean slicing
import numpy as np
import bpy
from mathutils import Vector
from math import sin, cos, pi, copysign
def randomize_pos(obj, min, max):
axis = np.random.randint(0, 3)
new_axis_pos = np.random.uniform(min, max)
# x axis
if axis == 0:
from math import sin, cos
def rotate_stroke(stroke, angle, axis='z'):
# Define rotation matrix based on axis
if axis.lower() == 'x':
transform_matrix = np.array([[1, 0, 0],
[0, cos(angle), -sin(angle)],
[0, sin(angle), cos(angle)]])
elif axis.lower() == 'y':
transform_matrix = np.array([[cos(angle), 0, -sin(angle)],
def draw_line(gp_frame, p0: tuple, p1: tuple):
# Init new stroke
gp_stroke =
gp_stroke.display_mode = '3DSPACE' # allows for editing
# Define stroke geometry
gp_stroke.points[0].co = p0
gp_stroke.points[1].co = p1
return gp_stroke
def draw_circle(gp_frame, center: tuple, radius: float, segments: int):
# Init new stroke
gp_stroke =
gp_stroke.display_mode = '3DSPACE' # allows for editing
gp_stroke.draw_cyclic = True # closes the stroke
# Define stroke geometry
angle = 2*math.pi/segments # angle in radians
for i in range(segments):
5agado /
Last active February 15, 2021 08:35
Snippets for Grease Pencil Scripting Post
import bpy
FRAMES_SPACING = 1 # distance between frames
bpy.context.scene.frame_start = 0
bpy.context.scene.frame_end = NUM_FRAMES*FRAMES_SPACING
for frame in range(NUM_FRAMES):
gp_frame =*FRAMES_SPACING)
# do something with your frame
5agado /
Last active May 5, 2022 11:03
Init method for Blender 2.8 Grease Pencil
import bpy
def get_grease_pencil(gpencil_obj_name='GPencil') -> bpy.types.GreasePencil:
Return the grease-pencil object with the given name. Initialize one if not already present.
:param gpencil_obj_name: name/key of the grease pencil object in the scene
# If not present already, create grease pencil object
if gpencil_obj_name not in bpy.context.scene.objects:
5agado /
Created January 19, 2018 14:52
Sublime3 plugin for text-generation via REST API
import sublime
import sublime_plugin
import http.client
host = ""
port = "9000"
conn = http.client.HTTPConnection("{}:{}".format(host, port))
class TextGenCommand(sublime_plugin.TextCommand):
def run(self, edit, model_id, min_nb_words=5):
5agado /
Created June 23, 2017 12:43
Code for reproducing a 2D Conway's Game of Life cellular automaton in Blender
import bpy
import sys
from mathutils import Vector
import numpy as np
class ConwayGOL_2D:
def __init__(self, N):
2D Conway Game of Life
:param N: grid side size (resulting grid will be a NxN matrix)
5agado / Pandas and Seaborn.ipynb
Created February 20, 2017 13:33
Data Manipulation and Visualization with Pandas and Seaborn — A Practical Introduction
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.