import bpy
import numpy as np
[docs]
class LaserShotStep:
def __init__(self) -> None:
self.dx = 0.0
self.dy = 0.0
self.dz = 0.0
self.rx = 0.0
self.ry = 0.0
self.rz = 0.0
[docs]
class UserProcess:
def __init__(self, *, ilspDomain=None, processSteps=None):
self._ilspDomain = ilspDomain
self.path2userProcess(processSteps)
self._processStepIdx = 0
self._coords = None
self._normals = None
self._ilspDomain.animation_data_create()
self._ilspDomain.animation_data.action = \
bpy.data.actions.new(name="LSP Processing")
self._fcurveRx = self._ilspDomain.animation_data.action.fcurves.new(
data_path='rotation_euler', index=0)
self._fcurveRy = self._ilspDomain.animation_data.action.fcurves.new(
data_path='rotation_euler', index=1)
self._fcurveRz = self._ilspDomain.animation_data.action.fcurves.new(
data_path='rotation_euler', index=2)
self._fcurveDx = self._ilspDomain.animation_data.action.fcurves.new(
data_path='location', index=0)
self._fcurveDy = self._ilspDomain.animation_data.action.fcurves.new(
data_path='location', index=1)
self._fcurveDz = self._ilspDomain.animation_data.action.fcurves.new(
data_path='location', index=2)
self._rx = 0
self._ry = 0
self._rz = 0
self._dx = bpy.context.scene.cursor.location[0]
self._dy = bpy.context.scene.cursor.location[1]
self._dz = bpy.context.scene.cursor.location[2]
[docs]
def path2userProcess(self, pathSteps):
self._processSteps = []
location = np.array([0, 0, 0])
time = 0.0
timeRemainingFromPreviousStep = 0.0
for step in pathSteps:
stepDeltaLocation = np.array([step.dx, step.dy, step.dz])
stepPathLength = np.linalg.norm(stepDeltaLocation)
if stepPathLength > 0.0:
endStepPathTime = pathLength / step.velocity
shotTime = 1.0 / step.frequency
ds *= step.velocity * shotTime / pathLength
while time <= pathTime:
newStep = LaserShotStep()
if time > 0.0:
newStep.dx = ds[0]
newStep.dy = ds[1]
newStep.dz = ds[2]
self._processSteps.append(newStep)
time += shotTime
remainingTime = pathTime - (time - shotTime)
print(remainingTime)
[docs]
def update(self):
try:
processStep = self._processSteps[self._processStepIdx]
except Exception:
return False
self._rx += processStep.rx
self._ry += processStep.ry
self._rz += processStep.rz
self._dx += processStep.dx
self._dy += processStep.dy
self._dz += processStep.dz
k = self._fcurveRx.keyframe_points.insert(
frame=self._processStepIdx + 1, value=self._rx)
k.interpolation = 'CONSTANT'
k = self._fcurveRy.keyframe_points.insert(
frame=self._processStepIdx + 1, value=self._ry)
k.interpolation = 'CONSTANT'
k = self._fcurveRz.keyframe_points.insert(
frame=self._processStepIdx + 1, value=self._rz)
k.interpolation = 'CONSTANT'
k = self._fcurveDx.keyframe_points.insert(
frame=self._processStepIdx + 1, value=self._dx)
k.interpolation = 'CONSTANT'
k = self._fcurveDy.keyframe_points.insert(
frame=self._processStepIdx + 1, value=self._dy)
k.interpolation = 'CONSTANT'
k = self._fcurveDz.keyframe_points.insert(
frame=self._processStepIdx + 1, value=self._dz)
k.interpolation = 'CONSTANT'
bpy.context.scene.frame_set(self._processStepIdx + 1)
self._coords = []
for vertice in self._ilspDomain.data.vertices:
self._coords.append(self._ilspDomain.matrix_world @ vertice.co)
self._normals = []
matrix_new = self._ilspDomain.matrix_world.to_3x3().inverted().transposed()
for polygon in self._ilspDomain.data.polygons:
unitNormal = matrix_new @ polygon.normal
unitNormal.normalize()
self._normals.append(unitNormal)
self._processStepIdx += 1
return True
@property
def ilspDomain(self):
return self._ilspDomain
@property
def processSteps(self):
return self._processSteps
@property
def stepIndex(self):
return self._processStepIdx
@property
def coords(self):
return self._coords
@property
def normals(self):
return self._normals