Skip to content

Instantly share code, notes, and snippets.

@Theverat
Last active June 16, 2019 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Theverat/e8eeb810d71e5d90e7f2e842eb002c8b to your computer and use it in GitHub Desktop.
Save Theverat/e8eeb810d71e5d90e7f2e842eb002c8b to your computer and use it in GitHub Desktop.
from bpy.types import PropertyGroup
from bpy.props import PointerProperty
def add_lens(obj):
""" opticore is an instance of the OptiCoreProps PropertyGroup """
opticore = obj.opticore
rad = opticore.rad
...
# Replace mesh
...
# Set material
if obj.material_slots:
obj.material_slots[obj.active_material_index].material = opticore.material
else:
obj.data.materials.append(opticore.material)
class OBJECT_OT_add_lens(Operator, AddObjectHelper):
"""Create a new Mesh Object"""
bl_idname = "mesh.add_lens"
bl_label = "Add Mesh Object"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if not obj:
# Create object (without mesh)
obj = ...
# Mark as lens, so in the future our custom panel is displayed on this object
obj.opticore.is_lens = True
# Generate the mesh
add_lens(obj)
return {'FINISHED'}
class OPTICORE_OT_apply_lens(Operator):
bl_idname = "opticore.apply_lens"
bl_label = "Apply"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
obj = context.object
mesh = obj.data
# Delete mesh vertices and re-create them
add_lens(obj)
return {'FINISHED'}
class OptiCoreProps(PropertyGroup):
rad = FloatProperty(
name="Surface 1 Radius",
default = 12.,
description="Radius of Curvature of Mirror Surface",
)
...
material = PointerProperty(type=bpy.types.Material)
is_lens = BoolProperty() # Not visible in UI
class OptiCorePanel(bpy.types.Panel):
bl_label = "Opticore Settings"
bl_context = "object"
@classmethod
def poll(cls, context):
# Check if it's a lens object
return context.object and not context.object.library and context.object.opticore.is_lens
def draw(self, context):
layout = self.layout
opticore = context.object.opticore
layout.prop(opticore, "rad")
...
# Don't need prop_search because we can now use a PointerProperty
layout.prop(opticore, "material")
# Apply button
layout.operator("opticore.apply_lens")
def register():
bpy.utils.register_class(OptiCoreProps)
bpy.utils.register_class(OptiCorePanel)
bpy.types.Object.opticore = PointerProperty(type=OptiCoreProps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment