Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/split_solidify.py
Created April 9, 2014 00:02
Show Gist options
  • Save zeffii/10211145 to your computer and use it in GitHub Desktop.
Save zeffii/10211145 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****
# ------ ------
bl_info = {
'name': 'split_solidify',
'author': '',
'version': (0, 1, 1),
'blender': (2, 6, 1),
'api': 43085,
'location': 'View3D > Tool Shelf',
'description': '',
'warning': '',
'wiki_url': '',
'tracker_url': '',
'category': 'Mesh'
}
# ------ ------
import bmesh
import bpy
from bpy.props import EnumProperty, FloatProperty, BoolProperty
import random
from math import cos
def edit_mode_out():
bpy.ops.object.mode_set(mode='OBJECT')
def edit_mode_in():
bpy.ops.object.mode_set(mode='EDIT')
def f_(bm, me, list_0, b_rnd, rnd, opp, th, en0):
''' fi are face indices '''
for fi in list_0:
list_1, list_2 = [], []
f = bm.faces[fi]
if b_rnd:
d = rnd * random.randrange(0, 10)
else:
d = opp
for vi in f.verts:
vidx = vi.index
v = bm.verts[vidx]
if en0 == 'opt0':
p1 = (v.co).copy() + ((f.normal).copy() * d)
p2 = (v.co).copy() + ((f.normal).copy() * (d - th))
elif en0 == 'opt1':
ang = ((v.normal).copy()).angle((f.normal).copy())
h = th / cos(ang)
p1 = (v.co).copy() + ((f.normal).copy() * d)
p2 = p1 + (-h * (v.normal).copy())
bm.verts.new((p1))
bm.verts.new((p2))
list_1.append(bm.verts[-1].index)
list_2.append(bm.verts[-2].index)
bm_seq = lambda k: tuple(bm.verts[i] for i in k)
n = len(list_1)
if n == 3:
# top
bm.faces.new(bm_seq(list_1))
# sides
for i in range(n):
j = (i+1) % n
idxs = list_1[i], list_2[i], list_2[j], list_1[j]
bm.faces.new(bm_seq(idxs))
# bottom
list_2.reverse()
bm.faces.new(bm_seq(list_2))
elif n == 4:
# top
bm.faces.new(bm_seq(list_1))
# sides
for i in range(n):
j = (i+1) % n
idxs = list_1[i], list_2[i], list_2[j], list_1[j]
bm.faces.new(bm_seq(idxs))
# ends
idxs = list_2[0], list_2[3], list_2[2], list_2[1]
bm.faces.new(bm_seq(idxs))
bmesh.update_edit_mesh(me, True)
class sp_sol_p0(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
#bl_idname = 'sp_sol_p0_id'
bl_label = 'Split Solidify'
bl_context = 'mesh_edit'
def draw(self, context):
layout = self.layout
layout.operator('sp_sol.op0_id', text='Split solidify')
class sp_sol_op0(bpy.types.Operator):
bl_idname = 'sp_sol.op0_id'
bl_label = 'Split Solidify'
bl_options = {'REGISTER', 'UNDO'}
opp = FloatProperty(
name='', default=.4, min=-100, max=100, step=1, precision=3)
th = FloatProperty(
name='', default=.04, min=-100, max=100, step=1, precision=3)
rnd = FloatProperty(
name='', default=.06, min=-10, max=10, step=1, precision=3)
b_rnd = BoolProperty(name='Random', default=False)
b_del = BoolProperty(name='Delete original faces', default=True)
en0 = EnumProperty(
items=(('opt0', 'Face', ''), ('opt1', 'Vertex', '')),
name='Normal',
default='opt0')
def draw(self, context):
layout = self.layout
layout.label('Normal:')
layout.prop(self, 'en0', expand=True)
layout.prop(self, 'b_rnd')
if not self.b_rnd:
layout.label('Distance:')
layout.prop(self, 'opp')
elif self.b_rnd:
layout.label('Random distance:')
layout.prop(self, 'rnd')
layout.label('Thickness:')
layout.prop(self, 'th')
layout.prop(self, 'b_del')
def execute(self, context):
b_rnd = self.b_rnd
rnd = self.rnd
opp = self.opp
th = self.th
b_del = self.b_del
en0 = self.en0
ob_act = context.active_object
me = ob_act.data
bm = bmesh.from_edit_mesh(me)
me.update()
list_0 = [f.index for f in bm.faces if f.select]
if len(list_0) == 0:
self.report({'INFO'}, 'No faces selected')
return {'CANCELLED'}
elif len(list_0) != 0:
f_(bm, me, list_0, b_rnd, rnd, opp, th, en0)
if b_del:
bpy.ops.mesh.delete(type='FACE')
else:
pass
return {'FINISHED'}
class_list = [sp_sol_op0, sp_sol_p0]
def register():
for c in class_list:
bpy.utils.register_class(c)
def unregister():
for c in class_list:
bpy.utils.unregister_class(c)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment