Skip to content

Instantly share code, notes, and snippets.

@SuddenDevelopment
Last active March 2, 2023 20:19
Show Gist options
  • Save SuddenDevelopment/42e1721e141cf718dfb9b1704953c8d1 to your computer and use it in GitHub Desktop.
Save SuddenDevelopment/42e1721e141cf718dfb9b1704953c8d1 to your computer and use it in GitHub Desktop.
a blender function to do a bunch of transforms and then update the view. so that view updates dont need to be run per object
def batchTransform(arrBatch, context=None, update_view=False):
# {
# object: string name_full,
# action: string = SCALE | ROTATE | ORIGIN | MOVE,
# value: (x,y,z), or string for parent or collection name
# mode: LOCAL=relative or adding, WORLD= world coordinates or setting value
# }
# return an array of issues, or empty array if all good.
arrErrors=[]
if context == None:
context = bpy.context
for objAction in arrBatch:
obj = None
ok = True
if objAction['object'] in bpy.data.objects:
obj = bpy.data.objects[objAction['object']]
if obj == None:
arrErrors.append({"object":objAction['object'],"error":"not found"})
ok=False
if ok and objAction['action'] == 'SCALE':
try:
if objAction['mode'] == 'LOCAL':
obj.scale += mathutils.Vector(objAction['value'])
else:
obj.scale = mathutils.Vector(objAction['value'])
except:
arrErrors.append({"object":objAction['object'],"error":"scale issue"})
elif objAction['action'] == 'ROTATE':
try:
rotation = objAction['value']
rotation_matrix = rotation.to_matrix().to_4x4()
rotation_matrix.to_euler()
obj.matrix_world @= rotation_matrix
except:
arrErrors.append({"object":objAction['object'],"error":"rotate issue"})
elif objAction['action'] == 'ORIGIN':
try:
setOrigin(obj, objAction['value'], objAction['mode'])
except:
arrErrors.append({"object":objAction['object'],"error":"origin issue"})
elif objAction['action'] == 'MOVE':
try:
if len(objAction['value']) == 2:
objAction['value'].append(obj.location[2])
obj.matrix_world.translation = mathutils.Vector(objAction['value'])
except:
arrErrors.append({"object":objAction['object'],"error":"move issue"})
elif objAction['action'] == 'PARENT':
objParent = None
if objAction['value'] in bpy.data.objects:
objParent = bpy.data.objects[objAction['value']]
obj.parent = objParent
obj.matrix_parent_inverse = objParent.matrix_world.inverted()
else:
arrErrors.append(
{"object": objAction['object'], "error": f'parent {objAction["value"]} not found'})
elif objAction['action'] == 'COLLECTION':
objCollection = None
if objAction['value'] not in bpy.data.collections:
objCollection = bpy.data.collections.new(
name=objAction['value'])
else:
objCollection = bpy.data.collections[objAction['value']]
objCollection.objects.link(obj)
obj.update_tag()
if update_view:
context.view_layer.update()
return arrErrors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment