Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

June 27, 2012

Setting render dimensions equal to loaded image

If you know why this is handy, then i'm glad to help out. I sometimes use background images as references in the 3dview/camera , but if my render resolution + dimensions are not of the same ratio as the x and y of the image being used (as a reference), then the reference becomes squashed. lame.

import bpy

image_index = 0
rnd = bpy.data.scenes[0].render
rnd.resolution_x, rnd.resolution_y = bpy.data.images[image_index].size[:]

5 comments:

  1. Cool! Do you know how to load a background image with python? Thanks

    ReplyDelete
    Replies
    1. You can load an image using
      bpy.data.images.load("/home/zeffii/pyth.jpg")

      But setting up an image as background, involves having access to the context. Having 'context' means the script needs to be running inside 3dview (VIEW_3D) in either UI or TOOLS panel. Not really a big deal, but probably not entirely trivial for a first time python user.

      Maybe if you describe what exactly you would like to autoload images for, i can be more exact with answer and maybe help with a solution.

      Delete
  2. Thanks. This is what I have
    What I want to do is a crude OnionSkin from and opencl render movie (load the avi file as a bg image x times and offset the frames
    the problem is I cant set the bg file...

    import bpy

    class View3DPanel():
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'

    @classmethod
    def poll(cls, context):
    return (context.object is not None)


    class PanelOne(View3DPanel, bpy.types.Panel):
    bl_idname = "VIEW3D_PT_test_1"
    bl_label = "Panel One"

    def draw(self, context):
    layout = self.layout
    row = layout.row(align=True)
    row.alignment = 'EXPAND'
    row.operator("my.button", text="BG").number=1

    class OBJECT_OT_Button(bpy.types.Operator):
    bl_idname = "my.button"
    bl_label = "Button"

    def execute(self, context):

    bpy.ops.image.open(filepath="/tmp/0001-0020.avi")
    bpy.ops.view3d.background_image_add(filepath="/tmp/0001-0020.avi")

    return{'FINISHED'}

    #Registration
    bpy.utils.register_class(PanelOne)
    bpy.utils.register_module(__name__)

    ReplyDelete
    Replies
    1. The documentation kind of shows us that there is only limited support for what you are trying to achieve. I should perhaps rephrase that: 'the python API only has limited ways to interact with the background image'.
      http://www.blender.org/documentation/blender_python_api_2_63_12/bpy.ops.view3d.html

      But if you look at /scripts/startup/bl_ui/space_view3d.py, like 2519 to 2594, you might get some ideas.

      I made a start here: https://gist.github.com/3020668 (modfied your code)

      Perhaps you might consider using the new masking tools, which come with their own new viewer window. http://mango.blender.org/production/masking-in-blender/ watch the video.

      Delete
    2. Awsome! I'll play around with it
      I think I can get it to work without changing the source data. That way I can offset the frames.

      for x in range(0,layers):
      bpy.ops.view3d.background_image_add(name="0001-0020.avi")
      view = context.space_data
      view.background_images[0].image = bpy.data.images.load(filepath="/tmp/0001-0020.avi")
      view.background_images[0].image_user.frame_offset = frame - 2

      Thank you so much

      Delete

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.