def get_matrix(self, node, parent=None, refresh=True): """ Return the matrix for transfomations applied to the passed node node - the Coin3D SoNode with the desired transformation parent - optional - sg root default refresh - if false, retrieves last matrix """ if not parent: parent = self.sg_root if not refresh: if self._matrix: return self._matrix #define the search path _search = coin.SoSearchAction() _search.setNode(node) _search.apply(parent) #get the matrix for the transformation _matrix = coin.SoGetMatrixAction(ViewState().viewport) _matrix.apply(_search.getPath()) self._matrix = coin.SbMatrix(_matrix.getMatrix().getValue()) return self._matrix
def get_path(self, node): _sa = coin.SoSearchAction() _sa.setNode(node) _sa.apply(ViewState().sg_root) return _sa
def get_transformed_coordinates(self): """ Return the transformed coordinates of the selected nodes based on the transformations applied by the drag tracker """ _coords = self.nodes['selected'].getChild(1) #define the search path if not defined if not self.start_path: _search = coin.SoSearchAction() _search.setNode(_coords) _search.apply(self.nodes['selected']) self.start_path = _search.getPath() #get the matrix for the transformation _matrix = coin.SoGetMatrixAction(self.viewport) _matrix.apply(self.start_path) _xf = _matrix.getMatrix() #create the 4D vectors for the transformation _vecs = [ coin.SbVec4f(_v.getValue() + (1.0,)) \ for _v in _coords.point.getValues() ] #multiply each coordinate by the transformation matrix and return #a list of the transformed coordinates, omitting the fourth value return [_xf.multVecMatrix(_v).getValue()[:3] for _v in _vecs]
def get_path(node): root = Gui.ActiveDocument.ActiveView.getSceneGraph() _sa = coin.SoSearchAction() _sa.setNode(node) _sa.apply(root) return _sa
def search(node, parent): """ Returns a search action """ _sa = coin.SoSearchAction() _sa.setNode(node) _sa.apply(parent) return _sa
def _findNodeIn(classTypeId, rootNode): sa = coin.SoSearchAction() sa.setType(classTypeId) sa.setSearchingAll(True) sa.apply(rootNode) if (sa.isFound()): return sa.getPath().getTail() else: return None
def searchForCamera(self, root): sa = coin.SoSearchAction() sa.setInterest(coin.SoSearchAction.FIRST) sa.setType(coin.SoCamera.getClassTypeId()) sa.apply(root) if sa.getPath(): node = sa.getPath().getTail() if node and node.isOfType(coin.SoCamera.getClassTypeId()): return node return None
def _getPickStyleNode(self, viewprovider, make_if_missing=True): from pivy import coin sa = coin.SoSearchAction() sa.setType(coin.SoPickStyle.getClassTypeId()) sa.traverse(viewprovider.RootNode) if sa.isFound() and sa.getPath().getLength() == 1: return sa.getPath().getTail() else: if not make_if_missing: return None pick_style = coin.SoPickStyle() pick_style.style.setValue(coin.SoPickStyle.SHAPE) viewprovider.RootNode.insertChild(pick_style, 0) return pick_style
def _getClipplaneNode(self, viewprovider, make_if_missing = True): from pivy import coin sa = coin.SoSearchAction() sa.setType(coin.SoClipPlane.getClassTypeId()) sa.setName('TVClipPlane') sa.traverse(viewprovider.RootNode) if sa.isFound() and sa.getPath().getLength() == 1: return sa.getPath().getTail() elif not sa.isFound(): if not make_if_missing: return None clipplane = coin.SoClipPlane() viewprovider.RootNode.insertChild(clipplane, 0) clipplane.setName('TVClipPlane') clipplane.on.setValue(False) #make sure the plane is not activated by default return clipplane
def is_in_3Dview(obj): """tests if the object has some 3d view representation""" try: viewprovider = obj.ViewObject from pivy import coin sa = coin.SoSearchAction() sa.setType(coin.SoSwitch.getClassTypeId()) sa.traverse(viewprovider.RootNode) if not (sa.isFound() and sa.getPath().getLength() == 1): return False n = sa.getPath().getTail().getNumChildren() return n > 0 except Exception as err: App.Console.PrintWarning( u"Show.TempoVis.isIn3DView error: {err}".format(err=str(err))) return True #assume.
def updateTransformedNormal(self): # // First get hold of an SoPath through the scenegraph down to the # // node ("mynode") you want to query about its current world space # // transformation(s). # # SoSearchAction * searchaction = new SoSearchAction; # searchaction->setNode(mynode); # searchaction->apply(myscenegraphroot); # # SoPath * path = searchaction->getPath(); # assert(path != NULL); # # // Then apply the SoGetMatrixAction to get the full transformation # // matrix from world space. # # const SbViewportRegion vpr = myviewer->getViewportRegion(); # SoGetMatrixAction * getmatrixaction = new SoGetMatrixAction(vpr); # getmatrixaction->apply(path); # # SbMatrix transformation = getmatrixaction->getMatrix(); # # // And if you want to access the individual transformation # // components of the matrix: # # SbVec3f translation; # SbRotation rotation; # SbVec3f scalevector; # SbRotation scaleorientation; # # transformation.getTransform(translation, rotation, scalevector, scaleorientation); searchaction = coin.SoSearchAction() searchaction.setNode(self) searchaction.apply( FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()) path = searchaction.getPath() vpr = FreeCADGui.ActiveDocument.ActiveView.getViewer( ).getViewportRegion() getmatrixaction = coin.SoGetMatrixAction(vpr) getmatrixaction.apply(path) transformation = getmatrixaction.getMatrix() self.transformedNormal = transformation.multVecMatrix(self.normal) self.calc.expression.set1Value( 2, "tA=vec3f(%f,%f,%f)" % (self.transformedNormal.getValue()[0], self.transformedNormal.getValue()[1], self.transformedNormal.getValue()[2])) return ()
def pick(self, cursorPos, obj): self.vp = obj.ViewObject pickAction = coin.SoRayPickAction(self._getViewport()) pickAction.setPickAll(True) pickAction.setRadius(Gui.activeView().getViewer().getPickRadius()) pickAction.setPoint(coin.SbVec2s(cursorPos)) path = self._getPathForVP(self.vp) sa = coin.SoSearchAction() sa.setNode(self.vp.RootNode) sa.apply(self._getSceneGraph()) pickAction.apply(sa.getPath()) for pp in pickAction.getPickedPointList(): node = pp.getPath().getTail() detail = self._castDetail(pp.getDetail()) self._updatePicked(node, detail) self._clearPreselect()
def is3DObject(obj): """is3DObject(obj): tests if the object has some 3d geometry. TempoVis is made only for objects in 3d view, so all objects that don't pass this check are ignored by TempoVis.""" # See "Gui Problem Sketcher and TechDraw" https://forum.freecadweb.org/viewtopic.php?f=3&t=22797 # observation: all viewproviders have transform node, then a switch node. If that switch node contains something, the object has something in 3d view. try: viewprovider = obj.ViewObject from pivy import coin sa = coin.SoSearchAction() sa.setType(coin.SoSwitch.getClassTypeId()) sa.traverse(viewprovider.RootNode) if not sa.isFound(): #shouldn't happen... return False n = sa.getPath().getTail().getNumChildren() return n > 0 except Exception as err: App.Console.PrintWarning(u"Show.TempoVis.isIn3DView error: {err}".format(err= str(err))) return True #assume.
def __init__(self, use_path=True): """docstring""" self.node = coin.SoSeparator() self.event_cb = coin.SoEventCallback() self.coordinate = coin.SoCoordinate3() self.marker = coin.SoMarkerSet() self.event_cb_switch = coin.SoSwitch() self.coordinate.point.setValue((0.0, 0.0, 0.0)) self.coordinate.setName('coordinate') self.event_cb.setName('event_cb') self.marker.setName('marker') self.cb_sigs = [ self.event_cb.addEventCallback( coin.SoLocation2Event.getClassTypeId(), self.on_mouse_cb), self.event_cb.addEventCallback( coin.SoMouseButtonEvent.getClassTypeId(), self.on_button_cb) ] self.event_cb_switch.addChild(self.event_cb) self.node.addChild(self.event_cb_switch) self.node.addChild(self.coordinate) self.node.addChild(self.marker) ViewState().sg_root.insertChild(self.node, 0) if not use_path: return _sa = coin.SoSearchAction() _sa.setNode(self.marker) _sa.apply(ViewState().sg_root) self.event_cb.setPath(_sa.getPath()) self.toggle_events()
def __init__(self, obj, sep, typ): super().__init__(obj) ## First child of ghost is SoTransform and has to be kept, second child ## is the actual container (SoSeparator) and will be replaced self.children = [self.children[0]] self.node = sep if typ is not 'normal': ## Create a SoAnnotation container to put the ghost on foreground self.node = SoAnnotation() self.node.addChild(sep) lineColor = selectionVisibility['LineColor'] faceColor = selectionVisibility['FaceColor'] transparency = selectionVisibility['Transparency'] lineTransparency = selectionVisibility['LineTransparency'] lineWidth = selectionVisibility['LineWidth'] faceLineWidth = selectionVisibility['FaceLineWidth'] search = coin.SoSearchAction() search.setInterest(search.ALL) search.setSearchingAll(True) SoBaseKit.setSearchingChildren(True) variations = { SoIndexedFaceSet: { 'transparency': transparency, 'lineWidth': faceLineWidth }, SoIndexedLineSet: { 'transparency': lineTransparency, 'lineWidth': lineWidth }, SoPointSet: { 'transparency': lineTransparency, 'lineWidth': lineWidth } } for sub_ob in variations: search.setType(sub_ob.getClassTypeId()) search.apply(sep) paths = search.getPaths() for path in paths: if path: parent = path.getNodeFromTail(1) for ch in parent.getChildren(): if ch.isOfType(SoMaterial.getClassTypeId()): ch.diffuseColor.getValues()[0].setValue( lineColor[typ]) ch.transparency.setValue( variations[sub_ob]['transparency'][typ]) ch.emissiveColor.getValues()[0].setValue( faceColor[typ]) if ch.isOfType(SoShapeHints.getClassTypeId()): ch.vertexOrdering.setValue(1) if ch.isOfType(SoDrawStyle.getClassTypeId()): ch.lineWidth.setValue( variations[sub_ob]['lineWidth'][typ]) self.children.append(self.node) Tracker.__init__(self, dotted=False, scolor=None, swidth=None, children=self.children, name="ghostTracker")
def _getPathForVP(self, vp): sa = coin.SoSearchAction() sa.setNode(vp.RootNode) sa.apply(self._getSceneGraph()) return sa.getPath()