Little Addon for optical lenses etc.
Little Addon for optical lenses etc.
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.
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 :
https://github.com/CodeFHD/OptiCore
Here is an image with a low vertex-count to show how the surfaces are generated:
It is basically a day worth of programming so far, so don't expect too much I plan to add more features like parabolic mirrors, off-axis mirrors or aspheric lenses.
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.
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 :
https://github.com/CodeFHD/OptiCore
Here is an image with a low vertex-count to show how the surfaces are generated:
It is basically a day worth of programming so far, so don't expect too much I plan to add more features like parabolic mirrors, off-axis mirrors or aspheric lenses.
Re: Little Addon for optical lenses etc.
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.
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.
Re: Little Addon for optical lenses etc.
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 (?)
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 (?)
Re: Little Addon for optical lenses etc.
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)
It looks like this:
Re: Little Addon for optical lenses etc.
Ok, thanks for the example! I will start from there
Re: Little Addon for optical lenses etc.
You can add a dropdown in the UI like this:
Code: Select all
layout.prop_search(self, "material_name",
bpy.data, "materials",
icon="NONE")
I found no way yet to combine the default layout generated by Blender when the draw() method is missing with a custom layout.
Yes, you can't create a modifier with Python.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 (?)
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.
Re: Little Addon for optical lenses etc.
Here's a rough sketch of how I would implement this: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.
https://gist.github.com/Theverat/e8eeb8 ... 42eb002c8b
Re: Little Addon for optical lenses etc.
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)
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)
Re: Little Addon for optical lenses etc.
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.
You gave me hope and will to dream of the quest again.
Thanks for making it easier!
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.
You gave me hope and will to dream of the quest again.
Thanks for making it easier!
Re: Little Addon for optical lenses etc.
I didn't start on the material stuff yet, but I finished all stuff about the surfaces I wanted to do do first:
Have to watch that presentation some other time though, going to bed now
Hope you find the time to do it, I imagine it will be a satisfying result
- 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
Thanks! The links look nice, I was looking for a good, free (sequential) ray tracing software for a whilekintuX 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.
You gave me hope and will to dream of the quest again.
Thanks for making it easier!
Have to watch that presentation some other time though, going to bed now
Hope you find the time to do it, I imagine it will be a satisfying result