Skip to content

Instantly share code, notes, and snippets.

View SuddenDevelopment's full-sized avatar
🤖

SuddenDevelopment SuddenDevelopment

🤖
View GitHub Profile
@SuddenDevelopment
SuddenDevelopment / instance_matrix.js
Last active May 6, 2024 21:08
helper for an instance matrix using noise in three.js / react 3 fiber / web3d.
export function getInstanceMatrix(position, radius, spacing, objMap){
/*
const matrices = useMemo(() => {
return getInstanceMatrix([0, 0, 0], 10, 1, {
minNoise: -1,
maxNoise: 1,
getNoise: (x, y, z) => { return noise.GetNoise(x, y, z) }
});
}, [noise]); // Add any other dependencies here
@SuddenDevelopment
SuddenDevelopment / blender_instance.py
Created February 8, 2024 15:09
open a new instance of blender from python
import subprocess
import platform
# Path to the Blender executable
if platform.system() == "Windows":
blender_executable_path = "C:\\Program Files\\Blender Foundation\\Blender\\blender.exe"
elif platform.system() == "Linux":
blender_executable_path = "/usr/bin/blender"
else:
blender_executable_path = "/Applications/Blender.app/Contents/MacOS/Blender"
@SuddenDevelopment
SuddenDevelopment / addPip.py
Created January 22, 2024 15:52
pip install from inside Blender Scripting tab
import subprocess
import sys
import importlib.util
# RUN THIS INSIDE BLENDER SCRIPTING TAB
def addPip(strLibrary, reinstall=False):
if importlib.util.find_spec(strLibrary) is None:
# Ensure pip is installed
try:
@SuddenDevelopment
SuddenDevelopment / cameraMove.jsx
Created May 14, 2023 14:39
web3D react 3 fiber R3F move camera code
// all these hooks MUST be inside the react components
const cameraState = useContext(SceneContext);
useFrame(state => {
const vectorGo = new THREE.Vector3();
vectorGo.set(...cameraState.position);
const vector = new THREE.Vector3();
const vector2 = new THREE.Vector3();
vector.set(...cameraState.target);
vector2.set(...cameraState.target);
// const intDistance = 10.75;
@SuddenDevelopment
SuddenDevelopment / clearPath.py
Created May 12, 2023 14:22
In Blender Python test for a clear path between 2 points
def isClearPath(vert1, vert2, depsgraph):
# Test if there are any obstructions from vert 1 to vert 2 until it reaches obj2
# test for given clearance around the ray traced path with 6 parallel line a given distance of clearnace away from original path t test fo
# isClear = isClearPath(vert1, vert2, depsgraph)
# Calculate the direction vector from vert1 to vert2
direction_vector = vert2 - vert1
# Normalize the direction vector
direction = direction_vector.normalized()
@SuddenDevelopment
SuddenDevelopment / blender_object_profile.py
Created April 1, 2023 17:39
blender object 2d profile object python
def objTo2DProfile(obj, strName=None, strPosition="ORIGIN"):
if strName == None:
strName = f'{obj.name}_profile'
arrVerts = []
arrPolyVerts = []
for vert in obj.data.vertices:
arrVerts.append((vert.co.x, vert.co.y))
arrVertIndices = mathutils.geometry.convex_hull_2d(arrVerts)
for intVert in arrVertIndices:
arrPolyVerts.append([arrVerts[intVert][0], arrVerts[intVert][1], 0])
@SuddenDevelopment
SuddenDevelopment / diceObject.py
Created March 31, 2023 19:50
dice a blender object by its vertices
def diceObj(obj):
bm = getBMesh(obj)
# add one for both ends of a row because we want to start from the center
# hold all the planes to bisect with as (location), (normal)
arrPlanes = []
# get each X and Y from
# get verts by faces so we have a normal
for face in bm.faces:
for v in face.verts:
arrPlanes.append(
@SuddenDevelopment
SuddenDevelopment / facetheworld.py
Created March 9, 2023 18:26
blender python function to orient an object based on a face normal
def faceTheWorld(obj, strMode="EDIT"):
# Thanks to JohnKaz and Kritskiy on BPY Discord for this function, looks simple, but not easy to figure out!
mw = obj.matrix_world.copy()
bm = bmesh.new()
if strMode == 'OBJECT':
bm.from_mesh(obj.data)
else:
bm = bmesh.from_edit_mesh(obj.data)
face = next(iter(face for face in bm.faces if face.select), None)
@SuddenDevelopment
SuddenDevelopment / blender_batch_object_transforms.py
Last active March 2, 2023 20:19
a blender function to do a bunch of transforms and then update the view. so that view updates dont need to be run per object
def batchTransform(arrBatch, context=None, update_view=False):
# {
# object: string name_full,
# action: string = SCALE | ROTATE | ORIGIN | MOVE,
# value: (x,y,z), or string for parent or collection name
# mode: LOCAL=relative or adding, WORLD= world coordinates or setting value
# }
# return an array of issues, or empty array if all good.
arrErrors=[]
if context == None:
@SuddenDevelopment
SuddenDevelopment / grid_bisect_object.py
Last active March 1, 2023 20:26
knifecut a grid of squares into a belnder object with bisect plane
import bpy
import bmesh
import math
def diceObj(obj, intSize = 0.5):
arrDimensions = obj.dimensions
# add one for both ends of a row because we want to start from the center
intRowsX = math.floor((arrDimensions[0]/intSize) + 1)
intRowsY = math.floor((arrDimensions[1]/intSize) + 1)
bm = bmesh.new()
bm.from_mesh(obj.data)