"""Fabex 'simulation_ops.py' © 2012 Vilem Novak
Blender Operator definitions are in this file.
They mostly call the functions from 'utils.py'
"""
import os
import shapely
from shapely import geometry as sgeometry
from shapely import affinity, prepared, speedups
import bpy
from bpy.props import StringProperty
from bpy.types import Operator
from .async_op import (
AsyncCancelledException,
AsyncOperatorMixin,
)
from ..simulation import do_simulation
from ..utilities.operation_utils import (
chain_valid,
get_chain_operations,
)
[docs]
class CAMSimulate(Operator, AsyncOperatorMixin):
"""Simulate CAM Operation
This Is Performed by: Creating an Image, Painting Z Depth of the Brush Subtractively.
Works only for Some Operations, Can Not Be Used for 4-5 Axis."""
[docs]
bl_idname = "object.cam_simulate"
[docs]
bl_label = "CAM Simulation"
[docs]
bl_options = {"REGISTER", "UNDO", "BLOCKING"}
[docs]
operation: StringProperty(
name="Operation",
description="Specify the operation to calculate",
default="Operation",
)
[docs]
async def execute_async(self, context):
"""Execute an asynchronous simulation operation based on the active camera
operation.
This method retrieves the current scene and the active camera operation.
It constructs the operation name and checks if the corresponding object
exists in the Blender data. If it does, it attempts to run the
simulation asynchronously. If the simulation is cancelled, it returns a
cancellation status. If the object does not exist, it reports an error
and returns a finished status.
Args:
context: The context in which the operation is executed.
Returns:
dict: A dictionary indicating the status of the operation, either
{'CANCELLED'} or {'FINISHED'}.
"""
s = bpy.context.scene
operation = s.cam_operations[s.cam_active_operation]
operation_name = "cam_path_{}".format(operation.name)
if operation_name in bpy.data.objects:
try:
await do_simulation(operation_name, [operation])
except AsyncCancelledException as e:
return {"CANCELLED"}
else:
self.report({"ERROR"}, "No Computed Path to Simulate")
return {"FINISHED"}
return {"FINISHED"}
[docs]
def draw(self, context):
"""Draws the user interface for selecting camera operations.
This method creates a layout element in the user interface that allows
users to search and select a specific camera operation from a list of
available operations defined in the current scene. It utilizes the
Blender Python API to integrate with the UI.
Args:
context: The context in which the drawing occurs, typically
provided by Blender's UI system.
"""
layout = self.layout
layout.prop_search(self, "operation", bpy.context.scene, "cam_operations")
[docs]
class CAMSimulateChain(Operator, AsyncOperatorMixin):
"""Simulate CAM Chain, Compared to Single Op Simulation Just Writes Into One Image and Thus Enables
to See how Ops Work Together."""
[docs]
bl_idname = "object.cam_simulate_chain"
[docs]
bl_label = "CAM Simulation"
[docs]
bl_options = {"REGISTER", "UNDO", "BLOCKING"}
@classmethod
[docs]
def poll(cls, context):
"""Check the validity of the active camera chain in the scene.
This method retrieves the currently active camera chain from the scene's
camera chains and checks its validity using the `isChainValid` function.
It returns a boolean indicating whether the active camera chain is
valid.
Args:
context (object): The context containing the scene and its properties.
Returns:
bool: True if the active camera chain is valid, False otherwise.
"""
s = context.scene
if len(s.cam_chains) > 0:
chain = s.cam_chains[s.cam_active_chain]
return chain_valid(chain, context)[0]
else:
return False
[docs]
operation: StringProperty(
name="Operation",
description="Specify the operation to calculate",
default="Operation",
)
[docs]
async def execute_async(self, context):
"""Execute an asynchronous simulation for a specified camera chain.
This method retrieves the active camera chain from the current Blender
scene and determines the operations associated with that chain. It
checks if all operations are valid and can be simulated. If valid, it
proceeds to execute the simulation asynchronously. If any operation is
invalid, it logs a message and returns a finished status without
performing the
Args:
context: The context in which the operation is executed.
Returns:
dict: A dictionary indicating the status of the operation, either
operation completed successfully.
"""
s = bpy.context.scene
chain = s.cam_chains[s.cam_active_chain]
chainops = get_chain_operations(chain)
canSimulate = True
for operation in chainops:
if operation.name not in bpy.data.objects:
canSimulate = True # force true
print("Operation Name " + str(operation.name))
if canSimulate:
try:
await do_simulation(chain.name, chainops)
except AsyncCancelledException as e:
return {"CANCELLED"}
else:
print("No Computed Path to Simulate")
return {"FINISHED"}
return {"FINISHED"}
[docs]
def draw(self, context):
"""Draw the user interface for selecting camera operations.
This function creates a user interface element that allows the user to
search and select a specific camera operation from a list of available
operations in the current scene. It utilizes the Blender Python API to
create a property search layout.
Args:
context: The context in which the drawing occurs, typically containing
information about the current scene and UI elements.
"""
layout = self.layout
layout.prop_search(self, "operation", bpy.context.scene, "cam_operations")