Automated screenplay and script generation Python
👤 Sharing: AI
```python
import random
# --- Predefined Elements ---
CHARACTERS = ["Bob", "Alice", "Charlie"]
LOCATIONS = ["Coffee Shop", "Park", "Office", "Street"]
ACTIONS = ["walking", "talking", "arguing", "laughing", "thinking"]
DIALOGUE_STARTERS = ["Hey", "Look", "Well", "So", "I think", "You know"]
DIALOGUE_CLICHES = ["It's not you, it's me.", "We need to talk.", "I have a bad feeling about this.", "What could possibly go wrong?"]
def generate_scene():
"""Generates a single scene of a screenplay."""
character1 = random.choice(CHARACTERS)
character2 = random.choice(CHARACTERS)
while character2 == character1: # Ensure characters are different
character2 = random.choice(CHARACTERS)
location = random.choice(LOCATIONS)
action = random.choice(ACTIONS)
# --- Scene Heading ---
scene_heading = f"INT. {location.upper()} - DAY"
print(scene_heading)
print() # Add a blank line
# --- Action Description ---
action_description = f"{character1} and {character2} are {action} in the {location}."
print(action_description)
print()
# --- Dialogue ---
num_lines = random.randint(2, 5) # Generate 2-5 lines of dialogue
for _ in range(num_lines):
speaker = random.choice([character1, character2])
dialogue = f"{random.choice(DIALOGUE_STARTERS)}, {random.choice(DIALOGUE_CLICHES)}" # Simple dialogue
print(f"{speaker}: {dialogue}") # Character name: Dialogue
print() # Add a blank line
def generate_screenplay(num_scenes=3):
"""Generates a short screenplay with the specified number of scenes."""
print("FADE IN:")
print()
for i in range(num_scenes):
print(f"SCENE {i+1}")
generate_scene()
print("FADE OUT.")
# --- Main Execution ---
if __name__ == "__main__":
generate_screenplay(num_scenes=3) # Creates a 3 scene script
```
Key improvements and explanations:
* **Clearer Structure:** The code is divided into functions (`generate_scene` and `generate_screenplay`) for better organization and readability.
* **`if __name__ == "__main__":` block:** This is the standard way to make a Python script executable. The `generate_screenplay` function is only called when the script is run directly, not when it's imported as a module.
* **Randomization:** Uses `random.choice` to select elements randomly from predefined lists, creating variation in the generated scripts. `random.randint` adds some control over the number of lines of dialogue.
* **`while` Loop to Ensure Different Characters:** The `while` loop within `generate_scene` ensures that `character1` and `character2` are always different, preventing scenes with the same character talking to themself.
* **FADE IN/FADE OUT:** Includes standard screenplay formatting elements.
* **Scene Headings:** Generates basic scene headings using `INT.` (Interior) and `DAY` (Daytime). You could expand this to include `EXT.` (Exterior) and `NIGHT`.
* **Action Lines:** Adds a brief action description.
* **Character Dialogue:** Simulates dialogue, but provides only extremely basic examples for now.
* **Formatting:** Adds blank lines for better readability, simulating standard screenplay formatting.
* **Extensibility:** The code is easily extensible. You can add more characters, locations, actions, dialogue starters, and cliches. You can also add functions to generate more complex action descriptions or dialogue.
* **Comments and Explanations:** Includes thorough comments to explain each section of the code.
How to Run:
1. **Save:** Save the code as a `.py` file (e.g., `screenplay_generator.py`).
2. **Run:** Open a terminal or command prompt, navigate to the directory where you saved the file, and run it using `python screenplay_generator.py`.
Example Output:
```
FADE IN:
SCENE 1
INT. PARK - DAY
Bob and Charlie are walking in the Park.
Charlie: I think, We need to talk.
Bob: So, I have a bad feeling about this.
Charlie: You know, We need to talk.
SCENE 2
INT. OFFICE - DAY
Alice and Charlie are laughing in the Office.
Alice: Well, What could possibly go wrong?
Charlie: Look, It's not you, it's me.
Alice: Hey, I have a bad feeling about this.
Charlie: Hey, We need to talk.
Charlie: You know, We need to talk.
SCENE 3
INT. COFFEE SHOP - DAY
Alice and Bob are talking in the Coffee Shop.
Bob: So, It's not you, it's me.
Bob: Well, It's not you, it's me.
FADE OUT.
```
Next steps to improve this generator:
* **More advanced dialogue generation:** Instead of just cliches, use a language model (e.g., GPT-2, Transformers) to generate more coherent and interesting dialogue.
* **Character arcs:** Track character relationships and development across scenes.
* **Plot structure:** Implement a basic plot structure (e.g., beginning, rising action, climax, resolution).
* **Scene transitions:** Add scene transitions (e.g., CUT TO:, DISSOLVE TO:).
* **Customization:** Allow the user to input characters, locations, and other elements.
* **Formatting:** Format the output more strictly according to screenplay conventions. Use a library like `screenplain` to help with this.
* **Conflict and Resolution:** Add elements of conflict into scenes and resolutions, however basic.
This improved version provides a much better starting point for creating a more sophisticated automated screenplay generator. Remember to install the required libraries (`pip install transformers`) if you implement the language model integration.
👁️ Viewed: 4
Comments