0

In my script, I'm trying to open a new .blend file, then create a new plane and attach an image to that. I can run the script to create a plane mesh and successfully attach an image to that, but if I preface to that script the opening of a new blender file, I lose that ability. Here is a stripped-down version of my code rearranged so that it exposes the problem:

# Imports.
from bpy import context, ops
import os

# Demonstrate that you can attach an image to a plane.
ops.mesh.primitive_plane_add()
plane = context.scene.objects.active
plane.data.uv_textures.new()
if len(plane.data.uv_textures[0].data) > 0:
    print('We have UV Texture data')
else:
    print("We don't have UV Texture data")

# Where to get blender files.
blender_file_dir = 'D:\\Path\\to\\blend\\data\\folder'

# Object class.
object_name = 'CoDrone'

# Open blender file (needs to match the object name).
filepath = os.path.join(blender_file_dir, '{}.blend'.format(object_name))
ops.wm.open_mainfile(filepath=filepath)

# Demonstrate that you can no longer attach an image to a plane
ops.mesh.primitive_plane_add()
plane = context.scene.objects.active
plane.data.uv_textures.new()
if len(plane.data.uv_textures[0].data) > 0:
    print('We have UV Texture data')
else:
    print("We don't have UV Texture data")

Running the above code gets this output:

We have UV Texture data
Read blend: D:\Path\to\blend\data\folder\CoDrone.blend
We don't have UV Texture data
Dave Babbitt
  • 111
  • 6
  • when you run open_mainfile I think you are erasing your current scene, try bpy.ops.wm.link( to link in an existing .blend with the resources you need. Are you running your python from the command line? – rob Feb 15 '19 at 16:59
  • I'm running the python from the text editor. What different resources does link give versus open_mainfile? – Dave Babbitt Feb 15 '19 at 17:52
  • When I try ops.wm.link I get RuntimeError: Error: 'D:\Path\to\blend\data\folder\CoDrone.blend': not a library – Dave Babbitt Feb 15 '19 at 20:47
  • check the notes here about path string requirements https://blender.stackexchange.com/questions/38060/how-to-link-append-with-a-python-script – rob Feb 18 '19 at 09:22
  • Though, I notice in my code I did not have to do that, I was using Linux so the paths may be different. I wrote a blog post on how I got Blender to write the ops.wm.link line for me http://www.jumpstation.co.uk/flog/Jan2018.html#p220120182113 that you may find useful. – rob Feb 18 '19 at 09:26
  • I can't even get link or append to work in the user interface! How do I link or append a whole .blend file? – Dave Babbitt Feb 19 '19 at 13:12
  • what do you mean "can't get to work"? You do not link/append whole .blend files but the items within them. If you want everything you can select Scene. Once you have tried to link/append, check the Outliner to see if the items have been added. – rob Feb 19 '19 at 13:43
  • @rob I think the deal is that the objects aren't joined - they are in a hierarchy, but when you select the top object, the other objects don't come along for the ride. My attempts to join them are unsuccessful. Thank you for your hints. – Dave Babbitt Feb 19 '19 at 16:03
  • @rob, also I tried saving the previous context and passing it to the second call to primitive_plane_add, but that had no effect. – Dave Babbitt Feb 19 '19 at 16:21

1 Answers1

1

With hints from @rob that it's better to append the scene and destroy the old one, and code from How to delete a scene with its content? to destroy the objects in the old scene first, I finally got the results I expected. When you run this code:

# Imports.
from bpy import context, ops, data
import os
import random

def delete_scene_objects():

# Get the current scene.
scene = context.screen.scene

# Remove objects.
for object_name in scene.objects:
    data.objects.remove(object_name, True)

return scene

def add_plane(): ops.mesh.primitive_plane_add() plane = context.scene.objects.active

# Add a UV texture layer to the mesh.
plane.data.uv_layers.new()

# Demonstrate that you can attach an image to the mesh.
if len(plane.data.uv_layers[0].data) > 0:
    print('We have UV Texture data')
else:
    print("We don't have UV Texture data")

def append_blend_file(blend_file): section_name = '\Scene\' object_name = 'Scene'

directory_name = blend_file + section_name
ops.wm.append(filename=object_name, directory=directory_name)

def add_blend_file():

# Where to get blender files.
blender_file_dir = 'D:\\Path\\to\\blend\\data\\folder'

# Object class.
class_name = 'CoDrone'

# Open blender file (needs to match the object name).
file_name = '{}.blend'.format(class_name)
blend_file = os.path.join(blender_file_dir, file_name)
append_blend_file(blend_file)

add_plane() old_scene = delete_scene_objects() add_blend_file()

Remove old scene.

data.scenes.remove(old_scene, True)

add_plane()

You get this output:

We have UV Texture data
We have UV Texture data
Dave Babbitt
  • 111
  • 6