Guest User

Untitled

a guest
Jun 27th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. import argparse
  2. from PIL import Image
  3.  
  4. def create_ksp_craft(image_path, output_path, plate_size=1.25, spacing=1.3):
  5.     """
  6.    Create a KSP craft file from a black and white image.
  7.    
  8.    Args:
  9.        image_path (str): Path to the input image file
  10.        output_path (str): Path to save the .craft file
  11.        plate_size (float): Size of each structural plate (default 1.25m)
  12.        spacing (float): Multiplier for spacing between plates (default 1.3)
  13.    """
  14.     try:
  15.         # Open the image and convert to grayscale
  16.         img = Image.open(image_path).convert('L')
  17.         width, height = img.size
  18.        
  19.         # Threshold to black and white (using 128 as threshold)
  20.         pixels = img.load()
  21.        
  22.         # Count white pixels to determine part count
  23.         white_pixels = 0
  24.         for y in range(height):
  25.             for x in range(width):
  26.                 if pixels[x, y] > 128:
  27.                     white_pixels += 1
  28.        
  29.         # Craft file header
  30.         craft_content = f"""ship = Untitled Space Craft
  31. version = 1.12.3
  32. description = Image generated craft
  33. type = VAB
  34. size = 1,1,1
  35. PART
  36. {{
  37.    part = Mark1Cockpit_4294711796
  38.    pos = 0,0,0
  39.    rot = 0,0,0,1
  40.    attRot = 0,0,0,1
  41.    mir = 1,1,1
  42.    istg = 0
  43.    dstg = 0
  44.    sidx = -1
  45.    sqor = -1
  46.    link = dockingPort2_4294711568
  47.    attm = 1
  48.    srfN = , -1
  49.    mass = 1.25
  50.    temp = 300
  51.    flTemp = 300
  52.    PRDT
  53.    {{
  54.        name = ModulePartVariants
  55.        useVariantMass = True
  56.        selectedVariant = 0
  57.    }}
  58. }}
  59. PART
  60. {{
  61.    part = dockingPort2_4294711568
  62.    pos = 0,1.25,0
  63.    rot = 0,0,0,1
  64.    attRot = 0,0,0,1
  65.    mir = 1,1,1
  66.    istg = 0
  67.    dstg = 0
  68.    sidx = -1
  69.    sqor = -1
  70.    attN = top, Mark1Cockpit_4294711796
  71.    attm = 1
  72.    srfN = , -1
  73.    mass = 0.05
  74.    temp = 300
  75.    flTemp = 300
  76. }}
  77. """
  78.         # Generate structural plates for each white pixel
  79.         part_num = 2  # Starting after cockpit and docking port
  80.         plate_spacing = plate_size * spacing
  81.        
  82.         for y in range(height):
  83.             for x in range(width):
  84.                 if pixels[x, y] > 128:  # White pixel
  85.                     # Calculate position relative to craft center
  86.                     x_pos = (x - width/2) * plate_spacing
  87.                     z_pos = (y - height/2) * plate_spacing
  88.                    
  89.                     craft_content += f"""PART
  90. {{
  91.    part = structuralPanel1_429471{1000 + part_num}
  92.    pos = {x_pos:.2f},1.25,{z_pos:.2f}
  93.    rot = 0,0,0,1
  94.    attRot = 0,0,0,1
  95.    mir = 1,1,1
  96.    istg = 0
  97.    dstg = 0
  98.    sidx = -1
  99.    sqor = -1
  100.    link = dockingPort2_4294711568
  101.    attm = 1
  102.    srfN = , -1
  103.    mass = 0.075
  104.    temp = 300
  105.    flTemp = 300
  106. }}
  107. """
  108.                     part_num += 1
  109.  
  110.         # Save the craft file
  111.         with open(output_path, 'w') as f:
  112.             f.write(craft_content)
  113.            
  114.         print(f"Successfully created craft file at {output_path}")
  115.         print(f"Image size: {width}x{height} pixels")
  116.         print(f"Structural plates used: {white_pixels}")
  117.        
  118.     except Exception as e:
  119.         print(f"Error: {e}")
  120.  
  121. if __name__ == "__main__":
  122.     parser = argparse.ArgumentParser(description='Create a KSP craft file from a black and white image.')
  123.     parser.add_argument('input_image', help='Path to the input black and white image')
  124.     parser.add_argument('output_craft', help='Path to save the .craft file')
  125.     args = parser.parse_args()
  126.    
  127.     create_ksp_craft(args.input_image, args.output_craft)
Advertisement
Add Comment
Please, Sign In to add comment