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!).
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.active_material tex = mat.texture_paint_slots.data.name size_x = mat.texture_paint_images['<image name>'].size size_y = mat.texture_paint_images['<image name>'].size # set render size bpy.context.scene.render.resolution_x = size_x bpy.context.scene.render.resolution_y = size_y
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) # get the size of the imported picture size_x = pic_size.size size_y = pic_size.size # 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!