Little Addon for optical lenses etc.

General project and community related discussions and offtopic threads.
CodeHD
Posts: 182
Joined: Tue Dec 11, 2018 12:38 pm

Little Addon for optical lenses etc.

Post by CodeHD » Sat Jun 15, 2019 5:41 pm

Hi all,

I have starting using Blender and LuxCore also with the hopes to make some visualisations for optical designs/scientific instruments. It proved quite useful, but something I had problems with was creating accurate spherical surfaces. I could make them by boolean intersections of a sphere with something else, but for large surface radii, that required high-u,v-spheres for accurate smoothing, and thus was very slow. Also, there could be artifacts at the lens edge.
I couldn't find existing addons for this, so I now started writing myself one to generate lenses (and mirrors) directly, with a well-defined numer of vertices on each surface.

I see that some others here use lenses, specifically in some renderings with BiDir, so I thought you might find this helpful, perhaps. :roll:

You can find the code here. I didn't bother to be creative about names, so because the work with LuxCore led to this, it is now called Opti(cs)Core :D :

https://github.com/CodeFHD/OptiCore

Here is an image with a low vertex-count to show how the surfaces are generated:
OptiCore.PNG
It is basically a day worth of programming so far, so don't expect too much :lol: I plan to add more features like parabolic mirrors, off-axis mirrors or aspheric lenses.

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

Re: Little Addon for optical lenses etc.

Post by B.Y.O.B. » Sun Jun 16, 2019 1:32 am

Cool!
It would be nice if we could select a material in the operator settings, for example our lens glass, so we can see the resulting optical path of a laser in the viewport while editing the lens properties.
For the same purpose it would also be good if the lens was by default set to smooth shading + autosmooth enabled.
Support LuxCoreRender project with salts and bounties

CodeHD
Posts: 182
Joined: Tue Dec 11, 2018 12:38 pm

Re: Little Addon for optical lenses etc.

Post by CodeHD » Sun Jun 16, 2019 8:05 am

Yes I was thinking the same. Is selecting the material actually possible or was that more a theoretical suggestion from you? I can't find information on it immediately...

The ideal would be to have it as a modifier, i.e. create an empty and then the modifier adds the mesh, so you could go back and change the settings of the first lens after creating a second one, or animate the shapes. But, as far as I read, that is not possible without making your own Blender fork (?)

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

Re: Little Addon for optical lenses etc.

Post by B.Y.O.B. » Sun Jun 16, 2019 9:06 am

CodeHD wrote:
Sun Jun 16, 2019 8:05 am
Is selecting the material actually possible or was that more a theoretical suggestion from you? I can't find information on it immediately...
It is possible.
Unfortunately you can't create a PointerProperty on operators (you get a message by Blender that it's not possible), so you can't link to a material directly. But you can create a StringProperty where you store the material name, and in your execute() method, you just find the material by name in bpy.data.materials and attach it to the created object.

Here's a working patch that illustrates this:

Code: Select all

diff --git a/__init__.py b/__init__.py
index 6a8f0d6..65dc117 100644
--- a/__init__.py
+++ b/__init__.py
@@ -13,7 +13,7 @@ bl_info = {
 import bpy
 import numpy as np
 from bpy.types import Operator
-from bpy.props import FloatProperty, IntProperty
+from bpy.props import FloatProperty, IntProperty, StringProperty
 from bpy_extras.object_utils import AddObjectHelper, object_data_add
 from . import elements as ele
 
@@ -53,6 +53,10 @@ class OBJECT_OT_add_lens(Operator, AddObjectHelper):
            default = 1.,
            description="Center thickness of lens",
            )
+    material_name = StringProperty(
+           name="Material",
+           default="",
+    )
 
     def execute(self, context):
diff --git a/elements/lens.py b/elements/lens.py
index bb33368..acc18c6 100644
--- a/elements/lens.py
+++ b/elements/lens.py
@@ -54,4 +54,8 @@ def add_lens(self, context):
     mesh.from_pydata(verts, edges, faces)
     # useful for development when the mesh may be invalid.
     #mesh.validate(verbose=True)
-    object_data_add(context, mesh, operator=self)
\ No newline at end of file
+    obj_base = object_data_add(context, mesh, operator=self)
+    obj = obj_base.object
+    if self.material_name in bpy.data.materials:
+        mat = bpy.data.materials[self.material_name]
+        obj.data.materials.append(mat)
You still have to type in the material name by hand, there's no dropdown yet.
It looks like this:
Attachments
2019-06-16_10-53-32.png
Support LuxCoreRender project with salts and bounties

CodeHD
Posts: 182
Joined: Tue Dec 11, 2018 12:38 pm

Re: Little Addon for optical lenses etc.

Post by CodeHD » Sun Jun 16, 2019 9:22 am

Ok, thanks for the example! I will start from there :)

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

Re: Little Addon for optical lenses etc.

Post by B.Y.O.B. » Sun Jun 16, 2019 9:26 am

