Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Martinelli 5agado

View GitHub Profile
@5agado
5agado / randomize_camera_loc.py
Last active Jul 22, 2022
Synthetic-Data-Generation in Blender
View randomize_camera_loc.py
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
5agado / boolean_slicing.py
Created Jun 6, 2021
Blender boolean slicing
View boolean_slicing.py
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:
View gp_rotate_stroke.py
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)],
View gp_draw_line.py
def draw_line(gp_frame, p0: tuple, p1: tuple):
# Init new stroke
gp_stroke = gp_frame.strokes.new()
gp_stroke.display_mode = '3DSPACE' # allows for editing
# Define stroke geometry
gp_stroke.points.add(count=2)
gp_stroke.points[0].co = p0
gp_stroke.points[1].co = p1
return gp_stroke
View gp_draw_circle.py
def draw_circle(gp_frame, center: tuple, radius: float, segments: int):
# Init new stroke
gp_stroke = gp_frame.strokes.new()
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
gp_stroke.points.add(count=segments)
for i in range(segments):
@5agado
5agado / gp_animate.py
Last active Feb 15, 2021
Snippets for Grease Pencil Scripting Post
View gp_animate.py
import bpy
NUM_FRAMES = 30
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 = gp_layer.frames.new(frame*FRAMES_SPACING)
# do something with your frame
@5agado
5agado / grease_pencil_init.py
Last active May 5, 2022
Init method for Blender 2.8 Grease Pencil
View grease_pencil_init.py
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
5agado / sublime_text_gen.py
Created Jan 19, 2018
Sublime3 plugin for text-generation via REST API
View sublime_text_gen.py
import sublime
import sublime_plugin
import http.client
host = "127.0.0.1"
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
5agado / conway_2D.py
Created Jun 23, 2017
Code for reproducing a 2D Conway's Game of Life cellular automaton in Blender
View conway_2D.py
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
5agado / Pandas and Seaborn.ipynb
Created Feb 20, 2017
Data Manipulation and Visualization with Pandas and Seaborn — A Practical Introduction
View Pandas and Seaborn.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.