I have found an awsome script by @Leander to do exactly what im trying to acheive here:
https://blender.stackexchange.com/a/109468/30363
which has two scripts, the first generates an object, the second script uses that generated object to extend its mesh until it hits another object in the scene, this then produces an object which looks like a laser plane scan.
I have managed to get the 1st script to run with blender 2.83.1 by updating the python code, using this https://blenderartists.org/t/2-80-cheat-sheet-for-updating-add-ons/1148974 as a guide,
import bpy
import bmesh
from mathutils.bvhtree import BVHTree
import mathutils
import math
import numpy as np
EPSILON = 0.00001
MAXIMUM_DISTANCE = 10
object_name = 'light_plane'
radius_start = math.pi*1/4
radius_end = math.pi*3/4
radius_step = math.pi/256
angles = [(math.cos(a), math.sin(a)) for a in np.arange(radius_start, radius_end + 0.00001, radius_step)]
def create_mesh_obj(bm, p_name = "from_bmesh"):
scn = bpy.context
me = bpy.data.meshes.new(name = p_name)
bm.to_mesh(me)
if (p_name not in bpy.data.objects):
ob = bpy.data.objects.new(name = p_name, object_data = me)
scn.collection.objects.link(ob)
else:
ob = bpy.data.objects[p_name]
ob.data = me
scn.view_layer.update()
return ob
def create_vertice_shapes(angles):
bm = bmesh.new()
for a in angles:
bm.verts.new((a[0], a[1], 0))
bm.verts.new((0, 0, 0))
ob = create_mesh_obj(bm)
bm.free()
# basis = ob.shape_key_add(from_mix=False)
# basis.name = "basis"
return ob
ob = create_vertice_shapes(angles)
ob.name = object_name
but i have had no such luck with the second script.
import bpy
import bmesh
from mathutils.bvhtree import BVHTree
import mathutils
import math
import numpy as np
EPSILON = 0.00001
MAXIMUM_DISTANCE = 5
object_name = 'light_plane'
radius_start = math.pi*1/4
radius_end = math.pi*3/4
radius_step = math.pi/256
angles = [(math.cos(a), math.sin(a)) for a in np.arange(radius_start, radius_end + 0.00001, radius_step)]
print(angles)
def set_positions(ob, angles, obstacles):
trees = []
for obstacle in obstacles:
bm = bmesh.new()
bm.from_object(obstacle, bpy.context.scene)
bmesh.ops.transform(bm, matrix=obstacle.matrix_world, verts=bm.verts)
trees.append(BVHTree.FromBMesh(bm, epsilon=EPSILON))
for idx, a in enumerate(angles):
min_dist = MAXIMUM_DISTANCE
v_loc = mathutils.Vector((a[0], a[1], 0)) * MAXIMUM_DISTANCE
for bvh in trees:
v = (ob.matrix_world * mathutils.Vector((a[0], a[1], 0))) - ob.location
loc, no, i, d = bvh.ray_cast(ob.location, v)
if d is not None:
if d < min_dist:
min_dist = d
v_loc = ob.matrix_world.inverted() * loc
ob.data.vertices[idx].co = v_loc
def my_handler(scene):
ob = bpy.data.objects[object_name]
selection = [oo for oo in bpy.data.objects if oo != ob]
print(ob.location)
set_positions(ob, angles, selection)
for h in bpy.app.handlers.depsgraph_update_pre:
bpy.app.handlers.scene_update_pre.remove(h)
for h in bpy.app.handlers.frame_change_pre:
bpy.app.handlers.frame_change_pre.remove(h)
#bpy.app.handlers.scene_update_pre.append(my_handler)
#bpy.app.handlers.frame_change_pre.append(my_handler)
my_handler(bpy.context.scene)
The error i get is "TypeError: expected 'Depsgraph' type found 'Scene' instead" highlighted on line 24
bm.from_object(obstacle, bpy.context.scene)
Can anyone offer some assistance?