B.Y.O.B. wrote:
Sun Jun 16, 2019 9:06 am
You still have to type in the material name by hand, there's no dropdown yet.
You can add a dropdown in the UI like this:

Code: Select all

layout.prop_search(self, "material_name",
  bpy.data, "materials",
  icon="NONE")
But to be able to use this code, you need a custom draw(self, context) method for your operator, where you have to show all the properties by hand.
I found no way yet to combine the default layout generated by Blender when the draw() method is missing with a custom layout.
CodeHD wrote:
Sun Jun 16, 2019 8:05 am
The ideal would be to have it as a modifier, i.e. create an empty and then the modifier adds the mesh, so you could go back and change the settings of the first lens after creating a second one, or animate the shapes. But, as far as I read, that is not possible without making your own Blender fork (?)
Yes, you can't create a modifier with Python.
However, you could register your properties on the bpy.types.Object class and create a custom UI panel that shows them + an "apply" button that calls your operator. On execution, the operator would read the properties from the object, destroy its mesh and recreate it.
This way you could always come back to an object and make a lens out of it (or alter the existing lens).
You could register a property "is_lens" on the object so you can mark it as a lens on creation and check this in your panel's poll() method, otherwise your panel would show up on every object in the scene.
Support LuxCoreRender project with salts and bounties

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

Re: Little Addon for optical lenses etc.

Post by B.Y.O.B. » Sun Jun 16, 2019 9:51 am

B.Y.O.B. wrote:
Sun Jun 16, 2019 9:26 am
However, you could register your properties on the bpy.types.Object class and create a custom UI panel that shows them + an "apply" button that calls your operator. On execution, the operator would read the properties from the object, destroy its mesh and recreate it.
This way you could always come back to an object and make a lens out of it (or alter the existing lens).
You could register a property "is_lens" on the object so you can mark it as a lens on creation and check this in your panel's poll() method, otherwise your panel would show up on every object in the scene.
Here's a rough sketch of how I would implement this:
https://gist.github.com/Theverat/e8eeb8 ... 42eb002c8b
Support LuxCoreRender project with salts and bounties

CodeHD
Posts: 182
Joined: Tue Dec 11, 2018 12:38 pm

Re: Little Addon for optical lenses etc.

Post by CodeHD » Sun Jun 16, 2019 2:41 pm

Thanks for the many suggestions ;)

I will work a bit more on the addon in the evening, to try and implement it.
I already almost finished adding parabolic mirrors yesterday (including an off-axis option)
OAP.PNG

kintuX
Posts: 464
Joined: Wed Jan 10, 2018 2:37 am

Re: Little Addon for optical lenses etc.

Post by kintuX » Sun Jun 16, 2019 8:55 pm

This looks sweet and yet healthy for the curious mind.

Here's a little helper tool to design & fine tune lenses: Optical Ray Tracer
... and a vide of an experimental adventure - simulating camera obscura, from pinhole to SLR and it's lens system
Jeremy Cox: Down the Rabbit Hole: Adventures in Cinema 4D (with Indigo Renderer)

Always wished to replicate or at least mimic analog stuff (cameras & film) just haven't found the time to do it yet. :cry:
You gave me hope and will to dream of the quest again. :)
Thanks for making it easier!

CodeHD
Posts: 182
Joined: Tue Dec 11, 2018 12:38 pm

Re: Little Addon for optical lenses etc.

Post by CodeHD » Sun Jun 16, 2019 9:17 pm

I didn't start on the material stuff yet, but I finished all stuff about the surfaces I wanted to do do first:
  • I added parabolic surfaces for the mirrors.
  • If parabolic surface is selected, you can also make it an off-axis mirror. It also then allows you to select if the component origin is at the focal point or the mirror center on the surface
  • (Spherical mirror currently ignores the origin and off-axis setting, though. It is always on-axis and origina on mirror center)
  • Both mirror types now work as convex mirrors as well
  • I included "sanity-checks" for the surfaces, so you can't get overlapping faces (at least I found no case). If the surfaces of a lens would overlap, it simply increases the center thickness

kintuX wrote:
Sun Jun 16, 2019 8:55 pm
This looks sweet and yet healthy for the curious mind.

Here's a little helper tool to design & fine tune lenses: Optical Ray Tracer
... and a vide of an experimental adventure - simulating camera obscura, from pinhole to SLR and it's lens system
Jeremy Cox: Down the Rabbit Hole: Adventures in Cinema 4D (with Indigo Renderer)

Always wished to replicate or at least mimic analog stuff (cameras & film) just haven't found the time to do it yet. :cry:
You gave me hope and will to dream of the quest again. :)
Thanks for making it easier!
Thanks! The links look nice, I was looking for a good, free (sequential) ray tracing software for a while :)
Have to watch that presentation some other time though, going to bed now :roll:

Hope you find the time to do it, I imagine it will be a satisfying result ;)

Post Reply