Skip to content

Instantly share code, notes, and snippets.

@robomojo
robomojo / makeDuplicateChildren.py
Created April 6, 2013 10:42
For blender. Script to generate duplicates of the selected objects, parent the duplicate to the original, and offset its position.
import bpy
mylist = bpy.context.selected_objects
for each in mylist:
bpy.ops.object.select_all(action='DESELECT')
each.select=True
bpy.ops.object.duplicate()
dup = bpy.data.objects[len(bpy.data.objects)-1]
dup.layers=each.layers
dup.parent = each
dup.location = [0,0,2]
@robomojo
robomojo / selectObjectsInSameLocation.py
Created April 6, 2013 13:40
For blender. Script to select from selection objects which occupy the same location - useful for finding duplicates in a large selection.
import bpy
selectionDictionary = {}
newSelection = []
selection = bpy.context.selected_objects
for each in selection:
loc = str(each.location)
if loc not in selectionDictionary:
selectionDictionary[loc] = each
else:
newSelection.append(each)
@robomojo
robomojo / moveToClosestSnap.py
Created April 6, 2013 16:31
For blender. Moves all in selected to closest whole number location
import bpy
for each in bpy.context.selected_objects:
each.location=each.location.to_tuple(1)
@robomojo
robomojo / renameLightsByProperties.py
Created April 6, 2013 22:46
For blender. Renames lights using their property settings and sets ambient colour
import bpy
for i in bpy.context.selected_objects:
name = ""
name += str(i.data.type)
name += "_"
name += str(int(i.data.energy*1000))
name += "_"
name += str(int(i.data.distance*1000))
name += "_"
name += str(int(i.data.color[0]*1000))
@robomojo
robomojo / scriptsInit.py
Created April 7, 2013 07:50
For blender. Some foundations for setting up operators to run other scripts in the text editor. Works as is but still wip!
import bpy
print("---------------------------------------------------")
print("Auto-running blender-internal text block!")
print("---------------------------------------------------")
def DoRunScript (scriptName):
bpy.ops.text.run_script({'edit_text':bpy.data.texts[scriptName]})
class renderLayerButtonOperator(bpy.types.Operator):
@robomojo
robomojo / renderLayerConfig.py
Created April 7, 2013 07:56
For blender. Automates custom scene configurations for the selected renderlayer. Works as is but WIP!
import bpy
# SETTING DATA VALUES: LIST[VISIBLE LAYERS], STRING(WORLD NAME)
animRange=[0,250]
singleFrame=[0,0]
#ob_bg = bpy.data.objects['Environment_proxy']
#material_shad = bpy.data.materials['LightingPass_Shadow']
@robomojo
robomojo / duplicateAndApplyModifiers.py
Last active December 15, 2015 22:08
For blender. Duplicates selected object and applies all modifiers
import bpy
def createDuplicate(ob):
bpy.ops.object.select_all(action = 'DESELECT')
ob.select = True
bpy.ops.object.duplicate() # uses selected
dup = bpy.data.objects[len(bpy.data.objects)-1]
return dup
def applyModifiers(ob):
bpy.context.scene.objects.active = ob
@robomojo
robomojo / bakeToShapeKeys.py
Last active December 15, 2015 22:09
For blender. Using first mesh selection, bakes to second selection as shape key animation by iterations.
import bpy
scene = bpy.context.scene
current = scene.frame_current
start = scene.frame_start
end = scene.frame_end
div = 10
ob1 = bpy.context.selected_objects[1]
ob2 = bpy.context.selected_objects[0]
bpy.context.scene.objects.active = ob2
-- CONVERT FOCAL LENGTH TO FOV
-- this conversion method copied from:
-- http://math.stackexchange.com/questions/148719/calculate-field-of-view-from-focal-length
function ConvertFocalLengthToFOV (FocalLength)
local filmWidth = 1.672
local FocalLength_mm = FocalLength * 0.03937
return_fov = math.deg(2 * math.atan(filmWidth / (2 * FocalLength_mm)))
@robomojo
robomojo / toggleInstance.py
Last active December 16, 2015 05:29
For blender. Toggles selected objects between the conceptual original mesh datablock, and an instance of it. This is pretty unsafe, as it relies on the period '.' blender uses on naming duplicates, and toggling to instance mode will generate a new instance every time. #blender #python
import bpy
def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
return type('Enum', (), enums)
EnumMode=enum('Toggle','ForceUnique','ForceInstance')
mode=EnumMode.Toggle