|
| 1 | +# Copyright (c) 2024, Lincoln D. Stein and the InvokeAI Development Team |
| 2 | +"""Z-Image Control invocation for spatial conditioning.""" |
| 3 | + |
| 4 | +from pydantic import BaseModel, Field |
| 5 | + |
| 6 | +from invokeai.app.invocations.baseinvocation import ( |
| 7 | + BaseInvocation, |
| 8 | + BaseInvocationOutput, |
| 9 | + Classification, |
| 10 | + invocation, |
| 11 | + invocation_output, |
| 12 | +) |
| 13 | +from invokeai.app.invocations.fields import ( |
| 14 | + FieldDescriptions, |
| 15 | + ImageField, |
| 16 | + InputField, |
| 17 | + OutputField, |
| 18 | +) |
| 19 | +from invokeai.app.invocations.model import ModelIdentifierField |
| 20 | +from invokeai.app.services.shared.invocation_context import InvocationContext |
| 21 | +from invokeai.backend.model_manager.taxonomy import BaseModelType, ModelType |
| 22 | + |
| 23 | + |
| 24 | +class ZImageControlField(BaseModel): |
| 25 | + """A Z-Image control conditioning field for spatial control (Canny, HED, Depth, Pose, MLSD).""" |
| 26 | + |
| 27 | + image_name: str = Field(description="The name of the preprocessed control image") |
| 28 | + control_model: ModelIdentifierField = Field(description="The Z-Image ControlNet adapter model") |
| 29 | + control_context_scale: float = Field( |
| 30 | + default=0.75, |
| 31 | + ge=0.0, |
| 32 | + le=2.0, |
| 33 | + description="The strength of the control signal. Recommended range: 0.65-0.80.", |
| 34 | + ) |
| 35 | + begin_step_percent: float = Field( |
| 36 | + default=0.0, |
| 37 | + ge=0.0, |
| 38 | + le=1.0, |
| 39 | + description="When the control is first applied (% of total steps)", |
| 40 | + ) |
| 41 | + end_step_percent: float = Field( |
| 42 | + default=1.0, |
| 43 | + ge=0.0, |
| 44 | + le=1.0, |
| 45 | + description="When the control is last applied (% of total steps)", |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@invocation_output("z_image_control_output") |
| 50 | +class ZImageControlOutput(BaseInvocationOutput): |
| 51 | + """Z-Image Control output containing control configuration.""" |
| 52 | + |
| 53 | + control: ZImageControlField = OutputField(description="Z-Image control conditioning") |
| 54 | + |
| 55 | + |
| 56 | +@invocation( |
| 57 | + "z_image_control", |
| 58 | + title="Z-Image ControlNet", |
| 59 | + tags=["image", "z-image", "control", "controlnet"], |
| 60 | + category="control", |
| 61 | + version="1.1.0", |
| 62 | + classification=Classification.Prototype, |
| 63 | +) |
| 64 | +class ZImageControlInvocation(BaseInvocation): |
| 65 | + """Configure Z-Image ControlNet for spatial conditioning. |
| 66 | +
|
| 67 | + Takes a preprocessed control image (e.g., Canny edges, depth map, pose) |
| 68 | + and a Z-Image ControlNet adapter model to enable spatial control. |
| 69 | +
|
| 70 | + Supports 5 control modes: Canny, HED, Depth, Pose, MLSD. |
| 71 | + Recommended control_context_scale: 0.65-0.80. |
| 72 | + """ |
| 73 | + |
| 74 | + image: ImageField = InputField( |
| 75 | + description="The preprocessed control image (Canny, HED, Depth, Pose, or MLSD)", |
| 76 | + ) |
| 77 | + control_model: ModelIdentifierField = InputField( |
| 78 | + description=FieldDescriptions.controlnet_model, |
| 79 | + title="Control Model", |
| 80 | + ui_model_base=BaseModelType.ZImage, |
| 81 | + ui_model_type=ModelType.ControlNet, |
| 82 | + ) |
| 83 | + control_context_scale: float = InputField( |
| 84 | + default=0.75, |
| 85 | + ge=0.0, |
| 86 | + le=2.0, |
| 87 | + description="Strength of the control signal. Recommended range: 0.65-0.80.", |
| 88 | + title="Control Scale", |
| 89 | + ) |
| 90 | + begin_step_percent: float = InputField( |
| 91 | + default=0.0, |
| 92 | + ge=0.0, |
| 93 | + le=1.0, |
| 94 | + description="When the control is first applied (% of total steps)", |
| 95 | + ) |
| 96 | + end_step_percent: float = InputField( |
| 97 | + default=1.0, |
| 98 | + ge=0.0, |
| 99 | + le=1.0, |
| 100 | + description="When the control is last applied (% of total steps)", |
| 101 | + ) |
| 102 | + |
| 103 | + def invoke(self, context: InvocationContext) -> ZImageControlOutput: |
| 104 | + return ZImageControlOutput( |
| 105 | + control=ZImageControlField( |
| 106 | + image_name=self.image.image_name, |
| 107 | + control_model=self.control_model, |
| 108 | + control_context_scale=self.control_context_scale, |
| 109 | + begin_step_percent=self.begin_step_percent, |
| 110 | + end_step_percent=self.end_step_percent, |
| 111 | + ) |
| 112 | + ) |
0 commit comments