1
$\begingroup$

Background: I am importing (appending) a new scene to an existing .blend file. The new scene has only compositing nodes and is only used for compositing renders created by the original scene.

What I want to do is to specifically address an image node in the compositor node tree, and then assign an image to it. That's it. So, for me, pseudo-code would look something like:

set the context to the compositor node tree
set the filename of the image node "Fred" to "E:/myimage.jpg"

I've been referred to: How do I edit an image node in blender using python? but can not get it to work properly. How to load and assign an image to a specific image node in the compositor?

$\endgroup$
  • 1
    $\begingroup$ Hello and welcome. If you feel a post was incorrectly closed as duplicate please state so in the comments section and politely edit your original question stating how you tried the suggested solution and why it didn't work for you, so it can be reopened. Antagonizing people trying to help you will only lead to other users avoiding your posts in the future $\endgroup$ – Duarte Farrajota Ramos Apr 21 at 9:55
  • 2
    $\begingroup$ What have you tried and what has failed? What part of the python api are you familiar with? $\endgroup$ – Leander Apr 21 at 10:14
  • $\begingroup$ Just starting. Don't know Python that well but am very familiar with other languages. I don't know anything about the Blender API and it seems very difficult to know how context works. The docs are definitely resource materials, not so much teaching materials. So, I end up asking for help. I'm a quick learner and need some basic help, Pointing to a link somewhere which is 'sorta' like it really does no good. Perhaps if SX is too sophisticated, you might point me somewhere where people can help newbies. $\endgroup$ – chippwalters Apr 21 at 10:43
  • $\begingroup$ This is the perfect place for asking such questions, really. We are all willing to help for sure. But... there are some rules (which you can read up in 5minutes) as already suggested 100 times before. So please take the time and read: blender.stackexchange.com/help/reopen-questions $\endgroup$ – brockmann Apr 21 at 10:47
  • $\begingroup$ OK. Read and understood. I did submit a new and now properly edited thread. I've read the lectures and have tried to abide. Now, how best to go about getting an answer to what I think should be a simple question? If the question is not clear, then what else can I do to make it clearer? $\endgroup$ – chippwalters Apr 21 at 10:52
2
$\begingroup$

Load your image using the path, add or get the image node in the compositor, and then simply assign your image data in memory to the image input of the node.

import bpy
from pathlib import Path

img_path = r"C:\MyDirectory\MyPicture.png"
if Path(img_path).is_file():
    img = bpy.data.images.load(img_path, check_existing=True)  # Load the image in data

    node_tree = bpy.context.scene.node_tree  # Get the current scene's compositor node tree

    img_comp_node = node_tree.nodes.new(type='CompositorNodeImage') #  Create a new image node
    # img_comp_node = node_tree.nodes.get('MyImageNodeName')  # Use this if your node already exists
    if img_comp_node:
        img_comp_node.image = img
else:
    print(f"The file at {img_path} doesn't exist")

Read: Controling compositor by python

| improve this answer | |
$\endgroup$
  • $\begingroup$ Hi @Gorgious Thanks! so the try structure replaces a file exists call? how do I specifially address an existing node? IOW, if I have an image node named "fred", how do I reference it? $\endgroup$ – chippwalters Apr 21 at 11:08
  • $\begingroup$ Yeah, actually you should use the file_exists method, and avoid using try/excepts like I did because the RuntimeError could be caused by other lines in the code inbetween. Another solution is to wrap all this in a method and put the except just after the bpy.data.images.load call, and return there $\endgroup$ – Gorgious Apr 21 at 11:12
  • $\begingroup$ would I use img_comp_node = node_tree.nodes.get('fred') to reference the image node named "fred"? $\endgroup$ – chippwalters Apr 21 at 11:15
  • $\begingroup$ @chippwalters That's correct, the node's name has to be fred. Not the label, not the image file name/path, just the unique node editor name, which you can access via the ui as well. $\endgroup$ – Leander Apr 21 at 11:18
  • $\begingroup$ @chippwalters Yes, but be careful because the image node's name can be different than its label, which by default is the name of the image file. To be sure, you can access the "Item" properties by typing N to get the right hand side panel, and check the node's name there. A node name is unique, but several nodes can share the same label $\endgroup$ – Gorgious Apr 21 at 11:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.