bl_info = {
"name": "Cobweb",
"author": "Vilem Duha",
"version": (2, 1),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh > Cobweb",
"description": "Adds a generative cobweb",
"warning": "",
"wiki_url": "",
"category": "Add Mesh",
}
import bpy, bpy_extras
import bmesh, mathutils
import random, math
from bpy_extras import object_utils, mesh_utils
from bpy_extras import view3d_utils
import time
from bpy.props import *
import bgl, gpu
from gpu_extras.batch import batch_for_shader
def testConnectible(obj, v1, v2):
# ob = bpy.data.objects[obj.name]
# print(obj.name)
# print(obj.data)
# obj.update
vec = v2.co - v1.co
v1hit, v1loc, v1norm, v1face_index = obj.closest_point_on_mesh(v1.co)
# norm = obj.data.polygons[face_index].normal
# print(v1norm.angle(vec))
if v1norm.angle(vec) > 1.57:
return False
v2hit, v2loc, v2norm, v2face_index = obj.closest_point_on_mesh(v2.co)
# vec1 = v1.co-v2.co
# print(v2norm.angle(-vec))
if v2norm.angle(-vec) > 1.57:
return False
hit, loc, norm, face = obj.ray_cast(v1.co + v1norm * 0.001, v2.co - v1.co)
hitvec = loc - v1.co
# print(hitvec.length, vec.length)
return hitvec.length * 1.01 >= vec.length
def testConnectibleConnection(obj, v1, v2):
# ob = bpy.data.objects[obj.name]
# print(obj.name)
# print(obj.data)
# obj.update
vec = v2.co - v1.co
hit, loc, norm, face = obj.ray_cast(v1.co, v2.co - v1.co)
hitvec = loc - v1.co
return not hit or hitvec.length * 1.01 >= vec.length
def mindist(v1, testvarray, maxdist):
mindist = 100000000000000
minv = None
for v2 in testvarray:
vec = v2.co - v1.co
d = vec.length
if d < mindist:
minv = v2
mindist = d
return minv
def addhits(self, context, event, ray_max=100000.0):
"""Run this function on left mouse, execute the ray cast"""
# get the context arguments
scene = context.scene
region = context.region
rv3d = context.region_data
coord = event.mouse_region_x, event.mouse_region_y
# get the ray from the viewport and mouse
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)
ray_target = ray_origin + (view_vector * ray_max)
vec = ray_target - ray_origin
result, hit, normal, face_index, object, matrix = scene.ray_cast(bpy.context.view_layer, ray_origin, vec)
# cast rays and find the closest object
best_length_squared = ray_max * ray_max
best_obj = None
if hit != None:
# scene.cursor_location = hit
self.hits.append((hit, normal))
n = mathutils.Vector((0, 0, 0))
return False
def createmesh(points):
s = bpy.context.scene
mesh = bpy.data.meshes.new("points")
bm = bmesh.new()
for v_co, normal in points:
offset = normal * .005
rv1 = mathutils.Euler((0, 1, 0))
rv2 = mathutils.Euler((1, 0, 0))
rnor = mathutils.Euler(normal)
offset1 = offset.orthogonal()
# offset1.rotate(rv1)
offset2 = offset1.copy()
offset2.rotate(rnor)
v = bm.verts.new(v_co)
v1 = bm.verts.new(v_co + offset1)
v2 = bm.verts.new(v_co + offset2)
f = bm.faces.new((v, v1, v2))
f.select = True
# v = bm.verts.new(v_co)
bm.to_mesh(mesh)
mesh.update()
name = 'cobweb source points'
if name in s.collection.objects:
ob = s.collection.objects[name]
ob.select_set(True)
bpy.context.view_layer.objects.active = ao
ob.data = mesh
else:
object_utils.object_data_add(bpy.context, mesh)
ob = bpy.context.view_layer.objects.active
ob.location = 0, 0, 0
ob.name = name
ob.hide_render = True
ob.hide_viewport = True
def draw_lines(vertices, indices, color):
# bgl.glEnable(bgl.GL_BLEND)
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": vertices}, indices=indices)
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
def draw_callback_3d(self, context):
coords =[]
lines = []
for i, pp in enumerate(self.hits):
# print('draw',pp)
if i > 0:
d = pp[0] - self.hits[i-1][0]
if d.length < .5:
lines.append([i-1,i])
coords.append(pp[0])
draw_lines(coords, lines, (0.5,1,0.5,1))
class CobwebPaint(bpy.types.Operator):
"""paint cobweb source"""
bl_idname = "object.cobweb_paint"
bl_label = "Cobweb paint"
bl_description = "Paint into the scene and press Enter to finish creating a cobweb"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return True # context.active_object is not None
def modal(self, context, event):
context.area.tag_redraw()
# self.report({'OPERATOR'}, "Select 3 or more points on the floor, Esc exits")
if event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE'}:
# allow navigation
return {'PASS_THROUGH'}
if event.type == 'LEFTMOUSE':
if event.value == 'PRESS':
self.drawing = True
else:
self.drawing = False
# print(dir(event))
# print(event.value)
elif event.type == 'MOUSEMOVE':
if self.drawing:
addhits(self, context, event)
return {'RUNNING_MODAL'}
else:
return {'PASS_THROUGH'}
elif event.type in {'RET', 'SPACE'}:
# self.hits=[]
# if bpy.context.active_object.name[:6] == 'cobweb':
# bpy.ops.ed.undo()
createmesh(self.hits)
origmesh = bpy.context.view_layer.objects.active
dim = origmesh.dimensions
s = max(dim.x, dim.y, dim.z)
settings = bpy.context.scene.cobweb_settings
bpy.ops.object.cobweb_add(pointcount=settings.pointcount,
connections=settings.connections,
radius=settings.radius,
pick_close_tries=settings.pick_close_tries,
condist2=settings.condist2,
subdivision=settings.subdivision,
smooth_iterations=settings.smooth_iterations,
enable_viewport_rendering=settings.enable_viewport_rendering,
add_cloth=settings.add_cloth,
drop_amount=settings.drop_amount)
# if event.type in { 'RET', 'SPACE'}:
# bpy.context.scene.objects.unlink(origmesh)
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
context.area.header_text_set(text = None)
msg = self.bl_label + ' (modal)'
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
# self.hits=[]
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
context.area.header_text_set( text = None)
return {'CANCELLED'}
else:
return {'PASS_THROUGH'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
# bpy.context.scene.update()
if context.space_data.type == 'VIEW_3D':
args = (self, context)
self.hits = []
self.drawing = False
# context.window_manager.modal_handler_add(self)
self._handle = bpy.types.
prince_zxill
- 粉丝: 1166
- 资源: 219
最新资源
- 第6节-指针.pdf
- 第5节-操作符详解.pdf
- 第9节-windows版本git的用法.pdf
- 第8节-实用调试技巧.pdf
- JDK17的下载与安装 .pdf
- idm641.exe
- flatpak-libs-1.0.9-13.el7-9.x64-86.rpm.tar.gz
- 不知道minGW64是那个的看点这个.txt
- flex-2.5.37-6.el7.x64-86.rpm.tar.gz
- 3--线性表之-链表.pdf
- 2--线性表之-顺序表.pdf
- 5--树和二叉树.pdf
- 4--线性表之-栈和队列.pdf
- 7--实践练习-迷宫问题.pdf
- Java Access Bridge测试例子,全网唯一的
- flex-devel-2.5.37-6.el7.x64-86.rpm.tar.gz
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