0
$\begingroup$

I am relatively new to Blender, not so much to Python, but either way I don't have a lot of experience with this stuff. What I am trying to use blender to do is import a mesh that was scanned with a 3D scanner, import a photo or multiple photos where the coordinates and rotation of the camera are known, then position the blender camera to those coordinates and project from view to texturize the mesh from the photos. So far everything has worked by hand, doing all of the steps through the GUI. I have gotten the scripting to replicate almost every step of the process but the one of the last steps where you select the image in the UV editor to project always crashes Blender. The line is as follows:

bpy.data.screens['UV Editing'].areas[2].spaces[0].image = bpy.data.images["image2.png"]

If I comment this line out, the script reaches the end fine. I also looked in the crash log and line 78 where the above code is is where it is crashing. Also, I'm certain that variable is correct because I have done:

print(bpy.data.screens['UV Editing'].areas[2].spaces[0].image)

and with nothing in the UV editor it prints "None" and when I manually select image2.png from the dropdown menu and run it again, it prints <bpy_struct, Image("image2.png")> Could this be a bug or am I doing something wrong? Any help would be greatly appreciated.

Here is the full code:

import bpy
import os
from bpy import data as D
from bpy import context as C
from mathutils import *
from math import *

scene = bpy.data.scenes["Scene"]

mesh = bpy.ops.import_scene.obj(filepath='C:\\Users\\Public\\Tests\\mesh.obj.obj')
#mesh_obj = bpy.data.objects.new("mesh_obj", mesh)

#bpy.ops.mesh.primitive_cube_add()

#import data for camera angles
x_pos = -.26111
y_pos = 1.5442
z_pos = .72831

x_rot = 231
y_rot = 169
z_rot = -4.47

#bpy.context.space_data.context = 'OBJECT'

scene.camera.rotation_mode = 'XYZ'
scene.camera.rotation_euler[0] = x_rot*(pi/180.0)
scene.camera.rotation_euler[1] = y_rot*(pi/180.0)
scene.camera.rotation_euler[2] = z_rot*(pi/180.0)

scene.camera.location.x = x_pos
scene.camera.location.y = y_pos
scene.camera.location.z = z_pos



obj = bpy.context.window.scene.objects[2]
bpy.context.view_layer.objects.active = obj
bpy.ops.object.editmode_toggle()

#bpy.ops.render.render(write_still=True)

#bpy.Redraw()

im_1 = bpy.ops.image.open(filepath="C:\\Users\\Julian Ferraro\\Documents\\image2.png", directory="C:\\Users\\Julian Ferraro\\Documents\\", files=[{"name":"image2.png", "name":"image2.png"}], relative_path=True, show_multiview=False)



mat = bpy.ops.material.new()
bpy.context.object.active_material.name = "UV_mat"
bpy.context.object.active_material.use_nodes = True

bsdf = bpy.data.materials["UV_mat"].node_tree.nodes["Principled BSDF"]
texImage = bpy.data.materials["UV_mat"].node_tree.nodes.new('ShaderNodeTexImage')
texImage.image = bpy.data.images["image2.png"]
bpy.data.materials["UV_mat"].node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])

image_texture = bpy.data.textures.new('export_texture', type = 'IMAGE')
image_texture.image = bpy.data.images["image2.png"]


#bpy.data.materials["UV_mat"].texture = image_texture

#print(bpy.data.materials["UV_mat"].name)


#bpy.ops.node.add_node(type="ShaderNodeTexImage", use_transform=True)


#bpy.ops.node.add_node(type="ShaderNodeTexImage", use_transform=True)

bpy.ops.object.editmode_toggle()

bpy.context.window.workspace = bpy.data.workspaces["UV Editing"]

im_2 = bpy.data.images.new(name='test2', width=1024, height=1024)

bpy.data.screens['UV Editing'].areas[2].spaces[0].image = bpy.data.images["image2.png"]


print(bpy.data.screens['UV Editing'].areas[2].spaces[0].image)

#print(bpy.data.workspaces[9])


print(bpy.context.workspace)
#bpy.ops.uv.project_from_view(camera_bounds=True, correct_aspect=True, scale_to_bounds=False)


print("DONE")

```
$\endgroup$
  • 1
    $\begingroup$ It is not possible to answer this unless you post the actual code and error messages at a minimum. If you are crashing Blender rather than just your script then you probably have an access violation. As a starting point check that the attribute you are trying to set actually exists before setting it by wrapping it in an if statement or a try: except: block. $\endgroup$ – Richard Rose Oct 30 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.