Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# ------------------------------------------------------------------------------------------------
# VERS: 1.0
# DATE: August 11, 2020
# MADE: Cristobal Vila,, with snippets kindly shared by
# Shawn Frueh, and also 'robberyman'
# ------------------------------------------------------------------------------------------------
Next pure Python API snippet is a mix between the one shared by Shawn Frueh:
And also this one shared by 'robberyman':
With this we can calculate the total accumulated length for all selected edges, no matter
these ones belong to different item meshes, procedural, deformed, animated... ALL!
import lxu, lx
import math
# Initialize the selection service
SELECTION_SERVICE = lxu.service.Selection()
# Get the edge selection int type
edge_selection_type = SELECTION_SERVICE.LookupType(lx.symbol.sSELTYP_EDGE)
# Get the selection object: lxu.object.SelectionType
edge_selection_object = SELECTION_SERVICE.Allocate(lx.symbol.sSELTYP_EDGE)
# Convert that type into a packet so that we can access the selection data
edge_translation_packet = lx.object.EdgePacketTranslation(edge_selection_object)
# Get the total count of selected points
selected_edge_count = SELECTION_SERVICE.Count(edge_selection_type)
# Iterate over each edge and get some data. These edges could be coming from multiple meshes
# and so you will need to check the item they be
for edge_index in range(selected_edge_count):
# Get a pointer to the vertex data in the given index.
edge_pointer = SELECTION_SERVICE.ByIndex(edge_selection_type, edge_index)
# If we don't get a pointer, skip to the next index in the loop.
# This is necessary to prevent any crashes.
if not edge_pointer:
# Get the item the edge belongs to: lxu.object.Item
selection_item = edge_translation_packet.Item(edge_pointer)
item = lx.object.Item(selection_item)
# Get the matrix channel index
matrix_index = selection_item.ChannelLookup("worldMatrix")
# Prep the selection item to be read at the current time. You want those animated points!
# Get the matrix from the channel as a COM object and convert it to a matrix object.
item_world_matrix = lx.object.Matrix(selection_item.ChannelValue(matrix_index))
# Get the mesh item the edge belongs to: lxu.object.Mesh
selection_mesh = edge_translation_packet.Mesh(edge_pointer)
mesh = lx.object.Mesh(selection_mesh)
# IDs for the edge endpoints,
aPoint, bPoint = edge_translation_packet.Vertices(edge_pointer)
point = mesh.PointAccessor()
# Select and get the position of boths points. This will be relative to the mesh
# So if the mesh is not zero you will need to multiply this position by the transform data
# of the item it belongs to. We do this by multiplying it by the world matrix of it's item.
posA = item_world_matrix.MultiplyVector(point.Pos())
posB = item_world_matrix.MultiplyVector(point.Pos())
ABX = posA[0] - posB[0]
ABY = posA[1] - posB[1]
ABZ = posA[2] - posB[2]
AB_distance = math.sqrt(ABX**2 + ABY**2 + ABZ**2) # Pythagoras Theorem
TOTAL_LENGTH += AB_distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.