Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- from PIL import Image
- def create_ksp_craft(image_path, output_path, plate_size=1.25, spacing=1.3):
- """
- Create a KSP craft file from a black and white image.
- Args:
- image_path (str): Path to the input image file
- output_path (str): Path to save the .craft file
- plate_size (float): Size of each structural plate (default 1.25m)
- spacing (float): Multiplier for spacing between plates (default 1.3)
- """
- try:
- # Open the image and convert to grayscale
- img = Image.open(image_path).convert('L')
- width, height = img.size
- # Threshold to black and white (using 128 as threshold)
- pixels = img.load()
- # Count white pixels to determine part count
- white_pixels = 0
- for y in range(height):
- for x in range(width):
- if pixels[x, y] > 128:
- white_pixels += 1
- # Craft file header
- craft_content = f"""ship = Untitled Space Craft
- version = 1.12.3
- description = Image generated craft
- type = VAB
- size = 1,1,1
- PART
- {{
- part = Mark1Cockpit_4294711796
- pos = 0,0,0
- rot = 0,0,0,1
- attRot = 0,0,0,1
- mir = 1,1,1
- istg = 0
- dstg = 0
- sidx = -1
- sqor = -1
- link = dockingPort2_4294711568
- attm = 1
- srfN = , -1
- mass = 1.25
- temp = 300
- flTemp = 300
- PRDT
- {{
- name = ModulePartVariants
- useVariantMass = True
- selectedVariant = 0
- }}
- }}
- PART
- {{
- part = dockingPort2_4294711568
- pos = 0,1.25,0
- rot = 0,0,0,1
- attRot = 0,0,0,1
- mir = 1,1,1
- istg = 0
- dstg = 0
- sidx = -1
- sqor = -1
- attN = top, Mark1Cockpit_4294711796
- attm = 1
- srfN = , -1
- mass = 0.05
- temp = 300
- flTemp = 300
- }}
- """
- # Generate structural plates for each white pixel
- part_num = 2 # Starting after cockpit and docking port
- plate_spacing = plate_size * spacing
- for y in range(height):
- for x in range(width):
- if pixels[x, y] > 128: # White pixel
- # Calculate position relative to craft center
- x_pos = (x - width/2) * plate_spacing
- z_pos = (y - height/2) * plate_spacing
- craft_content += f"""PART
- {{
- part = structuralPanel1_429471{1000 + part_num}
- pos = {x_pos:.2f},1.25,{z_pos:.2f}
- rot = 0,0,0,1
- attRot = 0,0,0,1
- mir = 1,1,1
- istg = 0
- dstg = 0
- sidx = -1
- sqor = -1
- link = dockingPort2_4294711568
- attm = 1
- srfN = , -1
- mass = 0.075
- temp = 300
- flTemp = 300
- }}
- """
- part_num += 1
- # Save the craft file
- with open(output_path, 'w') as f:
- f.write(craft_content)
- print(f"Successfully created craft file at {output_path}")
- print(f"Image size: {width}x{height} pixels")
- print(f"Structural plates used: {white_pixels}")
- except Exception as e:
- print(f"Error: {e}")
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description='Create a KSP craft file from a black and white image.')
- parser.add_argument('input_image', help='Path to the input black and white image')
- parser.add_argument('output_craft', help='Path to save the .craft file')
- args = parser.parse_args()
- create_ksp_craft(args.input_image, args.output_craft)
Advertisement
Add Comment
Please, Sign In to add comment