import bpy, math from mathutils import Vector bpy.ops.object.select_all(action='SELECT') bpy.ops.object.delete(use_global=False) def cube(name, loc, scale, bevel=0.08, mat=None): bpy.ops.mesh.primitive_cube_add(location=loc) o=bpy.context.object; o.name=name; o.scale=scale bpy.ops.object.transform_apply(location=False, rotation=False, scale=True) if bevel: m=o.modifiers.new('Edge bevel','BEVEL'); m.width=bevel; m.segments=2 if mat: o.data.materials.append(mat) return o def mat(name, color, metallic=0.0, rough=0.5): m=bpy.data.materials.new(name); m.diffuse_color=(*color,1); m.metallic=metallic; m.roughness=rough; return m steel=mat('Blade steel',(0.16,0.20,0.24),0.85,0.22) dark=mat('Grip leather',(0.045,0.025,0.018),0.0,0.7) gold=mat('Guard metal',(0.32,0.20,0.06),0.75,0.3) # sword lies along +Z, origin at grip center cube('Blade',(0,0,1.75),(0.16,0.045,1.55),0.04,steel) cube('BladeTip',(0,0,3.38),(0.02,0.045,0.12),0.02,steel) cube('CentralRidge',(0,0.06,1.75),(0.035,0.018,1.38),0.015,steel) cube('Crossguard',(0,0,0.12),(0.72,0.10,0.10),0.08,gold) cube('Grip',(0,0,-0.48),(0.11,0.11,0.55),0.06,dark) bpy.ops.mesh.primitive_uv_sphere_add(segments=16, ring_count=8, location=(0,0,-1.12), scale=(0.22,0.22,0.22)) p=bpy.context.object; p.name='Pommel'; p.data.materials.append(gold) # simple grip bands for z in [-0.88,-0.60,-0.32]: cube('GripBand',(0,0,z),(0.14,0.14,0.025),0.015,gold) # rotate for presentation, keep object axes consistent bpy.ops.object.select_all(action='SELECT') bpy.context.view_layer.objects.active=bpy.context.scene.objects['Blade'] # ground and camera bpy.ops.mesh.primitive_plane_add(size=12, location=(0,0,-1.35)) plane=bpy.context.object; plane.name='PreviewGround'; plane.data.materials.append(mat('Ground',(0.025,0.025,0.03),0,0.8)) bpy.ops.object.camera_add(location=(6,-8,4), rotation=(math.radians(68),0,math.radians(37))) cam=bpy.context.object; bpy.context.scene.camera=cam bpy.ops.object.light_add(type='AREA', location=(3,-4,6)); bpy.context.object.data.energy=900; bpy.context.object.data.shape='DISK'; bpy.context.object.data.size=5 bpy.ops.object.light_add(type='AREA', location=(-4,2,2)); bpy.context.object.data.energy=500; bpy.context.object.data.size=4 scene=bpy.context.scene; scene.render.engine='BLENDER_EEVEE'; scene.render.resolution_x=600; scene.render.resolution_y=800; scene.render.resolution_percentage=100 scene.render.filepath='C:/Users/huade/Desktop/unity/blender/greatsword_preview.png' bpy.ops.wm.save_as_mainfile(filepath='C:/Users/huade/Desktop/unity/blender/大剑.blend') bpy.ops.render.render(write_still=True)