Skip to content

Instantly share code, notes, and snippets.

@enzyme69
Created March 24, 2013 01:47
Show Gist options
  • Save enzyme69/5230092 to your computer and use it in GitHub Desktop.
Save enzyme69/5230092 to your computer and use it in GitHub Desktop.
Blender Sushi Script to shuffle the transform position of selected meshes.
# Based on Example Script "Python In Maya" by Shaun Friedberg.
# http://www.pyrokinesis.co.nz
import bpy
import random
def completeShuffle(listToShuffle):
'''
'''
#
if not isinstance(listToShuffle, list):
raise RuntimeError ('Please provide a list of items to shuffle: {listToShuffle}'.format(listToShuffle = list) )
# Get the length of the list
numOfListItems = len(listToShuffle)
# Loop over the list until none are left
while numOfListItems > 1:
# Decrement numOfListItems
numOfListItems = numOfListItems - 1
# Select an item randomly and shuffle
randomItem = random.randrange(numOfListItems)
listToShuffle[randomItem], listToShuffle[numOfListItems] = listToShuffle[numOfListItems], listToShuffle[randomItem]
# Return our shuffled list
return listToShuffle
# USAGE testList = [1,2,3,4,5]
def randomlySwapSelectedMesh():
meshes = {}
meshes['transforms'] = []
meshes['mesh'] = bpy.context.selected_objects
# Check if there is more than one mesh selected
if not len(meshes['mesh']) > 1:
raise RuntimeError('Please select more than one mesh to swap!')
print('ORIGINAL POSITION')
# LIERO: don't append object location to the list, use location.copy()
for mesh in meshes['mesh']:
#meshes['transforms'].append(mesh.location)
meshes['transforms'].append(mesh.location.copy())
print('Mesh: ', mesh)
print('Old Location: ', mesh.location)
print('-----------' * 5)
# Shuffle the list of transforms
#random.shuffle(meshes['transforms'])
completeShuffle(meshes['transforms'])
print('AFTER SHUFFLE PROCESS')
# Iterate over mesh and apply new transforms
for newTransform, mesh in zip(meshes['transforms'], meshes['mesh']):
print('New Location: ', newTransform)
print('Mesh: ', mesh)
mesh.location = newTransform
print('-----------' * 5)
randomlySwapSelectedMesh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment