1from pivy import coin 2 3 4def add_marker_from_svg(file_path, marker_name, pixel_x=10, pixel_y=None, 5 isLSBFirst=False, isUpToDown=False): 6 """adds a new marker bitmap from a vector graphic (svg)""" 7 8 # get an icon from the svg rendered with the given pixel 9 from PySide2 import QtCore, QtGui 10 pixel_y = pixel_y or pixel_x 11 icon = QtGui.QIcon(file_path) 12 icon = QtGui.QBitmap(icon.pixmap(pixel_x, pixel_y)) 13 14 # create a XMP-icon 15 buffer=QtCore.QBuffer() 16 buffer.open(buffer.WriteOnly) 17 icon.save(buffer,"XPM") 18 buffer.close() 19 20 # get a string from the XMP-icon 21 ary = str(buffer.buffer(), "utf8") 22 ary = ary.split("\n", 1)[1] 23 ary = ary.replace('\n', "").replace('"', "").replace(";", "") 24 ary = ary.replace("}", "").replace("#", "x").replace(".", " ") 25 string = str.join("", ary.split(",")[3:]) 26 27 # add the new marker style 28 setattr(coin.SoMarkerSet, marker_name, coin.SoMarkerSet.getNumDefinedMarkers()) 29 coin.SoMarkerSet.addMarker(getattr(coin.SoMarkerSet, marker_name), 30 coin.SbVec2s([pixel_x, pixel_y]), string, 31 isLSBFirst, isUpToDown) 32 33 34def get_point_on_screen(render_manager, screen_pos, normal="camera", point=None): 35 """get coordinates from pixel position""" 36 37 pCam = render_manager.getCamera() 38 vol = pCam.getViewVolume() 39 40 point = point or coin.SbVec3f(0, 0, 0) 41 42 if normal == "camera": 43 plane = vol.getPlane(10) 44 normal = plane.getNormal() 45 elif normal == "x": 46 normal = coin.SbVec3f(1, 0, 0) 47 elif normal == "y": 48 normal = coin.SbVec3f(0, 1, 0) 49 elif normal == "z": 50 normal = coin.SbVec3f(0, 0, 1) 51 normal.normalize() 52 x, y = screen_pos 53 vp = render_manager.getViewportRegion() 54 size = vp.getViewportSize() 55 dX, dY = size 56 57 fRatio = vp.getViewportAspectRatio() 58 pX = float(x) / float(vp.getViewportSizePixels()[0]) 59 pY = float(y) / float(vp.getViewportSizePixels()[1]) 60 61 if (fRatio > 1.0): 62 pX = (pX - 0.5 * dX) * fRatio + 0.5 * dX 63 elif (fRatio < 1.0): 64 pY = (pY - 0.5 * dY) / fRatio + 0.5 * dY 65 66 plane = coin.SbPlane(normal, point) 67 line = coin.SbLine(*vol.projectPointToLine(coin.SbVec2f(pX,pY))) 68 pt = plane.intersect(line) 69 return pt