import bpy
FILE_PATH = "/home/vandy/startUpProjects/tailorMade/pythonScripts/"
def library_get(name, data_blocks, libpath=None):
""" Since it is possible for a .blend file to have several objects
with the same name linked from different files, this routine lets us
hunt down the right one. """
for x in data_blocks:
if x.name == name and x.library is not None:
if libpath is None or libpath == x.library.filepath:
return x
return None
def addGroupToScene(grp, scn):
for obj in grp.objects:
scn.objects.link(obj)
def linkMeshObject():
fname = "//remote.blend"
print(fname)
with bpy.data.libraries.load(fname, link=True) as (src, dst):
# src has many properties, each of which is a list of name strings.
# dst is how we specify what we want to link from the library file.
# and the "omnitile" object
dst.objects = ["Cylinder"]
# just because we linked an object doesn't mean that object is in any of our scenes.
omnitile = library_get("Cylinder", bpy.data.objects)
scn = bpy.context.scene
try:
scn.objects.link(omnitile)
# now it is.
except:
pass
def linkMaterial():
fname = "//material1.blend"
print(fname)
with bpy.data.libraries.load(fname, link=True) as (src, dst):
# src has many properties, each of which is a list of name strings.
# dst is how we specify what we want to link from the library file.
# and the "omnitile" object
dst.materials = ["mainMaterial"]
# just because we linked an object doesn't mean that object is in any of our scenes.
omnitile = library_get("mainMaterial", bpy.data.materials)
scn = bpy.context.scene
print(len(scn.objects))
try:
scn.objects.link(omnitile)
# now it is.
except:
pass
#Material is still not getting applied. Running the below code doesn't work either
for obj in scn.objects:
if obj.type == 'MESH':
if obj.data.materials:
# assign to 1st material slot
obj.data.materials[0] = omnitile
else:
# no slots
obj.data.materials.append(omnitile)
linkMeshObject()
linkMaterial()
bpy.data.scenes['Scene'].render.filepath = FILE_PATH + 'test.png'
bpy.ops.render.render(write_still=True)
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.