import bpy
from bpy.props import EnumProperty, FloatProperty, IntProperty
import math
class FanClearAnimationOperator(bpy.types.Operator):
"""Clear animation data of selected objects and reset values"""
bl_idname = "object.clear_animation"
bl_label = "Clear Animation"
code
def execute(self, context):
for obj in context.selected_objects:
obj.animation_data_clear()
obj.rotation_euler = (0, 0, 0) # Reset rotation to zero
return {'FINISHED'}
class FanAnimationOperator(bpy.types.Operator):
"""Animate the rotation of selected objects as if they are fans"""
bl_idname = "object.fan_animation"
bl_label = "Fan Animation"
code
Assistant
User
I want to Allow user to set the start and end time of wobble effect
Assistant
User
Python: File "\Text", line 6
“”“Clear animation data of selected objects and reset values”“”
^
SyntaxError: invalid character '“' (U+201C)
Assistant
User
Implement it into this script
Assistant
rotation_type: EnumProperty(
name="Rotation Type",
items=[
('CONSTANT', "Constant Speed", "Constant speed rotation"),
],
default='CONSTANT'
)
def execute(self, context):
rotation_x = math.radians(context.scene.fan_rotation_x)
rotation_y = math.radians(context.scene.fan_rotation_y)
rotation_z = math.radians(context.scene.fan_rotation_z)
end_rotation_x = math.radians(context.scene.fan_end_rotation_x)
end_rotation_y = math.radians(context.scene.fan_end_rotation_y)
end_rotation_z = math.radians(context.scene.fan_end_rotation_z)
start_frame_x = context.scene.fan_start_frame_x
end_frame_x = context.scene.fan_end_frame_x
start_frame_y = context.scene.fan_start_frame_y
end_frame_y = context.scene.fan_end_frame_y
start_frame_z = context.scene.fan_start_frame_z
end_frame_z = context.scene.fan_end_frame_z
wobble_amplitude = math.radians(context.scene.fan_wobble_amplitude)
wobble_frequency = context.scene.fan_wobble_frequency
for obj in context.selected_objects:
obj.rotation_mode = 'XYZ'
# Set initial rotation to zero at frame 1
obj.rotation_euler = (0, 0, 0)
obj.keyframe_insert(data_path="rotation_euler", index=0, frame=1)
obj.keyframe_insert(data_path="rotation_euler", index=1, frame=1)
obj.keyframe_insert(data_path="rotation_euler", index=2, frame=1)
# Wobble function
def apply_wobble(angle, frame, axis):
wobble = wobble_amplitude * math.sin(wobble_frequency * frame)
return angle + wobble if axis == 'X' else (angle + wobble if axis == 'Y' else angle + wobble)
# Set keyframes for X rotation with wobble
for frame in range(start_frame_x, end_frame_x + 1):
current_rotation = rotation_x + (end_rotation_x - rotation_x) * ((frame - start_frame_x) / (end_frame_x - start_frame_x))
obj.rotation_euler[0] = apply_wobble(current_rotation, frame, 'X')
obj.keyframe_insert(data_path="rotation_euler", index=0, frame=frame)
# Set keyframes for Y rotation with wobble
for frame in range(start_frame_y, end_frame_y + 1):
current_rotation = rotation_y + (end_rotation_y - rotation_y) * ((frame - start_frame_y) / (end_frame_y - start_frame_y))
obj.rotation_euler[1] = apply_wobble(current_rotation, frame, 'Y')
obj.keyframe_insert(data_path="rotation_euler", index=1, frame=frame)
# Set keyframes for Z rotation with wobble
for frame in range(start_frame_z, end_frame_z + 1):
current_rotation = rotation_z + (end_rotation_z - rotation_z) * ((frame - start_frame_z) / (end_frame_z - start_frame_z))
obj.rotation_euler[2] = apply_wobble(current_rotation, frame, 'Z')
obj.keyframe_insert(data_path="rotation_euler", index=2, frame=frame)
return {'FINISHED'}
class FanAnimationPanel(bpy.types.Panel):
"""Panel for Fan Animation"""
bl_label = "Fan Animation"
bl_idname = "OBJECT_PT_fan_animation"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Fan Animation' # New category
def unregister():
bpy.utils.unregister_class(FanAnimationOperator)
bpy.utils.unregister_class(FanAnimationPanel)
bpy.utils.unregister_class(FanClearAnimationOperator)
del bpy.types.Scene.fan_rotation_type
del bpy.types.Scene.fan_rotation_x
del bpy.types.Scene.fan_rotation_y
del bpy.types.Scene.fan_rotation_z
del bpy.types.Scene.fan_end_rotation_x
del bpy.types.Scene.fan_end_rotation_y
del bpy.types.Scene.fan_end_rotation_z
del bpy.types.Scene.fan_start_frame_x
del bpy.types.Scene.fan_end_frame_x
del bpy.types.Scene.fan_start_frame_y
del bpy.types.Scene.fan_end_frame_y
del bpy.types.Scene.fan_start_frame_z
del bpy.types.Scene.fan_end_frame_z
del bpy.types.Scene.fan_wobble_amplitude
del bpy.types.Scene.fan_wobble_frequency
if name == "main":
register()
Your script defines an add-on for Blender that allows for the animation of selected objects, making them rotate as if they were fans. The add-on includes settings for rotation angles, start and end frames for each axis, and additional wobble settings to simulate more complex motion. Here are the steps to install and use this add-on effectively:
Steps to Install and Use the Add-On
Open Blender: Make sure you have Blender installed and open it.
Add the Script:
Open a new Text editor within Blender (Editor Type > Text Editor).
Copy and paste the entire script you provided into a new text block in the Text editor.
Run the Script:
Click on the Run Script button in the Text editor.
Enable the Add-On:
Go to Edit > Preferences.
Navigate to the Add-ons tab.
Search for "Fan Animation". If not already enabled, enable the add-on by ticking the checkbox.
Configure Animation Settings:
In the 3D Viewport, on the right-hand side, you’ll find a new tab section titled "Fan Animation" in the Tool Shelf (N key to open/close the panel).
You should see settings under "Rotation" for X, Y, and Z axes.
Input your desired start and end rotations as well as start and end frames for each axis.
Configure additional wobble settings like Wobble Amplitude and Wobble Frequency if needed.
Animate the Fans:
Select the objects you wish to animate.
Within the "Fan Animation" panel, configure your parameters.
Click the Animate Fans button to apply the animation with the specified settings.
Clear Animation Data:
If you need to remove the existing animation data, select your objects and then click the Clear Animation Data button in the same panel.
Detailed Code Explanation
Here’s a breakdown of what each part of the code does:
Operator Classes:
FanClearAnimationOperator: Clears animation data and resets the rotation of the selected objects.
FanAnimationOperator: Adds keyframes to the selected objects to animate their rotation with optional wobble.
Panel Class:
FanAnimationPanel: Creates a UI panel in the 3D Viewport's sidebar for setting rotation, frame settings, and wobble parameters.
Property Definitions:
FloatProperty and IntProperty are used to configure various animation settings such as rotation angles and start/end frames directly in the scene properties.
Register/Unregister Functions:
register(): Registers the operator and panel classes and adds relevant properties to the Scene type.
unregister(): Unregisters all classes and removes the properties from the Scene type.
Notes
Wobble Function: The script includes an apply_wobble function which adds a sinusoidal wobble to the rotation, simulating more complex motion.
Animation Frames: The property values for start and end frames can be adjusted to match the length of your specific animation requirement.
Ensure to have your objects prepared and selected before using the Animate Fans button. This add-on allows customization and can be further extended as needed.
To implement the feature that allows users to set the start and end times of the wobble effect, you'll need to add new properties for the wobble start and end frames. Then you can modify the existing apply_wobble function to account for these frames. This will require changes in the operator, panel, and adding/registering new properties.
Below are the steps to incorporate these changes into your existing script:
Step 1: Update the Operator to Include Wobble Timing
Modify the FanAnimationOperator to include properties for the wobble effect's start and end frames and update the logic for applying the wobble only within the specified range.
Here is the updated FanAnimationOperator class:
python
import bpy
from bpy.props import EnumProperty, FloatProperty, IntProperty
import math
class FanClearAnimationOperator(bpy.types.Operator):
"""Clear animation data of selected objects and reset values"""
bl_idname = "object.clear_animation"
bl_label = "Clear Animation"
def execute(self, context):
for obj in context.selected_objects:
obj.animation_data_clear()
obj.rotation_euler = (0, 0, 0) # Reset rotation to zero
return {'FINISHED'}
class FanAnimationOperator(bpy.types.Operator):
"""Animate the rotation of selected objects as if they are fans"""
bl_idname = "object.fan_animation"
bl_label = "Fan Animation"
rotation_type: EnumProperty(
name="Rotation Type",
items=[
('CONSTANT', "Constant Speed", "Constant speed rotation"),
],
default='CONSTANT'
)
def execute(self, context):
rotation_x = math.radians(context.scene.fan_rotation_x)
rotation_y = math.radians(context.scene.fan_rotation_y)
rotation_z = math.radians(context.scene.fan_rotation_z)
end_rotation_x = math.radians(context.scene.fan_end_rotation_x)
end_rotation_y = math.radians(context.scene.fan_end_rotation_y)
end_rotation_z = math.radians(context.scene.fan_end_rotation_z)
start_frame_x = context.scene.fan_start_frame_x
end_frame_x = context.scene.fan_end_frame_x
start_frame_y = context.scene.fan_start_frame_y
end_frame_y = context.scene.fan_end_frame_y
start_frame_z = context.scene.fan_start_frame_z
end_frame_z = context.scene.fan_end_frame_z
wobble_amplitude = math.radians(context.scene.fan_wobble_amplitude)
wobble_frequency = context.scene.fan_wobble_frequency
wobble_start_frame = context.scene.fan_wobble_start_frame
wobble_end_frame = context.scene.fan_wobble_end_frame
for obj in context.selected_objects:
obj.rotation_mode = 'XYZ'
# Set initial rotation to zero at frame 1
obj.rotation_euler = (0, 0, 0)
obj.keyframe_insert(data_path="rotation_euler", index=0, frame=1)
obj.keyframe_insert(data_path="rotation_euler", index=1, frame=1)
obj.keyframe_insert(data_path="rotation_euler", index=2, frame=1)
# Wobble function
def apply_wobble(angle, frame, axis):
if wobble_start_frame <= frame <= wobble_end_frame:
wobble = wobble_amplitude * math.sin(wobble_frequency * frame)
else:
wobble = 0
return angle + wobble if axis == 'X' else (angle + wobble if axis == 'Y' else angle + wobble)
# Set keyframes for X rotation with wobble
for frame in range(start_frame_x, end_frame_x + 1):
current_rotation = rotation_x + (end_rotation_x - rotation_x) * ((frame - start_frame_x) / (end_frame_x - start_frame_x))
obj.rotation_euler[0] = apply_wobble(current_rotation, frame, 'X')
obj.keyframe_insert(data_path="rotation_euler", index=0, frame=frame)
# Set keyframes for Y rotation with wobble
for frame in range(start_frame_y, end_frame_y + 1):
current_rotation = rotation_y + (end_rotation_y - rotation_y) * ((frame - start_frame_y) / (end_frame_y - start_frame_y))
obj.rotation_euler[1] = apply_wobble(current_rotation, frame, 'Y')
obj.keyframe_insert(data_path="rotation_euler", index=1, frame=frame)
# Set keyframes for Z rotation with wobble
for frame in range(start_frame_z, end_frame_z + 1):
current_rotation = rotation_z + (end_rotation_z - rotation_z) * ((frame - start_frame_z) / (end_frame_z - start_frame_z))
obj.rotation_euler[2] = apply_wobble(current_rotation, frame, 'Z')
obj.keyframe_insert(data_path="rotation_euler", index=2, frame=frame)
return {'FINISHED'}
class FanAnimationPanel(bpy.types.Panel):
"""Panel for Fan Animation"""
bl_label = "Fan Animation"
bl_idname = "OBJECT_PT_fan_animation"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Fan Animation' # New category
def draw(self, context):
layout = self.layout
# X Rotation Subpanel
layout.label(text="X Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_x")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_x", text="End Rotation")
# Y Rotation Subpanel
layout.label(text="Y Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_y")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_y", text="End Rotation")
# Z Rotation Subpanel
layout.label(text="Z Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_z")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_z", text="End Rotation")
# Wobble Settings Subpanel
layout.label(text="Wobble Settings:")
row = layout.row()
row.prop(context.scene, "fan_wobble_amplitude", text="Wobble Amplitude")
row = layout.row()
row.prop(context.scene, "fan_wobble_frequency", text="Wobble Frequency")
row = layout.row()
row.prop(context.scene, "fan_wobble_start_frame", text="Wobble Start Frame")
row = layout.row()
row.prop(context.scene, "fan_wobble_end_frame", text="Wobble End Frame")
# Animation Settings Subpanel
layout.label(text="Animation Settings:")
row = layout.row()
row.prop(context.scene, "fan_start_frame_x", text="Start Frame X")
row.prop(context.scene, "fan_end_frame_x", text="End Frame X")
row = layout.row()
row.prop(context.scene, "fan_start_frame_y", text="Start Frame Y")
row.prop(context.scene, "fan_end_frame_y", text="End Frame Y")
row = layout.row()
row.prop(context.scene, "fan_start_frame_z", text="Start Frame Z")
row.prop(context.scene, "fan_end_frame_z", text="End Frame Z")
layout.operator("object.fan_animation", text="Animate Fans")
layout.operator("object.clear_animation", text="Clear Animation Data")
def register():
bpy.utils.register_class(FanAnimationOperator)
bpy.utils.register_class(FanAnimationPanel)
bpy.utils.register_class(FanClearAnimationOperator)
bpy.types.Scene.fan_rotation_type = EnumProperty(
name="Rotation Type",
items=[
('CONSTANT', "Constant Speed", "Constant speed rotation"),
],
default='CONSTANT'
)
bpy.types.Scene.fan_rotation_x = FloatProperty(name="Rotation X", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_rotation_y = FloatProperty(name="Rotation Y", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_rotation_z = FloatProperty(name="Rotation Z", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_x = FloatProperty(name="End Rotation X", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_y = FloatProperty(name="End Rotation Y", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_z = FloatProperty(name="End Rotation Z", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_start_frame_x = IntProperty(name="Start Frame X", default=1, min=1)
bpy.types.Scene.fan_end_frame_x = IntProperty(name="End Frame X", default=250, min=1)
bpy.types.Scene.fan_start_frame_y = IntProperty(name="Start Frame Y", default=1, min=1)
bpy.types.Scene.fan_end_frame_y = IntProperty(name="End Frame Y", default=250, min=1)
bpy.types.Scene.fan_start_frame_z = IntProperty(name="Start Frame Z", default=1, min=1)
bpy.types.Scene.fan_end_frame_z = IntProperty(name="End Frame Z", default=250, min=1)
bpy.types.Scene.fan_wobble_amplitude = FloatProperty(name="Wobble Amplitude", default=10.0, min=0.0, max=360.0)
bpy.types.Scene.fan_wobble_frequency = FloatProperty(name="Wobble Frequency", default=1.0, min=0.0, max=10.0)
bpy.types.Scene.fan_wobble_start_frame = IntProperty(name="Wobble Start Frame", default=1, min=1)
bpy.types.Scene.fan_wobble_end_frame = IntProperty(name="Wobble End Frame", default=250, min=1)
def unregister():
bpy.utils.unregister_class(FanAnimationOperator)
bpy.utils.unregister_class(FanAnimationPanel)
bpy.utils.unregister_class(FanClearAnimationOperator)
del bpy.types.Scene.fan_rotation_type
del bpy.types.Scene.fan_rotation_x
del bpy.types.Scene.fan_rotation_y
del bpy.types.Scene.fan_rotation_z
del bpy.types.Scene.fan_end_rotation_x
del bpy.types.Scene.fan_end_rotation_y
del bpy.types.Scene.fan_end_rotation_z
del bpy.types.Scene.fan_start_frame_x
del bpy.types.Scene.fan_end_frame_x
del bpy.types.Scene.fan_start_frame_y
del bpy.types.Scene.fan_end_frame_y
del bpy.types.Scene.fan_start_frame_z
del bpy.types.Scene.fan_end_frame_z
del bpy.types.Scene.fan_wobble_amplitude
del bpy.types.Scene.fan_wobble_frequency
del bpy.types.Scene.fan_wobble_start_frame
del bpy.types.Scene.fan_wobble_end_frame
if __name__ == "__main__":
register()
Step 2: Update the Panel to Include Wobble Timing Properties
The FanAnimationPanel class needs to include the new properties for the wobble effect's start and end frames.
Summary of Changes:
Added new properties to the Scene type for the start and end frames of the wobble effect:
Updated the FanAnimationPanel class to include UI elements for the new properties.
Modified the apply_wobble function within the FanAnimationOperator class to use these properties, applying wobble only within the specified frame range.
With these changes, users can set the start and end frames for when the wobble effect should be applied to the rotation animation. This provides more precise control over the animation, enabling complex motion patterns.
It looks like there are non-standard quotation marks (curly quotes) in the docstrings which are causing the syntax error. Python requires standard ASCII double quotes (") for strings. Let's replace all instances of ‘“’ and ‘”’ with standard double quotes.
Here is the corrected version of your script:
python
import bpy
from bpy.props import EnumProperty, FloatProperty, IntProperty
import math
class FanClearAnimationOperator(bpy.types.Operator):
"""Clear animation data of selected objects and reset values"""
bl_idname = "object.clear_animation"
bl_label = "Clear Animation"
def execute(self, context):
for obj in context.selected_objects:
obj.animation_data_clear()
obj.rotation_euler = (0, 0, 0) # Reset rotation to zero
return {'FINISHED'}
class FanAnimationOperator(bpy.types.Operator):
"""Animate the rotation of selected objects as if they are fans"""
bl_idname = "object.fan_animation"
bl_label = "Fan Animation"
rotation_type: EnumProperty(
name="Rotation Type",
items=[
('CONSTANT', "Constant Speed", "Constant speed rotation"),
],
default='CONSTANT'
)
def execute(self, context):
rotation_x = math.radians(context.scene.fan_rotation_x)
rotation_y = math.radians(context.scene.fan_rotation_y)
rotation_z = math.radians(context.scene.fan_rotation_z)
end_rotation_x = math.radians(context.scene.fan_end_rotation_x)
end_rotation_y = math.radians(context.scene.fan_end_rotation_y)
end_rotation_z = math.radians(context.scene.fan_end_rotation_z)
start_frame_x = context.scene.fan_start_frame_x
end_frame_x = context.scene.fan_end_frame_x
start_frame_y = context.scene.fan_start_frame_y
end_frame_y = context.scene.fan_end_frame_y
start_frame_z = context.scene.fan_start_frame_z
end_frame_z = context.scene.fan_end_frame_z
wobble_amplitude = math.radians(context.scene.fan_wobble_amplitude)
wobble_frequency = context.scene.fan_wobble_frequency
wobble_start_frame = context.scene.fan_wobble_start_frame
wobble_end_frame = context.scene.fan_wobble_end_frame
for obj in context.selected_objects:
obj.rotation_mode = 'XYZ'
# Set initial rotation to zero at frame 1
obj.rotation_euler = (0, 0, 0)
obj.keyframe_insert(data_path="rotation_euler", index=0, frame=1)
obj.keyframe_insert(data_path="rotation_euler", index=1, frame=1)
obj.keyframe_insert(data_path="rotation_euler", index=2, frame=1)
# Wobble function
def apply_wobble(angle, frame, axis):
if wobble_start_frame <= frame <= wobble_end_frame:
wobble = wobble_amplitude * math.sin(wobble_frequency * frame)
else:
wobble = 0
return angle + wobble if axis == 'X' else (angle + wobble if axis == 'Y' else angle + wobble)
# Set keyframes for X rotation with wobble
for frame in range(start_frame_x, end_frame_x + 1):
current_rotation = rotation_x + (end_rotation_x - rotation_x) * ((frame - start_frame_x) / (end_frame_x - start_frame_x))
obj.rotation_euler[0] = apply_wobble(current_rotation, frame, 'X')
obj.keyframe_insert(data_path="rotation_euler", index=0, frame=frame)
# Set keyframes for Y rotation with wobble
for frame in range(start_frame_y, end_frame_y + 1):
current_rotation = rotation_y + (end_rotation_y - rotation_y) * ((frame - start_frame_y) / (end_frame_y - start_frame_y))
obj.rotation_euler[1] = apply_wobble(current_rotation, frame, 'Y')
obj.keyframe_insert(data_path="rotation_euler", index=1, frame=frame)
# Set keyframes for Z rotation with wobble
for frame in range(start_frame_z, end_frame_z + 1):
current_rotation = rotation_z + (end_rotation_z - rotation_z) * ((frame - start_frame_z) / (end_frame_z - start_frame_z))
obj.rotation_euler[2] = apply_wobble(current_rotation, frame, 'Z')
obj.keyframe_insert(data_path="rotation_euler", index=2, frame=frame)
return {'FINISHED'}
class FanAnimationPanel(bpy.types.Panel):
"""Panel for Fan Animation"""
bl_label = "Fan Animation"
bl_idname = "OBJECT_PT_fan_animation"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Fan Animation' # New category
def draw(self, context):
layout = self.layout
# X Rotation Subpanel
layout.label(text="X Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_x")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_x", text="End Rotation")
# Y Rotation Subpanel
layout.label(text="Y Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_y")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_y", text="End Rotation")
# Z Rotation Subpanel
layout.label(text="Z Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_z")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_z", text="End Rotation")
# Wobble Settings Subpanel
layout.label(text="Wobble Settings:")
row = layout.row()
row.prop(context.scene, "fan_wobble_amplitude", text="Wobble Amplitude")
row = layout.row()
row.prop(context.scene, "fan_wobble_frequency", text="Wobble Frequency")
row = layout.row()
row.prop(context.scene, "fan_wobble_start_frame", text="Wobble Start Frame")
row = layout.row()
row.prop(context.scene, "fan_wobble_end_frame", text="Wobble End Frame")
# Animation Settings Subpanel
layout.label(text="Animation Settings:")
row = layout.row()
row.prop(context.scene, "fan_start_frame_x", text="Start Frame X")
row.prop(context.scene, "fan_end_frame_x", text="End Frame X")
row = layout.row()
row.prop(context.scene, "fan_start_frame_y", text="Start Frame Y")
row.prop(context.scene, "fan_end_frame_y", text="End Frame Y")
row = layout.row()
row.prop(context.scene, "fan_start_frame_z", text="Start Frame Z")
row.prop(context.scene, "fan_end_frame_z", text="End Frame Z")
layout.operator("object.fan_animation", text="Animate Fans")
layout.operator("object.clear_animation", text="Clear Animation Data")
def register():
bpy.utils.register_class(FanAnimationOperator)
bpy.utils.register_class(FanAnimationPanel)
bpy.utils.register_class(FanClearAnimationOperator)
bpy.types.Scene.fan_rotation_type = EnumProperty(
name="Rotation Type",
items=[
('CONSTANT', "Constant Speed", "Constant speed rotation"),
],
default='CONSTANT'
)
bpy.types.Scene.fan_rotation_x = FloatProperty(name="Rotation X", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_rotation_y = FloatProperty(name="Rotation Y", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_rotation_z = FloatProperty(name="Rotation Z", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_x = FloatProperty(name="End Rotation X", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_y = FloatProperty(name="End Rotation Y", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_z = FloatProperty(name="End Rotation Z", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_start_frame_x = IntProperty(name="Start Frame X", default=1, min=1)
bpy.types.Scene.fan_end_frame_x = IntProperty(name="End Frame X", default=250, min=1)
bpy.types.Scene.fan_start_frame_y = IntProperty(name="Start Frame Y", default=1, min=1)
bpy.types.Scene.fan_end_frame_y = IntProperty(name="End Frame Y", default=250, min=1)
bpy.types.Scene.fan_start_frame_z = IntProperty(name="Start Frame Z", default=1, min=1)
bpy.types.Scene.fan_end_frame_z = IntProperty(name="End Frame Z", default=250, min=1)
bpy.types.Scene.fan_wobble_amplitude = FloatProperty(name="Wobble Amplitude", default=10.0, min=0.0, max=360.0)
bpy.types.Scene.fan_wobble_frequency = FloatProperty(name="Wobble Frequency", default=1.0, min=0.0, max=10.0)
bpy.types.Scene.fan_wobble_start_frame = IntProperty(name="Wobble Start Frame", default=1, min=1)
bpy.types.Scene.fan_wobble_end_frame = IntProperty(name="Wobble End Frame", default=250, min=1)
def unregister():
bpy.utils.unregister_class(FanAnimationOperator)
bpy.utils.unregister_class(FanAnimationPanel)
bpy.utils.unregister_class(FanClearAnimationOperator)
del bpy.types.Scene.fan_rotation_type
del bpy.types.Scene.fan_rotation_x
del bpy.types.Scene.fan_rotation_y
del bpy.types.Scene.fan_rotation_z
del bpy.types.Scene.fan_end_rotation_x
del bpy.types.Scene.fan_end_rotation_y
del bpy.types.Scene.fan_end_rotation_z
del bpy.types.Scene.fan_start_frame_x
del bpy.types.Scene.fan_end_frame_x
del bpy.types.Scene.fan_start_frame_y
del bpy.types.Scene.fan_end_frame_y
del bpy.types.Scene.fan_start_frame_z
del bpy.types.Scene.fan_end_frame_z
del bpy.types.Scene.fan_wobble_amplitude
del bpy.types.Scene.fan_wobble_frequency
del bpy.types.Scene.fan_wobble_start_frame
del bpy.types.Scene.fan_wobble_end_frame
if __name__ == "__main__":
register()
Summary of Updates
Fixed the quotes: Replaced the non-standard curly quotes with standard double quotes.
No other changes needed: Your logic and implementation remain exactly the same.
You can now follow the same steps as before to install and run this script in Blender. This should resolve the SyntaxError issue, and you'll have the updated functionality to control the start and end frames of the wobble effect.
Certainly! Below is the entire script with the functionality to allow users to set the start and end times of the wobble effect properly implemented and corrected:
python
import bpy
from bpy.props import EnumProperty, FloatProperty, IntProperty
import math
class FanClearAnimationOperator(bpy.types.Operator):
"""Clear animation data of selected objects and reset values"""
bl_idname = "object.clear_animation"
bl_label = "Clear Animation"
def execute(self, context):
for obj in context.selected_objects:
obj.animation_data_clear()
obj.rotation_euler = (0, 0, 0) # Reset rotation to zero
return {'FINISHED'}
class FanAnimationOperator(bpy.types.Operator):
"""Animate the rotation of selected objects as if they are fans"""
bl_idname = "object.fan_animation"
bl_label = "Fan Animation"
rotation_type: EnumProperty(
name="Rotation Type",
items=[
('CONSTANT', "Constant Speed", "Constant speed rotation"),
],
default='CONSTANT'
)
def execute(self, context):
rotation_x = math.radians(context.scene.fan_rotation_x)
rotation_y = math.radians(context.scene.fan_rotation_y)
rotation_z = math.radians(context.scene.fan_rotation_z)
end_rotation_x = math.radians(context.scene.fan_end_rotation_x)
end_rotation_y = math.radians(context.scene.fan_end_rotation_y)
end_rotation_z = math.radians(context.scene.fan_end_rotation_z)
start_frame_x = context.scene.fan_start_frame_x
end_frame_x = context.scene.fan_end_frame_x
start_frame_y = context.scene.fan_start_frame_y
end_frame_y = context.scene.fan_end_frame_y
start_frame_z = context.scene.fan_start_frame_z
end_frame_z = context.scene.fan_end_frame_z
wobble_amplitude = math.radians(context.scene.fan_wobble_amplitude)
wobble_frequency = context.scene.fan_wobble_frequency
wobble_start_frame = context.scene.fan_wobble_start_frame
wobble_end_frame = context.scene.fan_wobble_end_frame
for obj in context.selected_objects:
obj.rotation_mode = 'XYZ'
# Set initial rotation to zero at frame 1
obj.rotation_euler = (0, 0, 0)
obj.keyframe_insert(data_path="rotation_euler", index=0, frame=1)
obj.keyframe_insert(data_path="rotation_euler", index=1, frame=1)
obj.keyframe_insert(data_path="rotation_euler", index=2, frame=1)
# Wobble function
def apply_wobble(angle, frame, axis):
if wobble_start_frame <= frame <= wobble_end_frame:
wobble = wobble_amplitude * math.sin(wobble_frequency * frame)
else:
wobble = 0
return angle + wobble if axis == 'X' else (angle + wobble if axis == 'Y' else angle + wobble)
# Set keyframes for X rotation with wobble
for frame in range(start_frame_x, end_frame_x + 1):
current_rotation = rotation_x + (end_rotation_x - rotation_x) * ((frame - start_frame_x) / (end_frame_x - start_frame_x))
obj.rotation_euler[0] = apply_wobble(current_rotation, frame, 'X')
obj.keyframe_insert(data_path="rotation_euler", index=0, frame=frame)
# Set keyframes for Y rotation with wobble
for frame in range(start_frame_y, end_frame_y + 1):
current_rotation = rotation_y + (end_rotation_y - rotation_y) * ((frame - start_frame_y) / (end_frame_y - start_frame_y))
obj.rotation_euler[1] = apply_wobble(current_rotation, frame, 'Y')
obj.keyframe_insert(data_path="rotation_euler", index=1, frame=frame)
# Set keyframes for Z rotation with wobble
for frame in range(start_frame_z, end_frame_z + 1):
current_rotation = rotation_z + (end_rotation_z - rotation_z) * ((frame - start_frame_z) / (end_frame_z - start_frame_z))
obj.rotation_euler[2] = apply_wobble(current_rotation, frame, 'Z')
obj.keyframe_insert(data_path="rotation_euler", index=2, frame=frame)
return {'FINISHED'}
class FanAnimationPanel(bpy.types.Panel):
"""Panel for Fan Animation"""
bl_label = "Fan Animation"
bl_idname = "OBJECT_PT_fan_animation"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Fan Animation' # New category
def draw(self, context):
layout = self.layout
# X Rotation Subpanel
layout.label(text="X Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_x")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_x", text="End Rotation")
# Y Rotation Subpanel
layout.label(text="Y Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_y")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_y", text="End Rotation")
# Z Rotation Subpanel
layout.label(text="Z Rotation:")
row = layout.row()
row.prop(context.scene, "fan_rotation_z")
row = layout.row()
row.prop(context.scene, "fan_end_rotation_z", text="End Rotation")
# Wobble Settings Subpanel
layout.label(text="Wobble Settings:")
row = layout.row()
row.prop(context.scene, "fan_wobble_amplitude", text="Wobble Amplitude")
row = layout.row()
row.prop(context.scene, "fan_wobble_frequency", text="Wobble Frequency")
row = layout.row()
row.prop(context.scene, "fan_wobble_start_frame", text="Wobble Start Frame")
row = layout.row()
row.prop(context.scene, "fan_wobble_end_frame", text="Wobble End Frame")
# Animation Settings Subpanel
layout.label(text="Animation Settings:")
row = layout.row()
row.prop(context.scene, "fan_start_frame_x", text="Start Frame X")
row.prop(context.scene, "fan_end_frame_x", text="End Frame X")
row = layout.row()
row.prop(context.scene, "fan_start_frame_y", text="Start Frame Y")
row.prop(context.scene, "fan_end_frame_y", text="End Frame Y")
row = layout.row()
row.prop(context.scene, "fan_start_frame_z", text="Start Frame Z")
row.prop(context.scene, "fan_end_frame_z", text="End Frame Z")
layout.operator("object.fan_animation", text="Animate Fans")
layout.operator("object.clear_animation", text="Clear Animation Data")
def register():
bpy.utils.register_class(FanAnimationOperator)
bpy.utils.register_class(FanAnimationPanel)
bpy.utils.register_class(FanClearAnimationOperator)
bpy.types.Scene.fan_rotation_type = EnumProperty(
name="Rotation Type",
items=[
('CONSTANT', "Constant Speed", "Constant speed rotation"),
],
default='CONSTANT'
)
bpy.types.Scene.fan_rotation_x = FloatProperty(name="Rotation X", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_rotation_y = FloatProperty(name="Rotation Y", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_rotation_z = FloatProperty(name="Rotation Z", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_x = FloatProperty(name="End Rotation X", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_y = FloatProperty(name="End Rotation Y", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_end_rotation_z = FloatProperty(name="End Rotation Z", default=0.0, min=-360.0, max=360.0)
bpy.types.Scene.fan_start_frame_x = IntProperty(name="Start Frame X", default=1, min=1)
bpy.types.Scene.fan_end_frame_x = IntProperty(name="End Frame X", default=250, min=1)
bpy.types.Scene.fan_start_frame_y = IntProperty(name="Start Frame Y", default=1, min=1)
bpy.types.Scene.fan_end_frame_y = IntProperty(name="End Frame Y", default=250, min=1)
bpy.types.Scene.fan_start_frame_z = IntProperty(name="Start Frame Z", default=1, min=1)
bpy.types.Scene.fan_end_frame_z = IntProperty(name="End Frame Z", default=250, min=1)
bpy.types.Scene.fan_wobble_amplitude = FloatProperty(name="Wobble Amplitude", default=10.0, min=0.0, max=360.0)
bpy.types.Scene.fan_wobble_frequency = FloatProperty(name="Wobble Frequency", default=1.0, min=0.0, max=10.0)
bpy.types.Scene.fan_wobble_start_frame = IntProperty(name="Wobble Start Frame", default=1, min=1)
bpy.types.Scene.fan_wobble_end_frame = IntProperty(name="Wobble End Frame", default=250, min=1)
def unregister():
bpy.utils.unregister_class(FanAnimationOperator)
bpy.utils.unregister_class(FanAnimationPanel)
bpy.utils.unregister_class(FanClearAnimationOperator)
del bpy.types.Scene.fan_rotation_type
del bpy.types.Scene.fan_rotation_x
del bpy.types.Scene.fan_rotation_y
del bpy.types.Scene.fan_rotation_z
del bpy.types.Scene.fan_end_rotation_x
del bpy.types.Scene.fan_end_rotation_y
del bpy.types.Scene.fan_end_rotation_z
del bpy.types.Scene.fan_start_frame_x
del bpy.types.Scene.fan_end_frame_x
del bpy.types.Scene.fan_start_frame_y
del bpy.types.Scene.fan_end_frame_y
del bpy.types.Scene.fan_start_frame_z
del bpy.types.Scene.fan_end_frame_z
del bpy.types.Scene.fan_wobble_amplitude
del bpy.types.Scene.fan_wobble_frequency
del bpy.types.Scene.fan_wobble_start_frame
del bpy.types.Scene.fan_wobble_end_frame
if __name__ == "__main__":
register()
Summary of Updates
Fixed the quotes: Replaced the non-standard curly quotes with standard double quotes.
New properties added: Added properties for the start and end frames of the wobble effect in the Scene type.
Updated FanAnimationPanel: Included the UI elements for the start and end frames of the wobble effect.
Updated logic in FanAnimationOperator: Modified the apply_wobble function to only apply the wobble effect between the specified frames.
You can now follow the same steps as before to install and run this script in Blender, and you'll have the desired functionality along with the corrected syntax.