Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Weapon Effect UI - Maya Script
#Weapon Effect UI Ver.1
#Authors: Caspar Vee
#
#Currently only tested in Maya 2011.
#
#------------------------------------------
#
# -- OVERVIEW --
#This script creates weapon effects using Animated Sweeps and Particle Effects. There are three menus;
#Curve Menu: Select the curve in the scene. Curves must be visible to be on the list. If you create a new curve, reload
#the script to refresh the menu.
#Animated Sweep: Check the box if you want to use this effect. Options are: Start and End frames, frame delay, trail size and colour.
#Once you're happy with the effect, click 'Finalise'.
#Particle Effect: Check the box if you want to use this effect. Options are particle presets loaded from Maya's presets folder. Options are
#Particle size and Emit Rate. Once you're happy with the effect, click 'Finalise'
#
# -- INSTALLATION AND SETUP --
#To install the script, open Maya's script editor, load the script from the save location, select all and click "Save to Shelf".
#This script requires the use of an expression. Create an expression with any name and include the line of code below:
#python("doSweep("+frame+ " )");
#
#Run the script from the shelf when needed. Delete the expression once finished using the tool.
#
#Particle Presets: Maya does not save nParticle presets to the same place that it loads them from with nodePreset. To include any presets, move
#them to the folder Documents/maya/[maya version]/presets and ensure all presets have the prefix "transformPreset_".
#
#It is recommended that you name all objects in your scene meaningfully. This script cleans up after itself by deleting unused objects,
#but identifies these using default names. Giving all objects a meaningful prefix will avoid any unwanted deletion.
#
# -- REFERENCES --
#The keyframing script used for the animated sweep is a slightly modified version of a script provided by the user 'liubo' on Vimeo:
#https://vimeo.com/84725024
#
#------------------------------------------
import maya.cmds as cmds
import maya.mel as mel
#Loading lists
#GLOBAL defaults
#Animated Sweep
CURVE_NAME = "curve1"
listOfCurves = ""
START_FRAME = 1
END_FRAME = 10
FRAME_INCREMENT = 1
TRAIL_SIZE = 4
#Sweep Colours
RED = (1, 0, 0)
ORANGE = (1, 0.5, 0)
YELLOW = (1, 1, 0)
GREEN = (0, 1, 0)
SKYBLUE = (0, 1, 1)
DARKBLUE = (0, 0, 1)
PURPLE = (0.5, 0, 1)
PINK = (1, 0, 1)
BLACK = (0, 0, 0)
GREY = (0.5, 0.5, 0.5)
WHITE = (1, 1, 1)
COLOUR = WHITE
#Particle Sweep
PARTICLE_NAME = "particle1"
listOfPresets = ""
listOfShaders = ""
PARTICLE_SCALE = 0.5
EMIT_RATE = 200
#For getting the curve name
def setCurveName(selection):
global CURVE_NAME
CURVE_NAME = selection
print(CURVE_NAME)
#Animated Sweep
#For getting the start frame
def setStartFrame(inputText):
global START_FRAME
START_FRAME = inputText
print(START_FRAME)
#For getting the end frame
def setEndFrame(inputText):
global END_FRAME
END_FRAME = inputText
print(END_FRAME)
#For getting the frame increment
def setFrameIncrement(inputText):
global FRAME_INCREMENT
FRAME_INCREMENT = inputText
print(FRAME_INCREMENT)
#For getting the trail size
def setTrailSize(inputText):
global TRAIL_SIZE
TRAIL_SIZE = inputText
print(TRAIL_SIZE)
#For changing the colour of the sweep
def setColourName(colSelection):
global COLOUR
print (colSelection)
if (colSelection == "Red"):
COLOUR = RED
if (colSelection == "Orange"):
COLOUR = ORANGE
if (colSelection == "Yellow"):
COLOUR = YELLOW
if (colSelection == "Green"):
COLOUR = GREEN
if (colSelection == "Sky Blue"):
COLOUR = SKYBLUE
if (colSelection == "Dark Blue"):
COLOUR = DARKBLUE
if (colSelection == "Purple"):
COLOUR = PURPLE
if (colSelection == "Pink"):
COLOUR = PINK
if (colSelection == "Black"):
COLOUR = BLACK
if (colSelection == "Grey"):
COLOUR = GREY
if (colSelection == "White"):
COLOUR = WHITE
print(COLOUR)
def FinaliseSweep(self):
FINAL_PREFIX = "ANIM_"
#cmds.rename(CURVE_NAME, FINAL_PREFIX+CURVE_NAME)
cmds.rename("snapshot1Group", FINAL_PREFIX+CURVE_NAME+"_snapshot")
cmds.rename("loftedSurface1", FINAL_PREFIX+CURVE_NAME+"_loftedSurface")
#parentObject = cmds.listRelatives(CURVE_NAME, allParents=True )
#cmds.group( parentObject[0], CURVE_NAME, FINAL_PREFIX+CURVE_NAME+"snapshot", FINAL_PREFIX+CURVE_NAME+"loftedSurface", n="GRP_"+CURVE_NAME )
#Particle Sweep
#For getting the trail size
def setParticleName(selection):
global PARTICLE_NAME
PARTICLE_NAME = selection
print(PARTICLE_NAME)
#For getting the particle scale
def setParticleScale(inputText):
global PARTICLE_SCALE
PARTICLE_SCALE = inputText
print(PARTICLE_SCALE)
#For getting the emit rate
def setEmitRate(inputText):
global EMIT_RATE
EMIT_RATE = inputText
print(EMIT_RATE)
def FinaliseParticles(self):
FINAL_PREFIX = "PARTICLE_"
info = cmds.getAttr("nParticleShape1.particleRenderType")
print("INFO")
print(info)
print("INFO")
#rename particles/emitters
cmds.rename("nParticleShape1", FINAL_PREFIX+CURVE_NAME+"_particleShape")
cmds.rename("nParticle1", FINAL_PREFIX+CURVE_NAME+"_particle")
cmds.rename("emitter1", FINAL_PREFIX+CURVE_NAME+"_emitter")
cmds.rename("nucleus1", FINAL_PREFIX+CURVE_NAME+"nucleus")
#Get the materials
shaderGroup = cmds.listConnections(FINAL_PREFIX+CURVE_NAME+"_particle*", type='shadingEngine')
shaders = cmds.ls(cmds.listConnections(shaderGroup), materials=True)
#Loop through the shaders
for shade in shaders:
print(shade)
tempShader = cmds.duplicate(shade, ic=True)
print(tempShader[0])
shaderGroup2 = cmds.sets(r=True, nss=True, em=True, n="PARTICLE_SG")
#If the shader is of type Blobby Surface or otherwise, apply surface and volume shaders
if (info == 7):
cmds.connectAttr (tempShader[0] + ".outColor", shaderGroup2 + ".surfaceShader", f=True)
cmds.connectAttr (tempShader[0] + ".outColor", shaderGroup2 + ".volumeShader", f=True)
#Else if not a blobby surface, check for particleCloud type and apply to volume shader ONLY
elif (info !=7):
if (cmds.nodeType(tempShader[0]) == "particleCloud"):
cmds.hyperShade(a=tempShader[0])
cmds.connectAttr (tempShader[0] + ".outColor", shaderGroup2 + ".volumeShader", f=True)
#Select the particles and apply the set
selected = cmds.select(FINAL_PREFIX+CURVE_NAME+"_particle*")
cmds.sets(forceElement="PARTICLE_SG*")
cmds.hyperShade(a=tempShader[0])
def doSweep(frame):
sword_sweep(frame)
sparks_sweep(frame)
#define function
def sword_sweep(frame):
print(frame)
isEnabled = cmds.checkBox(AnimSweepCheck, q=True, v=True)
if frame == 1:
global CURVE_NAME
global START_TIME
global END_TIME
#Delete the old snapshots, if any exist
oldSnapshots = cmds.ls("snapshot*")
list_size = len(oldSnapshots)
if list_size >= 1:
cmds.delete("snapshot*")
#Delete the old lofts, if any exist
oldLofts = cmds.ls("loftedSurface*")
list_size = len(oldLofts)
if list_size >= 1:
cmds.delete("loftedSurface*")
if isEnabled == True:
#Create a snapshot from the curve's frames, using the start and end times and the frame increment
cmds.snapshot( CURVE_NAME, constructionHistory=True, startTime=START_FRAME, endTime=END_FRAME, increment=FRAME_INCREMENT )
#Find all of the transforms under the snapshot object
listOfTransforms = cmds.listRelatives("snapshot*", ad=True)
#Create a loft surface between the transforms
cmds.loft( listOfTransforms, ch=True, rn=True, ar=True )
#Create the new material and assign the colour and shading group
shader = cmds.shadingNode("lambert", asShader=True)
cmds.setAttr(shader + '.color', *COLOUR)
shaderGroup = cmds.sets(r=True, nss=True, em=True, n="lambertSG")
cmds.connectAttr (shader + ".outColor", shaderGroup + ".surfaceShader", f=True)
#Assign the new material to the surface
selected = cmds.select("loftedSurface*")
cmds.hyperShade(a=shader)
nodes = cmds.listRelatives(cmds.ls('snapshot*'), type='transform')
#Cycle through the transform curves and turn them from blocking to nonblocking on different frames
for i in range(len(nodes)):
cmds.setKeyframe(nodes[i],attribute='nodeState', value = 2, time = START_FRAME)
cmds.setKeyframe(nodes[i],attribute='nodeState', value = 0, time = i+int(START_FRAME))
cmds.setKeyframe(nodes[i],attribute='nodeState', value = 2, time = (i+(int(START_FRAME)+(int(TRAIL_SIZE)))))
#Hide the transform curves after creation
cmds.hide(cmds.ls("transform*", type='transform'))
cmds.select( clear=True )
def sparks_sweep(frame):
print(frame)
isEnabled = cmds.checkBox(ParticleSweepCheck, q=True, v=True)
if frame == 1:
global CURVE_NAME
global PARTICLE_NAME
#Delete unused materials
#Check if the particle exists, if it does, delete the old shaders
doesExist = cmds.particleExists("nParticle*")
if doesExist == True:
#get material and keep a list
tempShaderGroup = cmds.listConnections("nParticle*", type='shadingEngine')
# get the shaders amd delete them
listOfShaders = cmds.ls(cmds.listConnections(tempShaderGroup), materials=True)
list_size = len(listOfShaders)
if list_size >= 1:
for shade in listOfShaders:
print(shade)
cmds.delete(shade)
mel.eval('hyperShadePanelMenuCommand("hyperShadePanel1", "deleteUnusedNodes");')
#Delete the old emitters, if any exist
oldEmitters = cmds.ls("emitter*")
list_size = len(oldEmitters)
if list_size >= 1:
cmds.delete("emitter*")
#Delete the old particles, if any exist
oldParticles = cmds.ls("nParticle*")
list_size = len(oldParticles)
if list_size >= 1:
cmds.delete("nParticle*")
#Delete the old particles, if any exist
oldNuclei = cmds.ls("nucleus*")
list_size = len(oldNuclei)
if list_size >= 1:
cmds.delete("nucleus*")
if isEnabled == True:
nucleus = ""
cmds.select(CURVE_NAME)
cmds.emitter(n="emitter1", type="curve", r = float(EMIT_RATE), dx = 1)
cmds.nParticle()
cmds.nodePreset( load=('nParticle1', PARTICLE_NAME) )
cmds.setAttr('nParticle1.radius', float(PARTICLE_SCALE))
cmds.connectDynamic('nParticle1', em='emitter1')
cmds.select(clear=True)
#Load the list of curves
listOfCurves = cmds.ls(type="nurbsCurve", v=True)
list_size = len(listOfCurves)
if list_size >= 1:
print (listOfCurves)
#Load the list of particle presets
cmds.nParticle(n="nParticle1")
listOfPresets = cmds.nodePreset(ls='nParticle1')
list_size = len(listOfPresets)
if list_size >= 1:
print (listOfPresets)
#get material and keep a list, stops the script crashing
tempShaderGroup = cmds.listConnections("nParticle1", type='shadingEngine')
cmds.delete("nParticle1")
#create a window
win = cmds.window(title="Sweep Controls", width=400, height=200, s=False)
cmds.columnLayout()
#GET CURVE NAME
cmds.frameLayout(cll=False, label=" ", bgc=(0, 0.5, 0))
cmds.separator(height=5, style='none')
curveNameText = cmds.text(label="Curves:", al="left")
cmds.optionMenu("Curves", changeCommand=setCurveName, w=200)
for cve in listOfCurves:
print (cve)
cmds.menuItem(cve, label=cve)
cmds.separator(height=10, style='none')
cmds.setParent("..")
cmds.setParent("..")
#ANIMATED SWEEP
cmds.frameLayout(cll=True, label="Sweep", bgc=(0, 0.5, 0))
#define the layout of controls added
#to the window.
cmds.columnLayout()
cmds.separator(height=5, style='in')
AnimSweepCheck = cmds.checkBox(l="Animated Sweep", v=False)
cmds.separator(height=10, style='in')
startFrameText = cmds.text(label="Start Frame:")
cmds.textField("StartFrameText", w=200, enterCommand=setStartFrame, text=START_FRAME)
endFrameText = cmds.text(label="End Frame:")
cmds.textField("EndFrameText", w=200, enterCommand=setEndFrame, text=END_FRAME)
frameIncrementText = cmds.text(label="Frame Increment:")
cmds.textField("FrameIncrementText", w=200, enterCommand=setFrameIncrement, text=FRAME_INCREMENT)
cmds.separator(height=10, style='in')
trailSizeText = cmds.text(label="Trail Size:")
cmds.textField("TrailSizeText", w=200, enterCommand=setTrailSize, text=TRAIL_SIZE)
cmds.separator(height=10, style='in')
coloursText = cmds.text(label="Colours:", al="left")
cmds.optionMenu("Colours", changeCommand=setColourName, w=200)
cmds.menuItem(1, label="Red", en=True)
cmds.menuItem(2, label="Orange", en=True)
cmds.menuItem(3, label="Yellow", en=True)
cmds.menuItem(4, label="Green", en=True)
cmds.menuItem(5, label="Sky Blue", en=True)
cmds.menuItem(6, label="Dark Blue", en=True)
cmds.menuItem(7, label="Purple", en=True)
cmds.menuItem(8, label="Pink", en=True)
cmds.menuItem(9, label="Black", en=True)
cmds.menuItem(10, label="Grey", en=True)
cmds.menuItem(11, label="White", en=True)
cmds.separator(height=15, style='in')
cmds.button (label="Finalise", command=FinaliseSweep, w=200)
cmds.setParent("..")
cmds.setParent("..")
#PARTICLE SWEEP
cmds.frameLayout(cll=True, label="Particles", bgc=(0, 0.5, 0))
#define the layout of controls added
#to the window.
cmds.columnLayout()
cmds.separator(height=5, style='in')
ParticleSweepCheck = cmds.checkBox(l="Particle Sweep", v=False)
cmds.separator(height=10, style='in')
particleNameText = cmds.text(label="Particle Name:", al="left")
cmds.optionMenu("Presets", changeCommand=setParticleName, w=200)
for preset in listOfPresets:
print (preset)
cmds.menuItem(preset, label=preset)
cmds.separator(height=10, style='none')
particleScaleText = cmds.text(label="Particle Scale:", al="left")
cmds.textField("ParticleScaleText", w=200, enterCommand=setParticleScale, text=PARTICLE_SCALE)
cmds.separator(height=10, style='none')
emitRateText = cmds.text(label="Emit Rate:", al="left")
cmds.textField("EmitRateText", w=200, enterCommand=setEmitRate, text=EMIT_RATE)
cmds.separator(height=15, style='none')
cmds.button (label="Finalise", command=FinaliseParticles, w=200)
cmds.setParent("..")
cmds.setParent("..")
#show the window we last created
cmds.showWindow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment