Setting Image for Spot Projector

Use this forum for general user support and related questions.
igall2
Posts: 13
Joined: Sun Apr 15, 2018 1:07 pm

Setting Image for Spot Projector

Post by igall2 » Mon May 07, 2018 3:11 pm

Hi,

I'm trying to set the Image of Spot Projector from the Python Console of Blender, with the following line of code:
bpy.data.lamps['Spot'].luxcore.image = bpy.data.images['AAA.png']

As a result, I get an error:
Traceback (most recent call last):
File "C:\Users\igall\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons\BlendLuxCore\properties\light.py", line 60, in update_image
if context.lamp:
AttributeError: 'Context' object has no attribute 'lamp'


Despite the error, the field is being updated:
>>> bpy.data.lamps['Spot'].luxcore.image
bpy.data.images['AAA.png']


But the render results are unstable: sometimes the rendered image comes out with the pattern, and sometimes without.

What could be the reason for the problem?

Is there a better way to set the Image of Spot Projector from the Python Console of Blender?

Many thanks!
Igal

User avatar
B.Y.O.B.
Developer
Posts: 1018
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Setting Image for Spot Projector

Post by B.Y.O.B. » Mon May 07, 2018 4:14 pm

I fixed the exception being thrown and another issue.
However as you noted, it was only a "cosmetic" problem, since the image is still updated.
igall2 wrote:
Mon May 07, 2018 3:11 pm
But the render results are unstable: sometimes the rendered image comes out with the pattern, and sometimes without.
Sounds strange and I doubt it has anything to do with the rest (doesn't matter if the image was changed from UI or from Python).
Maybe it is related to this issue: https://github.com/LuxCoreRender/BlendL ... issues/152
Is your image a generated image? If yes, try to save it to disk.

By the way, you can find all properties here: https://github.com/LuxCoreRender/BlendL ... properties
Here is the faulty "update_image()" method: https://github.com/LuxCoreRender/BlendL ... ght.py#L72
And this is the fix: https://github.com/LuxCoreRender/BlendL ... 313625f46e
Support LuxCoreRender project with salts and bounties

igall2
Posts: 13
Joined: Sun Apr 15, 2018 1:07 pm

Re: Setting Image for Spot Projector

Post by igall2 » Tue May 08, 2018 7:52 am

Thanks B.Y.O.B!
B.Y.O.B. wrote:
Mon May 07, 2018 4:14 pm
I fixed the exception being thrown and another issue.
I took your fix and updated to the latest version.
B.Y.O.B. wrote:
Mon May 07, 2018 4:14 pm
Is your image a generated image? If yes, try to save it to disk.
I'm loading my image from the disk with the following lines of code:
image_name = os.path.basename(pattern_path)
bpy.ops.image.open(filepath=image_name, directory=os.path.dirname(pattern_path),
files=[{"name": image_name}], relative_path=False, show_multiview=True)


Still, the pattern existence in the render results is unstable.
That's how my results look like. It is a scene with two cameras. Each camera yields randomly a result with or without the pattern.
Image

What else could be possible to solve this please? :roll:
Last edited by igall2 on Wed May 09, 2018 8:33 am, edited 1 time in total.

User avatar
B.Y.O.B.
Developer
Posts: 1018
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Setting Image for Spot Projector

Post by B.Y.O.B. » Tue May 08, 2018 8:06 am

Can you upload an example scene showing the problem?
Support LuxCoreRender project with salts and bounties

igall2
Posts: 13
Joined: Sun Apr 15, 2018 1:07 pm

Re: Setting Image for Spot Projector

Post by igall2 » Tue May 08, 2018 2:23 pm

B.Y.O.B. wrote:
Tue May 08, 2018 8:06 am
Can you upload an example scene showing the problem?
Done.
Here is the link:
https://drive.google.com/file/d/1COTFuz ... sp=sharing

Important note:
The problem appeared on a Win10 x64 PC with GTX 1070 GPU and i7-8700K CPU
The problem didn't appear on a Win7 x64 PC with AMD Radeon R7 M360 GPU and i7-6600U CPU

To replicate the problem:
  • Download and unzip
  • In command line, go to the unzipped folder
  • Run the following command:
    blender.exe --background "LuxRenderTest.blend" --python "render_scene.py"
The multilayer EXR results of the rendering will appear in the same folder, in subdirectories "Left" and "Right".

I also placed in the folder a Jupyter notebook script that opens and shows the multilayer EXRs, open_multilayer_exrs.ipynb

Igal

User avatar
B.Y.O.B.
Developer
Posts: 1018
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Setting Image for Spot Projector

Post by B.Y.O.B. » Tue May 08, 2018 4:37 pm

I'm not sure if this is a problem in BlendLuxCore or in your script.

I would recommend to change your set_module_coord_frames() function in the following way:
replace this:

Code: Select all

    bpy.ops.image.open(filepath=image_name, directory=os.path.dirname(pattern_path),
                       files=[{"name": image_name}], relative_path=True, show_multiview=True)
    bpy.data.lamps['Spot'].luxcore.image = bpy.data.images[image_name]
with this:

Code: Select all

    from bpy_extras import image_utils
    img = image_utils.load_image(image_name, os.path.dirname(pattern_path))
    bpy.data.lamps['Spot'].luxcore.image = img
Running operators might require to force a scene update in some cases.
image_utils avoids this: https://docs.blender.org/api/2.79/bpy_e ... utils.html
Your script also makes some other wonky assumptions, e.g. that one of the cameras is selected. I would change this.

By the way, to do correct stereo you should use camera shift in X direction to make the camera frustums overlap exactly (currently you have some overshoot left and right if I'm not mistaken).

This is my result:
Attachments
Left.jpg
Right.jpg
scrn_2018-05-08_19-05-57.png
overshoot on the left and right
Support LuxCoreRender project with salts and bounties

igall2
Posts: 13
Joined: Sun Apr 15, 2018 1:07 pm

Re: Setting Image for Spot Projector

Post by igall2 » Wed May 09, 2018 8:43 am

Thanks B.Y.O.B,

I have replaced the code as you suggested, but still sometimes on a faster computer one of the cameras is rendered without the pattern.
B.Y.O.B. wrote:
Tue May 08, 2018 4:37 pm
Your script also makes some other wonky assumptions, e.g. that one of the cameras is selected. I would change this.
It seems that this is an important point to understand.
Given multiple cameras in the scene, is it possible to render them simultaneously, by a single Blender API call?
Or, should one organize a loop by cameras? In this case, should one use viewport?

It could be that because of this misunderstanding the rendering in the uploaded script sometimes goes wrong.

I believe that once it becomes clear I can write a correct script.

User avatar
B.Y.O.B.
Developer
Posts: 1018
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Setting Image for Spot Projector

Post by B.Y.O.B. » Wed May 09, 2018 3:34 pm

igall2 wrote:
Wed May 09, 2018 8:43 am
Given multiple cameras in the scene, is it possible to render them simultaneously, by a single Blender API call?
Or, should one organize a loop by cameras? In this case, should one use viewport?
No you can't render simultaneously, and it is not necessary.

What I meant is that your script assumes that the camera is selected when the .blend file is opened. In my tests, I once saved the file with the spotlight selected, and the script failed.
You should access them by name, e.g. bpy.data.objects["CamLeft"], instead of bpy.context.object and assuming that it is the camera.
igall2 wrote:
Wed May 09, 2018 8:43 am
I have replaced the code as you suggested, but still sometimes on a faster computer one of the cameras is rendered without the pattern.
Then please do further tests.
If you can show that the problem is a bug in BlendLuxCore, I can help you again.
Support LuxCoreRender project with salts and bounties

igall2
Posts: 13
Joined: Sun Apr 15, 2018 1:07 pm

Re: Setting Image for Spot Projector

Post by igall2 » Thu May 10, 2018 10:57 am

Thanks B.Y.O.B,

I did the corrections that you have suggested.
B.Y.O.B. wrote:
Wed May 09, 2018 3:34 pm
Then please do further tests.
If you can show that the problem is a bug in BlendLuxCore, I can help you again.
I have done more tests. Now I have the same behavior on both computers.
One or both of the cameras are now rendered without the pattern in about 50% of the runs.
I have uploaded here the new script + Blender model:
https://drive.google.com/open?id=16f1Ow ... EgsS7SrmJL
You can download and run it in the similar way like the previous one.

Best Regards,
Igal

User avatar
B.Y.O.B.
Developer
Posts: 1018
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Setting Image for Spot Projector

Post by B.Y.O.B. » Thu May 10, 2018 4:41 pm

Please test your script with Cycles as render engine.
If it works ok there but not in BlendLuxCore, I will investigate.
Support LuxCoreRender project with salts and bounties

Post Reply