Quick Startο
Get up and running with KnitScript in just a few minutes! This guide will walk you through creating your first knitting pattern.
π― Your First Patternο
Letβs create a simple stockinette pattern to get familiar with KnitScript syntax.
Basic Stockinetteο
Create a file called stockinette.ks
:
// Simple stockinette scarf pattern
width = 10;
with Carrier as c1:{
in Leftward direction:{
tuck Front_Needles[0:width:2];
}
in reverse direction:{
tuck Front_Needles[1:width:2];
}
for _ in range(10):{
in reverse direction:{
knit Loops;
}
}
}
cut c1;
Now convert it to knitout:
from knit_script import knit_script_to_knitout
# Convert pattern to knitout
knit_graph, machine = knit_script_to_knitout(
pattern="stockinette.ks",
out_file_name="stockinette.k"
)
print(f"β
Generated the stockinette pattern")
π§Ά Understanding the Patternο
Letβs break down what each part does:
Variable Declarationο
width = 10;
This creates a variable width
that we can use throughout our pattern.
Carrier Managementο
with Carrier as c1:{
// pattern code here
}
The with Carrier as c1
block:
* Sets carrier 1 as the active working carrier
* Automatically handles inhook operations when needed
* Scopes the carrier setting to this block
Directional Operationsο
in Leftward direction:{
tuck Front_Needles[0:width:2];
}
The in direction
block:
* Sets the carriage pass direction
* Groups multiple operations into a single carriage pass
* Organizes operations to be sorted by the given direction
Needle Selectionο
Front_Needles[0:width:2] // Even Needles 0 through 9 on front bed
Loops // All needles currently holding loops
KnitScript provides convenient ways to select needles:
- Front_Needles
/ Back_Needles
: All needles on a bed
- Loops
/ Front_Loops
/ Back_Loops
: Needles with stitches
- Array slicing: [start:end:step]
like Python
π Adding Complexityο
Parameterized Patternsο
Make your pattern configurable by accepting parameters from Python:
# Python code
knit_graph, machine = knit_script_to_knitout(
pattern="stockinette.ks",
out_file_name="custom_stockinette.k",
width=20, # Inject width parameter
height=50 # Inject height parameter
)
// stockinette.ks - now uses injected parameters
// width and height are available from Python
with Carrier as c1:{
in Leftward direction:{
tuck Front_Needles[0:width:2];
}
in reverse direction:{
tuck Front_Needles[1:width: 2];
}
for _ in range(height):{ // Use injected height
in reverse direction:{
knit Loops;
}
}
}
cut c1;
Adding Functionsο
Create reusable components with functions:
// Function for alternating tuck cast-on
def alt_tuck_cast_on(width = 10):{
in Leftward direction:{
tuck Front_Needles[0:width:2]; // Even needles
}
in reverse direction:{
tuck Front_Needles[1:width:2]; // Odd needles
}
}
// Use the function
with Carrier as c1:{
alt_tuck_cast_on(20); // Cast on 20 stitches
// Main knitting
for row in range(50):{
in reverse direction:{
knit Loops;
}
}
}
cut c1;
π¨ Multi-Sheet Knittingο
For more complex patterns, try multi-sheet knitting:
// Two-layer tube pattern
width = 20;
with Carrier as c1, Gauge as 2:{
// Front of tube (Sheet 0)
with Sheet as s0:{
in Leftward direction:{
tuck Front_Needles[0:width:2];
tuck Back_Needles[1:width:2];
}
}
// Back of tube (Sheet 1)
with Sheet as s1:{
in reverse direction:{
tuck Front_Needles[0:width:2];
tuck Back_Needles[1:width:2];
}
}
// Knit both layers
for _ in range(30):{
with Sheet as s0:{
in reverse direction:{ knit Loops; }
}
with Sheet as s1:{
in reverse direction:{ knit Loops; }
}
}
}
cut c1;
π§ Development Workflowο
Install KnitScript:
pip install knit-script
Create your first pattern:
Create
my_pattern.ks
with your KnitScript code.Test and iterate:
from knit_script import knit_script_to_knitout # Test your pattern try: knit_graph, machine = knit_script_to_knitout( pattern="my_pattern.ks", out_file_name="output.k" ) print("β Pattern compiled successfully!") except Exception as e: print(f"β Error: {e}")
π― Next Stepsο
Now that you have KnitScript installed:
Learn the syntax: Read the Language Reference for full documentation
Need Help?ο
π Documentation: Complete language reference in Language Reference