Skip to content

Instantly share code, notes, and snippets.

@Farfarer
Last active July 15, 2019 10:08
Show Gist options
  • Save Farfarer/bb6ea294444b42e1687306dc1d164a2e to your computer and use it in GitHub Desktop.
Save Farfarer/bb6ea294444b42e1687306dc1d164a2e to your computer and use it in GitHub Desktop.
Command to swizzle RGBA vertex map components.
#!/usr/bin/env python
import lx
import lxifc
import lxu.command
class ListMaps (lxifc.Visitor):
def __init__ (self, meshmap):
self.meshmap = meshmap
self.mapIDs = []
def vis_Evaluate (self):
self.mapIDs.append (self.meshmap.ID ())
class OptionPopupComponent(lxifc.UIValueHints):
def __init__(self):
self._items_i = ('r', 'g', 'b', 'a')
self._items_u = ('Red', 'Green', 'Blue', 'Alpha')
self._item_count = 4
def uiv_Flags(self):
return lx.symbol.fVALHINT_POPUPS
def uiv_PopCount(self):
return self._item_count
def uiv_PopUserName(self,index):
return self._items_u[index]
def uiv_PopInternalName(self,index):
return self._items_i[index]
class SwizzleValues (lxifc.Visitor):
def __init__ (self, point, polygon, meshMapIDs, mode, R, G, B, A):
self.point = point
self.polygon = polygon
self.meshMapIDs = meshMapIDs
self.mode = mode
self.R = R
self.G = G
self.B = B
self.A = A
self.value = lx.object.storage ('f', 4)
self.value.set ((0.0,0.0,0.0,1.0))
def swizzle (self):
v = self.value.get()
self.value.set((v[self.R], v[self.G], v[self.B], v[self.A]))
def vis_Evaluate (self):
pointID = self.point.ID ()
polygonCount = self.point.PolygonCount ()
polygonIDs = []
for x in xrange(polygonCount):
polygonIDs.append (self.point.PolygonByIndex(x))
for meshMapID in self.meshMapIDs:
self.point.MapEvaluate (meshMapID, self.value)
self.swizzle()
self.point.SetMapValue (meshMapID, self.value)
for polygonID in polygonIDs:
self.polygon.Select (polygonID)
if self.polygon.TestMarks (self.mode):
try:
self.polygon.MapValue (meshMapID, pointID, self.value)
except:
pass
else:
self.swizzle()
self.polygon.SetMapValue (pointID, meshMapID, self.value)
class SwizzleRGBA_Cmd(lxu.command.BasicCommand):
def __init__(self):
lxu.command.BasicCommand.__init__(self)
self.dyna_Add ('r', lx.symbol.sTYPE_STRING)
self.basic_SetFlags (0, lx.symbol.fCMDARG_OPTIONAL | lx.symbol.fCMDARG_QUERY)
self.dyna_Add ('g', lx.symbol.sTYPE_STRING)
self.basic_SetFlags (1, lx.symbol.fCMDARG_OPTIONAL | lx.symbol.fCMDARG_QUERY)
self.dyna_Add ('b', lx.symbol.sTYPE_STRING)
self.basic_SetFlags (2, lx.symbol.fCMDARG_OPTIONAL | lx.symbol.fCMDARG_QUERY)
self.dyna_Add ('a', lx.symbol.sTYPE_STRING)
self.basic_SetFlags (3, lx.symbol.fCMDARG_OPTIONAL | lx.symbol.fCMDARG_QUERY)
def cmd_UserName(self):
return 'Swizzle RGBA'
def cmd_Desc(self):
return 'Swizzle RGBA vertex map components'
def cmd_Tooltip(self):
return 'Swizzle RGBA vertex map components'
def cmd_Help(self):
return 'http://www.farfarer.com/'
def basic_ButtonName(self):
return 'Swizzle RGBA'
def cmd_Flags(self):
return lx.symbol.fCMD_MODEL | lx.symbol.fCMD_UNDO | lx.symbol.fCMD_MUSTSETARG
def basic_Enable(self, msg):
return True
def cmd_Interact(self):
pass
def arg_UIHints (self, index, hints):
if index == 0:
hints.Label ('Red')
elif index == 1:
hints.Label ('Green')
elif index == 2:
hints.Label ('Blue')
elif index == 3:
hints.Label ('Alpha')
def arg_UIValueHints(self, index):
if index == 0:
return OptionPopupComponent ()
elif index == 1:
return OptionPopupComponent ()
elif index == 2:
return OptionPopupComponent ()
elif index == 3:
return OptionPopupComponent ()
def cmd_Query(self,index,vaQuery):
va = lx.object.ValueArray ()
va.set (vaQuery)
if index == 0:
va.AddString ('r')
elif index == 1:
va.AddString ('g')
elif index == 2:
va.AddString ('b')
elif index == 3:
va.AddString ('a')
return lx.result.OK
def toIndex(self, component):
indices = {'r': 0, 'g': 1, 'b': 2, 'a' : 3}
return indices[component.lower()]
def basic_Execute(self, msg, flags):
meshMapNames = []
mesh_svc = lx.service.Mesh ()
mode = mesh_svc.ModeCompose ('select', 'hide lock')
sel_svc = lx.service.Selection ()
sel_type_vmap = sel_svc.LookupType (lx.symbol.sSELTYP_VERTEXMAP)
vmap_pkt_trans = lx.object.VMapPacketTranslation (sel_svc.Allocate (lx.symbol.sSELTYP_VERTEXMAP))
sel_vmap_count = sel_svc.Count (sel_type_vmap)
for vmap_idx in xrange (sel_vmap_count):
pkt = sel_svc.ByIndex (sel_type_vmap, vmap_idx)
vmap_type = vmap_pkt_trans.Type (pkt)
if vmap_type == lx.symbol.i_VMAP_RGBA:
meshMapNames.append(vmap_pkt_trans.Name (pkt))
allMaps = False
if len(meshMapNames) == 0:
allMaps = True
layer_svc = lx.service.Layer ()
layer_scan = lx.object.LayerScan (layer_svc.ScanAllocate (lx.symbol.f_LAYERSCAN_EDIT))
if not layer_scan.test ():
return
R = self.toIndex(self.dyna_String (0, 'r'))
G = self.toIndex(self.dyna_String (1, 'g'))
B = self.toIndex(self.dyna_String (2, 'b'))
A = self.toIndex(self.dyna_String (3, 'a'))
layer_scan_count = layer_scan.Count ()
for l in xrange (layer_scan_count):
mesh_item = layer_scan.MeshItem (l)
mesh = lx.object.Mesh (layer_scan.MeshEdit (l))
if not mesh.test ():
continue
if mesh.PolygonCount () == 0:
continue
point = lx.object.Point (mesh.PointAccessor ())
polygon = lx.object.Polygon (mesh.PolygonAccessor ())
meshmap = lx.object.MeshMap (mesh.MeshMapAccessor ())
if not (point.test () and polygon.test () and meshmap.test()):
continue
# RGBA MAPS
meshMapIDs = []
if not allMaps:
for meshMapName in meshMapNames:
try:
meshmap.SelectByName (lx.symbol.i_VMAP_RGBA, meshMapName)
except:
continue
else:
meshMapIDs.append (meshmap.ID ())
else:
meshmap.FilterByType (lx.symbol.i_VMAP_RGBA)
mapVisitor = ListMaps (meshmap)
meshmap.Enumerate (lx.symbol.iMARK_ANY, mapVisitor, 0)
meshMapIDs = mapVisitor.mapIDs
meshmap.FilterByType (0)
visitor = SwizzleValues (point, polygon, meshMapIDs, mode, R, G, B, A)
point.Enumerate (mode, visitor, 0)
layer_scan.SetMeshChange (l, lx.symbol.f_MESHEDIT_MAP_OTHER | lx.symbol.f_MESHEDIT_MAP_CONTINUITY)
layer_scan.Apply ()
lx.bless (SwizzleRGBA_Cmd, 'ffr.swizzleRGBA')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment