Skip to content

Instantly share code, notes, and snippets.

@ivogrig
Created June 10, 2015 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivogrig/19f5238660ac70d362bd to your computer and use it in GitHub Desktop.
Save ivogrig/19f5238660ac70d362bd to your computer and use it in GitHub Desktop.
Sets a new source mesh to a mesh instance, by manipulating the 'meshInst' and 'source' graphs.
################################################################################
#
# SetMeshInstance.py
#
# Version: 0.1
#
# Author: Ivo Grigull
#
# Last Update: 10/06/2015
#
# Usage:
# Copy this file into the lxserv folder before starting modo
# Run the script using arguments as in the example below or
# Select the instance, the new source and run the script without arguments
#
################################################################################
""" Example script usage:
# Create various primitive meshes
lx.eval('script.implicit "Unit Cube Item"')
lx.eval('script.implicit "Unit Sphere Item"')
lx.eval('script.implicit "Unit Toroid Item"')
# Create an instance
lx.eval('item.duplicate instance:true')
lx.eval('item.name Instance meshInst')
# Set the instance source to use the Cube
lx.eval('mesh.setInstance Instance Cube'
"""
import lx
import lxifc
import lxu.command
import modo
def setSourceInstance(instName=None, sourceName=None):
"""Sets a new source mesh to a mesh instance, by manipulating the 'meshInst' and 'source' graphs.
:param basestring instName: Name of the instance item
:param basestring instName: Name of the new source mesh item
If both arguments are None, the selection will be used. Select the instance first and then the new source mesh.
"""
scene = modo.Scene()
# If no arguments were passed
if not all((instName, sourceName)):
selection = scene.selected
if not len(selection) == 2:
return False
instance, newSource = selection
else:
# Try looking the items up using the given names
try:
instance = scene.item(instName)
newSource = scene.item(sourceName)
except LookupError:
return False
# Check for expected types
if not instance.type == 'meshInst' or not newSource.type == 'mesh':
return False
# Now disconnect the existing connection and connect the new source
# in both graphs respectively
# meshInst graph
for source in instance.itemGraph('meshInst'):
meshInstGraph = instance.itemGraph('meshInst')._graph
meshInstGraph.DeleteLink(source, instance)
meshInstGraph.AddLink(newSource, instance)
# source graph
for dest in instance.itemGraph('source').forward():
sourceGraph = instance.itemGraph('source')._graph
sourceGraph.DeleteLink(instance, dest)
sourceGraph.AddLink(instance, newSource)
return True
class CmdSetMeshInstance(lxu.command.BasicCommand):
def __init__(self):
lxu.command.BasicCommand.__init__(self)
self.dyna_Add('instance', lx.symbol.sTYPE_STRING)
self.basic_SetFlags(0, lx.symbol.fCMDARG_OPTIONAL)
self.dyna_Add('source', lx.symbol.sTYPE_STRING)
self.basic_SetFlags(1, lx.symbol.fCMDARG_OPTIONAL)
def cmd_Flags(self):
return lx.symbol.fCMD_MODEL | lx.symbol.fCMD_UNDO
def basic_Enable(self, msg):
return True
def cmd_Interact(self):
pass
def basic_Execute(self, msg, flags):
result = True
if self.dyna_IsSet(0) and self.dyna_IsSet(1):
nameInstance = self.dyna_String(0, None)
nameSource = self.dyna_String(1, None)
result = setSourceInstance(nameInstance, nameSource)
else:
result = setSourceInstance()
message = lx.object.Message(msg)
message.SetCode (lx.result.OK) if result else message.SetCode (lx.result.ABORT)
return result
lx.bless(CmdSetMeshInstance, "mesh.setInstance")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment