Skip to content

Instantly share code, notes, and snippets.

@hsab
Last active August 29, 2018 16:12
Show Gist options
  • Save hsab/75d1c25ee8d24c37cd136788a977d608 to your computer and use it in GitHub Desktop.
Save hsab/75d1c25ee8d24c37cd136788a977d608 to your computer and use it in GitHub Desktop.
Blender: Import Scenes from Bunch of Files
import bpy
import os
import ntpath
D = bpy.data
C=bpy.context
path = 'D:\odrive\Cloud\Gutter\Outlier 2.0\Blends'
def listdir_fullpath(d):
return [os.path.join(d, f) for f in os.listdir(d) if 'blend' in f]
def get_basefile(d):
return ntpath.basename(d).split('.blend')[0]
files = listdir_fullpath(path)
for blend in files:
sceneName = get_basefile(blend)
with bpy.data.libraries.load(blend) as (data_from, data_to):
for attr in dir(data_to):
setattr(data_to, attr, getattr(data_from, attr))
for sc in D.scenes:
if 'Scene' in sc.name:
z = 0
dimZ = 0
sc.name = sceneName
for obj in sc.objects:
if obj.name == 'imageplane':
obj.name = 'Figure-' + sceneName
obj.data.name = 'Figure-' + sceneName
for slot in obj.material_slots:
if slot.material != None:
slot.material.name = 'F-' + sceneName
elif obj.name == 'Plane':
obj.name = 'Stairs-' + sceneName
obj.data.name = 'Stairs-' + sceneName
z= obj.location[2]
dimZ = obj.dimensions[2]
for slot in obj.material_slots:
mat = bpy.data.materials.get("Stairs")
if mat is None:
mat = bpy.data.materials.new(name="Stairs")
slot.material = mat
elif obj.name == 'Camera':
obj.name = 'CAM-' + sceneName
obj.data.name = 'CAM-' + sceneName
else:
print(obj.name)
objs = D.objects
objs.remove(obj, True)
for sc in D.scenes:
if sc.name != 'Import':
C.screen.scene = sc
cam = bpy.data.objects['CAM-'+C.scene.name]
stairs=bpy.data.objects['Stairs-'+C.scene.name]
bpy.ops.object.select_all(action='DESELECT')
stairs.select = True
C.scene.objects.active = stairs
bpy.ops.object.transform_apply(location = False, scale = True, rotation = False)
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
cam.parent = stairs
cam.matrix_parent_inverse = stairs.matrix_world.inverted()
dimX = stairs.dimensions[0]
factor = 3 / dimX
bpy.ops.transform.resize(value=(factor, factor, factor))
bpy.ops.object.transform_apply(location = False, scale = True, rotation = False)
bpy.ops.object.location_clear(clear_delta=False)
bpy.ops.object.rotation_clear(clear_delta=False)
stairs.location[2] = stairs.dimensions[2]/2
D.screens['Default'].areas[3].spaces[0].cursor_location = (0,0,0)
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
cam.parent = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment