How to get the vertex that I clicked on OCC widget? - python

I use OCC with python to visualize .igs and .stl format. In .stl file I have a mesh on my model and I want to know what vertex on this mesh was clicked. At least to get some kind of id. I see that the model that I choose automatically highlights without any settings, so I guess there is a way to do this. But I couldn’t find any information about it.

Okay, found it. In case someone else will need it:
display = self.occWidget._display
display.SetSelectionModeVertex() # This is the required function
display.register_select_callback(recognize_clicked)
where recognize_clicked is
def recognize_clicked(shp, *kwargs):
""" This is the function called every time
a face is clicked in the 3d view
"""
for shape in shp:
print("Face selected: ", shape)
Face selection - SetSelectionModeFace()
Vertex selection - SetSelectionModeVertex()
Edge selection - SetSelectionModeEdge()
Shape selection - SetSelectionModeShape()
Neutral (default) selection - SetSelectionModeNeutral()
That is all the modes that I've found in other examples. Please, if you find more, write in a comment that resource.

Related

Display Harfang 3D physics debug with RenderCollision?

I can't get RenderCollision to work, no matter how I try.
The documentation says :
RenderCollision (view_id: int, vtx_layout: VertexLayout, prg: ProgramHandle, render_state: RenderState, depth: int) -> None
Here's my (limited) understanding of what I should pass as parameters to this function :
view_id : can be set from 0 to 255, according to the doc. In my case, it is 0
vtx_layout : the vertex layout to store 3D lines
ProgramHandle : the program (shader) needed to draw 3D lines
RenderState : something I'm supposed to provide using ComputeRenderState (found it here)
depth : something relative to the zDepth, I guess?
At this point, I feel I'm not far from using it properly, but I'm having a hard time to figure out the RenderState thing.
Anyone been there before?
RenderCollision is a debug function, so it won't "consume" any view_id. Indeed, you can pass it the view_id, it will write into the current view.
vtx_layout and prg, as you guessed it, handle the rendering of the debug lines (RenderCollision is using lines to draw the collision shapes).
It usually works this way:
Avoid clearing the view when drawing the debug info
hg.SetViewClear(view_id, hg.CF_None, 0, 1.0, 0)
Set the rect of the current view (the same as your main rendering)
hg.SetViewRect(view_id, 0, 0, screen_size_x, screen_size_y)
Set the camera transformation matrix (the same as your current camera)
hg.SetViewTransform(view_id, view_matrix, projection_matrix)
This is the one you were probably looking at: BM_Opaque will let know Harfang where you want to specifically draw within the rendering pipeline:
render_state = hg.ComputeRenderState(hg.BM_Opaque, hg.DT_Disabled, hg.FC_Disabled)
Final instruction that will draw the collision shapes:
physics.RenderCollision(view_id, vtx_line_layout, line_shader, render_state , 0)
You will find a working example here, I hope it will help:
https://github.com/harfang3d/tutorials-hg2/blob/master/physics_overrides_matrix.py#L69

PyQt - remove lines and polygons from dropdown menu when using QgsMapToolIdentify

I am developing a tool in QGIS 3.16.2 and python 3.7 where i want to make a tool where i can identify any arbitrary point on the map canvas layer, which i already achieved. But when i select any point on the map canvas i want the drop down to only show points and not line and polygons too (see pictures further down).
The code i used to make map identify tool working is as follows:
def choose_a_point(self):
self.canvas = self.iface.mapCanvas()
self.identify_vector = QgsMapToolIdentify(self.canvas)
self.identify_vector.canvasReleaseEvent = self.store_m_value
self.canvas.setMapTool(self.identify_vector)
# Execute function that stores identified point info
def store_m_value(self, event):
result = self.identify_vector.identify(event.x(), event.y(), [], QgsMapToolIdentify.DefaultQgsSetting)
# Check if the selected feature is a point!
if result[0].mFeature.geometry().type() == QgsWkbTypes.PointGeometry:
if result:
pass # Do something useful here
When i click on a point in the map canvas i get following dropdown (seams like this function is built in to the QgsMapToolIdentify:
But i want to get the following result, so the lines and polygons are removed from the dropdown list:
I tried looking at the documentation for QgsMapToolIdentify on this page QT documentation - , but with no luck.
Hope someone can help me fix this problem.

Need help creating a code that instances an object along a surface evenly

I'm pretty new to coding and I need to create a code that generates an object along the normal of a selected geo surface.
Anything to start me on the right path would be appreciated.
I initially tried to use duplicate, but I was told that command instancer is the better option.
def ChainmailSurfaceGenerator():
thing = MC.ls(sl=True)
print thing
if not thing:
MC.error("select a torus")
#create a group for your chainmail*
grp = MC.group(empty=True, name=thing[0] + '_grp#'
#create hierarchy of grouped toruses
MC.polyTorus( radius=1, n = 'chainmail_link')
MC.duplicate('chainmail_link')
#query the direction of UV normals off the face
#lessen the amount of geometry if its too dense
#try it out on a plane surface first
#keep things simple early
#what if chains don't connect perfectly? add a randomizer so that the chains aren't all completely symmetrical
#don't use MC.duplicate; use Instancer
You might not actually need to query the UV direction of the target geometry.
Try looking into the commands for constraints like cmds.normalConstraint or cmds.geometryConstraint. As for the number of objects to make, consider using a for loop in a range of how many objects you want. If you want to apply a random rotation, I would recommend using a group or a locator as an offset for transforming the group around (either with setAttr or xform) so that you can move the group while maintaining the ability to rotate the duplicate in it's y axis. If you go the route of normal constraints, be sure to use the aim flag to point the object up in its y axis! Googling the commands should come up with documentation on what flags to use.

Maya Python - How do I query the selection to check if only polyVerts or different?

I have a bunch of poly vertex components selected. But sometimes users might select something else along with it (a joint, another mesh, etc.). I'm trying to find a way to test against everything selected to verify it is indeed a vertex. But I can't find anything like this.
Is there a python command to do this directly?
It may be useful for your use-case to use filterExpand with the selectionMask flag set to 31 to return just polygon vertex components from everything that has been selected.
Following is a simple sample (with some comments):
Try it out with different kind of objects and components selected.
import maya.cmds as cmds
# Will return EVERYTHING selected
all_selected = cmds.ls(sl=True)
# Will filter out and return just the verts
# from everything selected
just_the_selected_verts = cmds.filterExpand(sm=31)
Check out filterExpand in the docs here.
Not exactly. You can find objects which are ready for component selection with cmds.ls(hl=True). You can find selected object which aren't components with cmds.ls(o=True). That means you can isolate only the component selections indirectly like this:
set (cmds.ls(sl=True, o=False)) - set(cmds.ls(sl=True, o=True))
which makes a set of the whole selection, then one with only the objects, and finally subtracts the second from the first leaving only the component selections (note that will also pass attribute selections if you have those).

[Python]obj.rotate on a specific object

This is my little Python programm Using Vpython
I want to rotate a box.
I want to use the boxes axis and not the one of the scene.
so for example if its rotated to the right and then i want to get the "nose" down, i want to do this in the view of the box...
imagine i was a jet ;)
BTW: I´m a python 3
from visual import *
a=box(size=(5,1,3),axis=(1,0,0))
def tasten():
"Looooopings "
if scene.kb.keys: #action on keyboard?
druck=scene.kb.getkey() #save to cache
if druck=='left':
a.rotate(angle=-1/100, axis=(1,0,0)) #links drehen
if druck=='right':
a.rotate(angle=1/100, axis=(1,0,0)) #rechts drehen
if druck=='up':
a.rotate(angle=-1,axis=(0,0,1)) #nose down
while True:
tasten()
I would recommend creating a box class that stores the orientation, as martineau is suggesting. The class would have a vector that stores its orientation, then a method to rotate it in whatever way required.

Categories

Resources