Last active
September 24, 2019 11:25
-
-
Save GregMalick/fae131d15d063c96fc78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' this Command fires a ray down the +Z axis of a locator at a mesh | |
and selects any poly that the ray intersects | |
does not yet handle animated positions | |
Command format: | |
shootRay LocatorName MeshName ''' | |
NAME_CMD_SHOOTRAY = "shootRay" | |
NAME_ARG_LOCATOR = "StrLocatorName" | |
NAME_ARG_MESH = "StrMeshName" | |
getScene = lxu.select.SceneSelection().current | |
getSelectionService = lx.service.Selection | |
meshType = lx.service.Scene().ItemTypeLookup(lx.symbol.sITYPE_MESH) | |
locType = lx.service.Scene().ItemTypeLookup(lx.symbol.sITYPE_LOCATOR) | |
class CmdShootRay (lxu.command.BasicCommand): | |
def __init__(self): | |
lxu.command.BasicCommand.__init__(self) | |
# Define the parameters needed by the command | |
self.dyna_Add (NAME_ARG_LOCATOR, '&item') | |
self.dyna_Add (NAME_ARG_MESH , '&item') | |
def cmd_Flags (self): | |
return lx.symbol.fCMD_MODEL | lx.symbol.fCMD_UNDO | |
def cmd_Enable (self,msg): | |
return True | |
def cmd_Interact (self): | |
pass | |
def basic_Execute (self, msg, flags): | |
# Get the parameters needed by the command | |
locIdent = self.dyna_String (0, "") | |
meshIdent = self.dyna_String (1, "") | |
try: | |
if locIdent and meshIdent: | |
scene = getScene() | |
selectionService = getSelectionService() | |
locItem = scene.ItemLookupIdent (locIdent) | |
meshItem = scene.ItemLookupIdent (meshIdent) | |
if (locItem.Type() == locType and meshItem.Type() == meshType): | |
posXYZ = None | |
rotXYZ = None | |
# get the Locator Position | |
locItem.ReadEvaluated() | |
# get the Locator Position | |
channelValue = locItem.ChannelValue(lx.symbol.sICHAN_XFRMCORE_WPOSMATRIX) | |
positionMatrix = lxu.object.Matrix(channelValue) | |
posXYZ = positionMatrix.Get4()[3][:3] | |
# get the Locator Rotation | |
channelValue = locItem.ChannelValue(lx.symbol.sICHAN_XFRMCORE_WROTMATRIX) | |
rotationMatrix = lxu.object.Matrix(channelValue) | |
rotXYZ = rotationMatrix.Get4()[2][:3] | |
# Convert meshItem to a meshObject. | |
channels = scene.Channels(lx.symbol.s_ACTIONLAYER_EDIT, lx.service.Selection().GetTime()) | |
meshChannel = channels.ValueObj(meshItem, meshItem.ChannelLookup(lx.symbol.sICHAN_MESH_MESH)) | |
meshObject = lx.object.Mesh(meshChannel) | |
# get the polyAccessor (in order to get the polyId later) | |
polygonAccessor = meshObject.PolygonAccessor () | |
try: | |
# TRY TO FIND INTERSECTION - This code shoots the ray in the +Z axis | |
intersect = polygonAccessor.IntersectRay(posXYZ, rotXYZ) | |
except: | |
lx.out("NO HIT!") | |
pass | |
else: | |
# A HIT! and poly "selected" by the accessor | |
lx.out(intersect) | |
# get the polyID | |
polyID = polygonAccessor.ID() | |
# select the poly in the Viewport | |
polyType = selectionService.Allocate(lx.symbol.sSELTYP_POLYGON) | |
polySelectionType = selectionService.LookupType (lx.symbol.sSELTYP_POLYGON) | |
vertexPacketTranslation = lx.object.VertexPacketTranslation (polyType) | |
packet = vertexPacketTranslation.Packet (polyID, 0, meshObject) | |
selectionService.Select (polySelectionType, packet) | |
else: | |
lx.out("Syntax should be: shootRay locatorName meshName") | |
else: | |
lx.out("Syntax should be: shootRay locatorName meshName") | |
except: | |
lx.out(traceback.format_exc()) | |
# shootRay Locator Sphere | |
lx.bless (CmdShootRay, NAME_CMD_SHOOTRAY) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to Farfarer, Matt, Captain Obvious