Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Add this script to a "Batch Script" editor (F10) and execute it with Script > Run.

The script will add adds random or uniform colors to a selection of objects:

Code Block
languagepy
# Load Python's random number generator
import random
 

# Define colour options and create a new empty GUI
colourModes = ["Random colours","Uniform colour"]
guiForm     = GUIFormDialog.new()
 
# Add input fields, drop-down menus and checkboxes with defaults to the GUI
guiForm.addListField("Colour mode", colourModes, 0)
guiForm.addIntField("Red (0-255)", 255)
guiForm.addIntField("Green (0-255)", 255)
guiForm.addIntField("Blue (0-255)", 255)
guiForm.addBoolField("Activate RBD mode", False)
 

# If user pressed "OK" start to read out the user's input
if (guiForm.show() == GUI_DIALOG_ACCEPTED):
	mode          = guiForm.getFieldValue("Colour mode")
	rbdActivation = guiForm.getFieldValue("Activate RBD mode")
	nodeSelection = scene.getSelectedNodes()
 
	# Assemble the RBD colour based on random values or the user's input
	for entry in nodeSelection:
		if (mode == 0):
			red   = random.randint(0, 255)
			green = random.randint(0, 255)
			blue  = random.randint(0, 255)
 
		if (mode == 1):
			red   = guiForm.getFieldValue("Red (0-255)")
			green = guiForm.getFieldValue("Green (0-255)")
			blue  = guiForm.getFieldValue("Blue (0-255)")

		# Apply the colour
		colour = Vector.new(red, green, blue)
		entry.setParameter("Color", colour)
 
		# Activate the RBD feature on demand
		if (rbdActivation == True):
			entry.setParameter("Dynamics","Active rigid body")
 
# If user pressed "Cancel" print out a warning
else: scene.message(str("Script cancelled by user"))