2012 in blogging

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

About 55,000 tourists visit Liechtenstein every year. This blog was viewed about 290,000 times in 2012. If it were Liechtenstein, it would take about 5 years for that many people to see it. Your blog had more visits than a small country in Europe!

In 2012, there were 374 new posts, growing the total archive of this blog to 1,123 posts. There were 810 pictures uploaded, taking up a total of 186 MB. That’s about 2 pictures per day.

The busiest day of the year was August 31st with 1,893 views. The most popular post that day was Friday Flashback #85.

Click here to see the complete report.

Saturday snippet – Tuple assignment

Early this week, I posted a script that did a random shuffle of a collection of objects. That script used tuple assignment to swap elements in a list; here’s a simpler example, with the tuple assignment (line 9) highlighted:

import random
v = [o for o in Application.Selection]

print [o.Name for o in v]

# random shuffle
for i in range( len(v) ):
	j = random.randint( i, len(v)-1 )
	v[i], v[j] = v[j], v[i]

print [o.Name for o in v]

# [u'cube', u'cube1', u'cube2', u'cube3', u'cube4', u'cube5', u'cube6', u'cube7', u'cube8']
# [u'cube5', u'cube4', u'cube7', u'cube2', u'cube1', u'cube3', u'cube', u'cube8', u'cube6']

Looking at that line, you might wonder why you don’t end up assigning the same value to both a[i] and a[j] (eg, how does that line not do a[i] = a[j] and then a[j] = a[i] ?).

In tuple assignment, the right-hand side is considered a tuple of values. So the right-hand side is evaluated first, and then the resulting values are pairwise assigned to the tuple on the left hand side. For example, consider this-rather-more-concrete snippet:

a = 2
b = 8
a,b = b-a,b+a
print a
print b
# 6
# 10

The right-hand side “b-a,b+a” is first evaluated, giving the tuple 6, 10, so you effectively have this:

a,b = 6,10

Cycling particle colors through gradients

Here’s an ICE tree that uses a simple two-state setup to gradually move particle colors through two gradients (in State 0, the gradient is from black to white, and in State 1, from white to black). Randomizing the End Time gives me some variation between particles, and I customized the Modify Particle Colors to use “time in state” so I could use that as the trigger test.
ModifyParticleColor-w-StatesIn the Modify Particle Color compound, I simply swapped Get Particle Age for Get Time in State.

Building an array for weighted random selection

Here’s a little ICE tree that takes an array like
[ 0.1, 0.2, 0.2, 0.5 ]
and builds an array where

  • 10% of the elements have value 0
  • 20% of the elements have value 1
  • 20% of the elements have value 2
  • 50% of the elements have value 3

This is something I wanted to do for randomizing with weighted probabilities. The array [ 0.1, 0.2, 0.2, 0.5 ] is the weights I want to use for the values 0, 1, 2, and 3. The idea is that if I randomly select from the larger array, the weights will determine how likely I am to get each value (eg if 50% of the elements have value 3, then I’ll be much more likely to get that value when I randomly select).

To automatically build the array, I had to use a Repeat with Counter; I didn’t see around that.


So, for example, in an array of 1000 elements, I would have 100 elements with the value 0, 200 elements with the value 1, 200 elements with the value 2, and 500 elements with the value 3.

That works fine if the input array of weights adds up to 1, but what if I start with an array like [2 4 2 8 16] ?
To handle that, I need to make a small change:


Scripting – Shuffling a collection to randomly select by percentage

randompercentageVia a tech-artists.org tweet, I came across some MAXScript for randomly selecting a specified percentage of mesh elements (for example, give me a random selection that includes 35% of all vertices).

I converted it to Python in Softimage. Note that I don’t really shuffle a collection: you can’t set items in a collection, so there’s no way to swap items. Instead, I put the collection in a list and shuffle the list.

Shuffling actually makes this harder than it has to be. Check out Alan’s nice script for a better way to do this.

si = Application
log = si.LogMessage
sisel = si.Selection

import random

# Return a list that includes a randomly selected
# percentage of the items in a collection
def get_random_percentage( collection, percentage ):
	v = [x for x in collection]

	# random shuffle
	for i in range( len(v) ):
		j = random.randint( i, len(v)-1 )
		v[i], v[j] = v[j], v[i]

	# select by percentage
	step = 100/percentage
	w = []
	for i in xrange( 0, len(v), step ):
		w.append( v[i] )

#	print len(w)
#	print (percentage/100.0) * len(v)
	return w

Application.SelectObj("torus", "", True)
# Select a random 50% of the vertices
x = get_random_percentage( sisel(0).ActivePrimitive.Geometry.Vertices, 50 )

print sisel(0).SubComponent.ComponentCollection.Count

# Suppose you had 2000 cubes.
# Select a random 25% of those 2000...
Application.SelectObj("cube*", "", True)
x = get_random_percentage( sisel, 25 )

I learned a couple of things about MAXScript:

  • MAXScript arrays are 1-based
  • In the MaxScript docs, there aren’t any function/method reference pages. You have to go to a “value” page (eg Number Values) and there you’ll find all the methods. That’s fine once you know, but it was confusing at first when I didn’t see anything in the TOC for Functions or Methods.

Saturday Snippet – XSICollections and CollectionItems

When you stick something, like say a Vertex, into an XSICollection, you get a CollectionItem. But you can get back to the Vertex if you know how (via the SubComponent

si = Application
log = si.LogMessage
sisel = si.Selection
import win32com.client

oColl = win32com.client.Dispatch( "XSI.Collection" )
o = sisel(0)

print si.ClassName( o.ActivePrimitive.Geometry.Vertices(0) )
# Vertex

#oColl.Add( o.ActivePrimitive.Geometry.Vertices(0) )
oColl.AddItems( o.ActivePrimitive.Geometry.Vertices )
print si.ClassName( oColl(0) )
# CollectionItem

print si.ClassName( oColl(0).SubComponent.ComponentCollection(0) )
# Vertex

a = o.ActivePrimitive.Geometry.Vertices(0)
b = oColl(0).SubComponent.ComponentCollection(0)
print a.IsEqualTo(b)
# True
print b.IsEqualTo(a)
# True