Skip to content
Snippets Groups Projects
model_primitives_topology.py 39.7 KiB
Newer Older
# SPDX-License-Identifier: GPL-2.0-or-later

# <pep8 compliant>

""" Get POV-Ray specific objects In and Out of Blender """
Maurice Raybaud's avatar
Maurice Raybaud committed
from math import pi, cos, sin
import os.path
Maurice Raybaud's avatar
Maurice Raybaud committed
import bpy
from bpy_extras.object_utils import object_data_add
Maurice Raybaud's avatar
Maurice Raybaud committed
from bpy.utils import register_class, unregister_class
from bpy.types import Operator
    StringProperty,
    BoolProperty,
    IntProperty,
    FloatProperty,
    FloatVectorProperty,
    EnumProperty,
from mathutils import Vector, Matrix

from . import model_primitives
class POV_OT_lathe_add(Operator):
Maurice Raybaud's avatar
Maurice Raybaud committed
    """Add the representation of POV lathe using a screw modifier."""
    bl_idname = "pov.addlathe"
    bl_label = "Lathe"
    bl_description = "adds lathe"
    COMPAT_ENGINES = {"POVRAY_RENDER"}

        # ayers=[False]*20
        # layers[0]=True
Campbell Barton's avatar
Campbell Barton committed
        bpy.ops.curve.primitive_bezier_curve_add(
            location=context.scene.cursor.location,
Campbell Barton's avatar
Campbell Barton committed
            rotation=(0, 0, 0),
        ob = context.view_layer.objects.active
Campbell Barton's avatar
Campbell Barton committed
        ob_data = ob.data
        ob.name = ob_data.name = "PovLathe"
        ob_data.dimensions = "2D"
        ob_data.transform(Matrix.Rotation(-pi / 2.0, 4, "Z"))
        ob.pov.object_as = "LATHE"
Maurice Raybaud's avatar
Maurice Raybaud committed
        self.report(
            {"INFO"}, "This native POV-Ray primitive" "won't have any vertex to show in edit mode"
        bpy.ops.object.modifier_add(type="SCREW")
Campbell Barton's avatar
Campbell Barton committed
        mod = ob.modifiers[-1]
Campbell Barton's avatar
Campbell Barton committed
        mod.show_render = False
    """Create the proxy mesh of a POV superellipsoid using pov_superellipsoid_define()."""
Maurice Raybaud's avatar
Maurice Raybaud committed
    if op:
        mesh = None
Maurice Raybaud's avatar
Maurice Raybaud committed
        u = op.se_u
        v = op.se_v
        n1 = op.se_n1
        n2 = op.se_n2
        edit = op.se_edit
        se_param1 = n2  # op.se_param1
        se_param2 = n1  # op.se_param2
Maurice Raybaud's avatar
Maurice Raybaud committed
    else:
Maurice Raybaud's avatar
Maurice Raybaud committed
        mesh = ob.data
Maurice Raybaud's avatar
Maurice Raybaud committed
        u = ob.pov.se_u
        v = ob.pov.se_v
        n1 = ob.pov.se_n1
        n2 = ob.pov.se_n2
        edit = ob.pov.se_edit
        se_param1 = ob.pov.se_param1
        se_param2 = ob.pov.se_param2

    verts = []
    stepSegment = 360 / v * pi / 180
    stepRing = pi / u
    angSegment = 0
    angRing = -pi / 2
    step = 0
    for ring in range(0, u - 1):
Maurice Raybaud's avatar
Maurice Raybaud committed
        angRing += stepRing
        for segment in range(0, v):
Maurice Raybaud's avatar
Maurice Raybaud committed
            step += 1
            angSegment += stepSegment
            x = r * (abs(cos(angRing)) ** n1) * (abs(cos(angSegment)) ** n2)
            if (cos(angRing) < 0 < cos(angSegment)) or (cos(angRing) > 0 > cos(angSegment)):
Maurice Raybaud's avatar
Maurice Raybaud committed
                x = -x
            y = r * (abs(cos(angRing)) ** n1) * (abs(sin(angSegment)) ** n2)
            if (cos(angRing) < 0 < sin(angSegment)) or (cos(angRing) > 0 > sin(angSegment)):
Maurice Raybaud's avatar
Maurice Raybaud committed
                y = -y
            z = r * (abs(sin(angRing)) ** n1)
Maurice Raybaud's avatar
Maurice Raybaud committed
            if sin(angRing) < 0:
                z = -z
            x = round(x, 4)
            y = round(y, 4)
            z = round(z, 4)
            verts.append((x, y, z))
    if edit == "TRIANGLES":
        verts.extend([(0, 0, 1),(0, 0, -1)])
    for i in range(0, u - 2):
        m = i * v
        for p in range(0, v):
            if p < v - 1:
                face = (m + p, 1 + m + p, v + 1 + m + p, v + m + p)
            if p == v - 1:
                face = (m + p, m, v + m, v + m + p)
        indexUp = len(verts) - 2
        indexDown = len(verts) - 1
        indexStartDown = len(verts) - 2 - v
        for i in range(0, v):
            if i < v - 1:
                face = (indexDown, i, i + 1)
Maurice Raybaud's avatar
Maurice Raybaud committed
                faces.append(face)
            if i == v - 1:
                face = (indexDown, i, 0)
Maurice Raybaud's avatar
Maurice Raybaud committed
                faces.append(face)
        for i in range(0, v):
            if i < v - 1:
                face = (indexUp, i + indexStartDown, i + indexStartDown + 1)
Maurice Raybaud's avatar
Maurice Raybaud committed
                faces.append(face)
            if i == v - 1:
                face = (indexUp, i + indexStartDown, indexStartDown)
Maurice Raybaud's avatar
Maurice Raybaud committed
                faces.append(face)
    if edit == "NGONS":
        face = list(range(v))
Maurice Raybaud's avatar
Maurice Raybaud committed
        faces.append(face)
        face = []
        indexUp = len(verts) - 1
        for i in range(0, v):
            face.append(indexUp - i)
Maurice Raybaud's avatar
Maurice Raybaud committed
        faces.append(face)
    mesh = model_primitives.pov_define_mesh(mesh, verts, [], faces, "SuperEllipsoid")
Maurice Raybaud's avatar
Maurice Raybaud committed
    if not ob:
Maurice Raybaud's avatar
Maurice Raybaud committed
        ob = object_data_add(context, mesh, operator=None)
        # engine = context.scene.render.engine what for?
Maurice Raybaud's avatar
Maurice Raybaud committed
        ob = context.object
        ob.name = ob.data.name = "PovSuperellipsoid"
Maurice Raybaud's avatar
Maurice Raybaud committed
        ob.pov.se_param1 = n2
        ob.pov.se_param2 = n1

        ob.pov.se_u = u
        ob.pov.se_v = v
        ob.pov.se_n1 = n1
        ob.pov.se_n2 = n2
        ob.pov.se_edit = edit

        bpy.ops.object.mode_set(mode="EDIT")
        bpy.ops.mesh.hide(unselected=False)
        bpy.ops.object.mode_set(mode="OBJECT")
        ob.data.auto_smooth_angle = 1.3
        bpy.ops.object.shade_smooth()
        ob.pov.object_as = "SUPERELLIPSOID"
class POV_OT_superellipsoid_add(Operator):
    """Add the representation of POV superellipsoid using the pov_superellipsoid_define()."""
    bl_idname = "pov.addsuperellipsoid"
    bl_label = "Add SuperEllipsoid"
    bl_description = "Create a SuperEllipsoid"
    bl_options = {'REGISTER', 'UNDO'}
    COMPAT_ENGINES = {"POVRAY_RENDER"}
    # Keep in sync within model_properties.py section Superellipsoid
Maurice Raybaud's avatar
Maurice Raybaud committed
    # as this allows interactive update
    #     If someone knows how to define operators' props from a func, I'd be delighted to learn it!
    # XXX ARE the first two used for import ? could we hide or suppress them otherwise?
Maurice Raybaud's avatar
Maurice Raybaud committed
    se_param1: FloatProperty(name="Parameter 1", description="", min=0.00, max=10.0, default=0.04)
Maurice Raybaud's avatar
Maurice Raybaud committed
    se_param2: FloatProperty(name="Parameter 2", description="", min=0.00, max=10.0, default=0.04)
Maurice Raybaud's avatar
Maurice Raybaud committed
        name="U-segments", description="radial segmentation", default=20, min=4, max=265
Maurice Raybaud's avatar
Maurice Raybaud committed
        name="V-segments", description="lateral segmentation", default=20, min=4, max=265
Loading
Loading full blame...