def __init__(self, name, parent): Node.__init__(self, name, parent) self._path = StringAttribute('filename', self) self._points = NumericAttribute('points', self) self._data = NumericAttribute('data', self) self._format = EnumAttribute('format', self) self._frame = NumericAttribute('frame', self) self._startFrame = NumericAttribute('startFrame', self) self._endFrame = NumericAttribute('endFrame', self) #self._geo = GeoAttribute('geo', self) self.addInputAttribute(self._path) self.addInputAttribute(self._points) self.addInputAttribute(self._data) self.addInputAttribute(self._format) self.addInputAttribute(self._frame) self.addInputAttribute(self._startFrame) self.addInputAttribute(self._endFrame) self._format.value().addEntry(0, 'pc2') self._format.value().addEntry(1, 'ascii') self._format.value().setCurrentIndex(0) self._setAttributeAllowedSpecializations(self._path, ['Path-write']) self._setAttributeAllowedSpecializations(self._points, ['Vec3Array']) self._setAttributeAllowedSpecializations(self._data, ['Float']) self._setAttributeAllowedSpecializations(self._frame, ['Float']) self._setAttributeAllowedSpecializations(self._startFrame, ['Float']) self._setAttributeAllowedSpecializations(self._endFrame, ['Float'])
class pcReader(Node): def __init__(self, name, parent): Node.__init__(self, name, parent) self.path = StringAttribute('filename', self) self.input = NumericAttribute('frame', self) self.output = NumericAttribute('points',self) self.addInputAttribute(self.input) self.addInputAttribute(self.path) self.addOutputAttribute(self.output) self._setAttributeAffect(self.input, self.output) self._setAttributeAffect(self.path, self.output) self._setAttributeAllowedSpecializations(self.input, ['Float']) self._setAttributeAllowedSpecializations(self.output, ['Vec3Array']) #self._pcLoader = pcLoader.pcLoader('/Users/sjt/Dev/pc2/cache.pc2') def update(self, attribute): path = self.path.value().stringValue() self._pcLoader = pcLoader.pcLoader(path) value1 = self.input.value().floatValueAt(0) points = self._pcLoader.get_frame(value1) imath_points = [] for point in points: imath_points.append(Imath.Vec3f(point[0],point[1],point[2])) self.output.outValue().setVec3Values(imath_points)
def __init__(self, name, parent): Node.__init__(self, name, parent) self.path = StringAttribute('filename', self) self.input = NumericAttribute('frame', self) self.output = NumericAttribute('points',self) self.addInputAttribute(self.input) self.addInputAttribute(self.path) self.addOutputAttribute(self.output) self._setAttributeAffect(self.input, self.output) self._setAttributeAffect(self.path, self.output) self._setAttributeAllowedSpecializations(self.input, ['Float']) self._setAttributeAllowedSpecializations(self.output, ['Vec3Array'])
def __init__(self, name, parent): Node.__init__(self, name, parent) self._fileName = StringAttribute("fileName", self) self._leftSpectrum = NumericAttribute("leftSpectrum", self) self._rightSpectrum = NumericAttribute("rightSpectrum", self) self._time = NumericAttribute("time", self) self._play = False self.addInputAttribute(self._fileName) self.addOutputAttribute(self._time) self.addOutputAttribute(self._leftSpectrum) self.addOutputAttribute(self._rightSpectrum) self._setAttributeAffect(self._fileName, self._time) self._setAttributeAffect(self._fileName, self._leftSpectrum) self._setAttributeAffect(self._fileName, self._rightSpectrum) self._setAttributeAllowedSpecializations(self._time, ["Float"]) self._setAttributeAllowedSpecializations(self._leftSpectrum, ["FloatArray"]) self._setAttributeAllowedSpecializations(self._rightSpectrum, ["FloatArray"]) self._setUpdateEnabled(False) self._leftSpectrum.outValue().resize(64) self._rightSpectrum.outValue().resize(64) SoundManager.init()
def __init__(self, name, parent): Node.__init__(self, name, parent) self.setClassName("ViewportCamera") self._modelMatrix = NumericAttribute("modelMatrix", self) self._fov = NumericAttribute("fov", self) self._zNear = NumericAttribute("zNear", self) self._zFar = NumericAttribute("zFar", self) self.addOutputAttribute(self._modelMatrix) self.addOutputAttribute(self._fov) self.addOutputAttribute(self._zNear) self.addOutputAttribute(self._zFar) self._setAttributeAllowedSpecializations(self._modelMatrix, ["Matrix44"]) self._setAttributeAllowedSpecializations(self._fov, ["Float"]) self._setAttributeAllowedSpecializations(self._zNear, ["Float"]) self._setAttributeAllowedSpecializations(self._zFar, ["Float"]) viewport.addCameraNode(self) self._extractViewportInfo()
class SimplePyNode(Node): def __init__(self, name, parent): Node.__init__(self, name, parent) self.input1 = NumericAttribute("input1", self) self.input2 = NumericAttribute("input2", self) self.output = NumericAttribute("output", self) self.addInputAttribute(self.input1) self.addInputAttribute(self.input2) self.addOutputAttribute(self.output) self._setAttributeAffect(self.input1, self.output) self._setAttributeAffect(self.input2, self.output) self._setAttributeAllowedSpecializations(self.input1, ["Float"]) self._setAttributeAllowedSpecializations(self.input2, ["Float"]) self._setAttributeAllowedSpecializations(self.output, ["Float"]) def update(self, attribute): value1 = self.input1.value().floatValueAt(0) value2 = self.input2.value().floatValueAt(0) self.output.outValue().setFloatValueAt(0, value1 + value2)
def __init__(self, name, parent): Node.__init__(self, name, parent) self.input1 = NumericAttribute("input1", self) self.input2 = NumericAttribute("input2", self) self.output = NumericAttribute("output", self) self.addInputAttribute(self.input1) self.addInputAttribute(self.input2) self.addOutputAttribute(self.output) self._setAttributeAffect(self.input1, self.output) self._setAttributeAffect(self.input2, self.output) self._setAttributeAllowedSpecializations(self.input1, ["Float"]) self._setAttributeAllowedSpecializations(self.input2, ["Float"]) self._setAttributeAllowedSpecializations(self.output, ["Float"])
class SoundStreamNode(Node): def __init__(self, name, parent): Node.__init__(self, name, parent) self._fileName = StringAttribute("fileName", self) self._leftSpectrum = NumericAttribute("leftSpectrum", self) self._rightSpectrum = NumericAttribute("rightSpectrum", self) self._time = NumericAttribute("time", self) self._play = False self.addInputAttribute(self._fileName) self.addOutputAttribute(self._time) self.addOutputAttribute(self._leftSpectrum) self.addOutputAttribute(self._rightSpectrum) self._setAttributeAffect(self._fileName, self._time) self._setAttributeAffect(self._fileName, self._leftSpectrum) self._setAttributeAffect(self._fileName, self._rightSpectrum) self._setAttributeAllowedSpecializations(self._time, ["Float"]) self._setAttributeAllowedSpecializations(self._leftSpectrum, ["FloatArray"]) self._setAttributeAllowedSpecializations(self._rightSpectrum, ["FloatArray"]) self._setUpdateEnabled(False) self._leftSpectrum.outValue().resize(64) self._rightSpectrum.outValue().resize(64) SoundManager.init() def isPlaying(self): return self._play def _advanceTime(self): leftSpectrum = self._leftSpectrum.outValue() rightSpectrum = self._rightSpectrum.outValue() timeVal = self._time.outValue() framesPerSecond = 24.0 timeStep = 1.0 / framesPerSecond enlapsedTime = 0.0 while self._play: enlapsedTime = enlapsedTime + timeStep SoundManager.setSpectrumOnNumeric(leftSpectrum, 0) SoundManager.setSpectrumOnNumeric(rightSpectrum, 1) self._leftSpectrum.valueChanged() self._rightSpectrum.valueChanged() timeVal.setFloatValueAt(0, enlapsedTime) self._time.valueChanged() time.sleep(timeStep) def play(self, value = True): self._play = value if self._play: SoundManager.load(self._fileName.value().stringValue()) SoundManager.play() thread.start_new_thread(self._advanceTime, ()) else: SoundManager.stop() self._time.outValue().setFloatValueAt(0, 0.0) self._time.valueChanged() def __del__(self): self._play = False SoundManager.terminate()
class ViewportCameraNode(Node): def __init__(self, name, parent): Node.__init__(self, name, parent) self.setClassName("ViewportCamera") self._modelMatrix = NumericAttribute("modelMatrix", self) self._fov = NumericAttribute("fov", self) self._zNear = NumericAttribute("zNear", self) self._zFar = NumericAttribute("zFar", self) self.addOutputAttribute(self._modelMatrix) self.addOutputAttribute(self._fov) self.addOutputAttribute(self._zNear) self.addOutputAttribute(self._zFar) self._setAttributeAllowedSpecializations(self._modelMatrix, ["Matrix44"]) self._setAttributeAllowedSpecializations(self._fov, ["Float"]) self._setAttributeAllowedSpecializations(self._zNear, ["Float"]) self._setAttributeAllowedSpecializations(self._zFar, ["Float"]) viewport.addCameraNode(self) self._extractViewportInfo() def __del__(self): viewport.removeCameraNode(self) def cameraChanged(self): # called by viewport.py # here we have to dirty each camera related attribute in order to cause an update self._extractViewportInfo() self._modelMatrix.valueChanged() self._fov.valueChanged() self._zNear.valueChanged() self._zFar.valueChanged() def _extractViewportInfo(self): viewports = viewport.instancedViewports() if viewports: vport = viewports[0] modelMatrix = vport.modelMatrix() self._modelMatrix.outValue().setMatrix44ValueAt(0, modelMatrix) fov = vport.fov() self._fov.outValue().setFloatValueAt(0, fov) zNear = vport.zNear() self._zNear.outValue().setFloatValueAt(0, zNear) zFar = vport.zFar() self._zFar.outValue().setFloatValueAt(0, zFar)
class WriteNode(Node): def __init__(self, name, parent): Node.__init__(self, name, parent) self._path = StringAttribute('filename', self) self._points = NumericAttribute('points', self) self._data = NumericAttribute('data', self) self._format = EnumAttribute('format', self) self._frame = NumericAttribute('frame', self) self._startFrame = NumericAttribute('startFrame', self) self._endFrame = NumericAttribute('endFrame', self) #self._geo = GeoAttribute('geo', self) self.addInputAttribute(self._path) self.addInputAttribute(self._points) self.addInputAttribute(self._data) self.addInputAttribute(self._format) self.addInputAttribute(self._frame) self.addInputAttribute(self._startFrame) self.addInputAttribute(self._endFrame) self._format.value().addEntry(0, 'pc2') self._format.value().addEntry(1, 'ascii') self._format.value().setCurrentIndex(0) self._setAttributeAllowedSpecializations(self._path, ['Path-write']) self._setAttributeAllowedSpecializations(self._points, ['Vec3Array']) self._setAttributeAllowedSpecializations(self._data, ['Float']) self._setAttributeAllowedSpecializations(self._frame, ['Float']) self._setAttributeAllowedSpecializations(self._startFrame, ['Float']) self._setAttributeAllowedSpecializations(self._endFrame, ['Float']) def update(self, attribute): pass def write(self): # first we check if something is connected to the frame attr if self._frame.input() is None: raise RuntimeError('A time node isn\'t connected to \'frame\'') time_node = self._frame.input().parent() # and is it a time node? if time_node.className() != 'Time': raise RuntimeError('Input of \'frame\' is not a Time node.') time_attr = time_node.findAttribute('time') # check the output path and range attrs output_path = self._path.value().stringValue() if output_path.strip() == '': raise RuntimeError('No output path specified.') start_frame = self._startFrame.value().floatValueAt(0) end_frame = self._endFrame.value().floatValueAt(0) if start_frame >= end_frame: raise RuntimeError('End is before start.') # create the folder for the output try: os.makedirs(os.path.dirname(output_path)) except os.error: pass except Exception, err: raise RuntimeError('Unable to create folder %s: %s' % (os.path.dirname(output_path), str(err))) selected_format = self._format.value().entries()[self._format.value().currentIndex()] if selected_format == 'pc2': # I learned how to write pc 2 caches in python from Matt Ebb's script for blender:http://mattebb.com/projects/bpython/pointcache/export_pc2.py import struct if self._points.input() is not None: num_points = len(self._points.value().vec3Values()) sample_rate = 1.0 # TODO:support various sample rates? num_samples = int(end_frame) - int(start_frame) pc2_path = os.path.splitext(output_path)[0] + '.pc2' f = open(pc2_path, 'wt') header = struct.pack('<12ciiffi', 'P','O','I','N','T','C','A','C','H','E','2','\0', 1, num_points, int(start_frame), sample_rate, num_samples) f.write(header) for i in range(int(start_frame), int(end_frame+1)): time_attr.value().setFloatValueAt(0,i) time_attr.forceDirty() for p in self._points.value().vec3Values(): point = struct.pack('<fff', p.x, p.y, p.z) f.write(point) f.flush() f.close() else: raise RuntimeError('No data to write!') elif selected_format == 'ascii': f = open(output_path, 'wt') ''' I decided to check what input to use from inside the loop because it makes things a bit clearer and a simple speed comparison show it isn't _that_ much of a slowdown. With 100000 frames (the points array was 10 elements) I got these write times: points: 14.098s (loop inside input check) vs 14.34s (check inside loop) float: 4.036 (loop inside input check) vs 4.55 (check inside loop) ''' for i in range(int(start_frame), int(end_frame+1)): time_attr.value().setFloatValueAt(0,i) time_attr.forceDirty() if self._points.input() is not None: for p in self._points.value().vec3Values(): f.write('<%f %f %f>, ' % (p.x, p.y, p.z)) f.write('\n') elif self._data.input() is not None: f.write('%s\n' % str(self._data.value().floatValueAt(0))) else: f.close() raise RuntimeError('No data to write!') f.close()