Character Essences, Research & Coding

Curious Cones

This little project aims to create a random set of cones that “look at” a shiny red sphere passing by. The reference below shows the original maya + python youtube tutorial. The code that follows was saved as 2 shelf tools which can be used with any mesh.

The first script (randomInstances.py) creates 30 instances of the first object selected in the scene. These instances are then randomly positioned, rotated and scaled.

#randomInstances.py
import maya.cmds as cmds
import random
random.seed(1234)
result = cmds.ls(orderedSelection = True)
print 'result: %s ' %(result)
transformName = result[0]
instanceGroupName = cmds.group(empty = True, name = transformName + '_instance_grp#')
for i in range(0, 30):
   instanceResult = cmds.instance(transformName, name = transformName + '_instance#')
   cmds.parent(instanceResult, instanceGroupName)
   tx = random.uniform(-10, 10)
   ty = random.uniform(0, 20)
   tz = random.uniform(-10, 10)
   rotX = random.uniform(0, 360)
   rotY = random.uniform(0, 360)
   rotZ = random.uniform(0, 360)
   sXYZ = random.uniform(0.1, 1.25)
   cmds.move(tx, ty, tz, instanceResult)
   cmds.rotate(rotX, rotY, rotZ, instanceResult)
   cmds.scale(sXYZ, sXYZ, sXYZ, instanceResult)
cmds.hide(transformName)
cmds.xform(instanceGroupName, centerPivots = True)

The second script(aimAtFirst.py) takes the first elements selected and sets it as a target, while the rest of the elements selected are set as sources, looking at this target along their Y axis. At least one source and one target must be selected in order for the algorithm to work.

#aimAtFirst.py

selectionList = cmds.ls(orderedSelection = True)
if len(selectionList) >= 2:
   print 'Selected items: %s ' % (selectionList)
   targetName = selectionList[0]
   selectionList.remove(targetName)
   for objectName in selectionList:
      print 'Constraining %s towards %s ' %(objectName, targetName)
      cmds.aimConstraint(targetName, objectName, aimVector = [0, 1, 0])
else:
   print 'Please select 2 or more objects'

Reference: Autodesk Scripting on Youtube

Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s