Files
2026-11-29 15:49:17 +03:00

37 lines
2.4 KiB
Python

import bpy, math, os, bmesh
scene=bpy.context.scene
coll=next((c for c in scene.collection.children if c.name.startswith('Rifle V4')),None)
assert coll, 'Open Reference Sculpt V4 scene first'
old=[o for o in coll.objects if o.name.startswith(('Muzzle body','Muzzle inner emitter'))]
assert old, 'Original muzzle not found'
material=old[0].data.materials[0]
# Closed annular mesh: the front face bridges outer and inner walls, never the bore.
# Inner wall extends behind the muzzle into the barrel; the rear opening stays open.
cy=(490-441)/180
sections=[((1477-880)/180,.26),((1500-880)/180,.35),((1703-880)/180,.35),((1720-880)/180,.30),((1720-880)/180,.20),((1698-880)/180,.185),((1240-880)/180,.125),((1240-880)/180,.15),((1477-880)/180,.15)]
n=64
verts=[(x,math.cos(2*math.pi*i/n)*r,cy+math.sin(2*math.pi*i/n)*r) for x,r in sections for i in range(n)]
faces=[]
for j in range(len(sections)):
nxt=(j+1)%len(sections)
for i in range(n):faces.append((j*n+i,j*n+(i+1)%n,nxt*n+(i+1)%n,nxt*n+i))
mesh=bpy.data.meshes.new('Hollow muzzle mesh');mesh.from_pydata(verts,[],faces);mesh.update()
bm=bmesh.new();bm.from_mesh(mesh);bmesh.ops.recalc_face_normals(bm,faces=bm.faces);bm.to_mesh(mesh);bm.free()
obj=bpy.data.objects.new('Round muzzle with deep bore',mesh);coll.objects.link(obj);mesh.materials.append(material)
for p in mesh.polygons:p.use_smooth=True
bevel=obj.modifiers.new('Rim bevel','BEVEL');bevel.width=.012;bevel.segments=3
obj.modifiers.new('Weighted normals','WEIGHTED_NORMAL')
# Bore also passes through the existing barrel and collar rather than ending at a solid cap.
bpy.ops.mesh.primitive_cylinder_add(vertices=64,radius=.125,depth=3.2,location=((1480-880)/180,0,cy),rotation=(0,math.pi/2,0))
cutter=bpy.context.object;cutter.name='Bore cutter'
for ob in list(coll.objects):
if ob.name.startswith(('Barrel','Muzzle energy collar')):
mod=ob.modifiers.new('Continuous bore','BOOLEAN');mod.operation='DIFFERENCE';mod.solver='EXACT';mod.object=cutter
bpy.context.view_layer.objects.active=ob;bpy.ops.object.modifier_apply(modifier=mod.name)
bpy.data.objects.remove(cutter,do_unlink=True)
for ob in old:bpy.data.objects.remove(ob,do_unlink=True)
bpy.ops.object.select_all(action='DESELECT');obj.select_set(True);bpy.context.view_layer.objects.active=obj
path=r'C:\roblox\hunter\3D models\sniper_reference_v5.blend'
bpy.ops.wm.save_as_mainfile(filepath=path)
print('Saved',path,'Bore depth',sections[4][0]-sections[6][0],'Mesh faces',len(mesh.polygons))