I would like to automate the setting of a scene. I would like the image i will render has the exact same size of an image I import with the built-in add-on "import image as a plane".
For the moment, I import the image manually, but in the future I would like to wrap up in an addon... (but I'm not confident enough to write the addon right now, so let's go manually!).
First attempt
Here is what I began to write, but it seems it's going nowhere... My strategy is to identify the name of the material created when the plane is generated, find the name of the picture I imported, and then get its size...
import bpy
# use "Import image as plane"
# get the material's name of the selected object
mat = bpy.context.selected_objects[0].active_material
tex = mat.texture_paint_slots.data.name
size_x = mat.texture_paint_images['<image name>'].size[0]
size_y = mat.texture_paint_images['<image name>'].size[1]
# set render size
bpy.context.scene.render.resolution_x = size_x
bpy.context.scene.render.resolution_y = size_y
Second attempt
I am able to find the material created with the image imported as a plane. So I am looking inside this material the Image Texture. It seems to be much more clean as the first attempt.
import bpy
# identify the picture we just imported
texture = []
for ob in bpy.data.objects:
if ob.type == "MESH":
for mat_slot in ob.material_slots:
if mat_slot.material:
if mat_slot.material.node_tree:
texture.extend([x for x in mat_slot.material.node_tree.nodes if x.type=='TEX_IMAGE'])
pic_size = print(texture[0])
# get the size of the imported picture
size_x = pic_size.size[0]
size_y = pic_size.size[1]
# set render size
bpy.context.scene.render.resolution_x = size_x
bpy.context.scene.render.resolution_y = size_y
But something is wrong...
Thank you for your help!