0
$\begingroup$

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
$\endgroup$
9
  • $\begingroup$ 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? $\endgroup$
    – rob
    Feb 15, 2019 at 16:59
  • $\begingroup$ I'm running the python from the text editor. What different resources does link give versus open_mainfile? $\endgroup$ Feb 15, 2019 at 17:52
  • $\begingroup$ When I try ops.wm.link I get RuntimeError: Error: 'D:\Path\to\blend\data\folder\CoDrone.blend': not a library $\endgroup$ Feb 15, 2019 at 20:47
  • $\begingroup$ check the notes here about path string requirements blender.stackexchange.com/questions/38060/… $\endgroup$
    – rob
    Feb 18, 2019 at 9:22
  • $\begingroup$ 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 jumpstation.co.uk/flog/Jan2018.html#p220120182113 that you may find useful. $\endgroup$
    – rob
    Feb 18, 2019 at 9:26

1 Answer 1

1
$\begingroup$

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
$\endgroup$
3
  • $\begingroup$ If you can't bring Moses to the mountain, append the mountain that contains Moses and delete the old mountain. $\endgroup$ Feb 19, 2019 at 18:00
  • $\begingroup$ Since Blender version 2.8 "uv_textures" has been renamed to "uv_layers", just for anyone looking for it and confused about it. $\endgroup$
    – Simarilius
    Oct 10, 2022 at 17:04
  • $\begingroup$ With a hint from @Simarilius, I upgraded the answer to Blender version 2.8. $\endgroup$ Oct 13, 2022 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .