Message 1 of 2
Group instead a curve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
import maya.cmds as cmds
import os
"""
Duplicate object(s) and convert it/them into prop
"""
def create_circular_shapes_and_groups():
# Get selected objects
selected_objects = cmds.ls(selection=True)
if not selected_objects:
cmds.warning("No hay objetos seleccionados.")
return
# Get bounding box selection
bounding_box = cmds.exactWorldBoundingBox(selected_objects)
# Calculate BB radius
radius = max(bounding_box[3] - bounding_box[0], bounding_box[5] - bounding_box[2]) / 2.0
# create group local
group_local = cmds.group(em=True, name="group_local")
# Create shape local
circle_local = cmds.circle(name="local_shape", center=(0, 0, 0), normal=(0, 1, 0), radius=radius)[0]
# parent local to group local
cmds.parent(circle_local, group_local)
# Yellow local
shape_node_local = cmds.listRelatives(circle_local, shapes=True, fullPath=True)[0]
cmds.setAttr(f"{shape_node_local}.overrideEnabled", 1)
cmds.setAttr(f"{shape_node_local}.overrideColor", 17)
# Parent selected objects to local
cmds.parent(selected_objects, circle_local)
# Create group world
group_world = cmds.group(em=True, name="group_world")
# Create shape world 110% bigger
radius_world = radius * 1.1
circle_world = cmds.circle(name="world_shape", center=(0, 0, 0), normal=(0, 1, 0), radius=radius_world)[0]
# Parent circle world to group_world
cmds.parent(circle_world, group_world)
# Yellow to world
shape_node_world = cmds.listRelatives(circle_world, shapes=True, fullPath=True)[0]
cmds.setAttr(f"{shape_node_world}.overrideEnabled", 1)
cmds.setAttr(f"{shape_node_world}.overrideColor", 17)
# Parent group_local to world
cmds.parent(group_local, circle_world)
# Duplicate special selection
def duplicate_special():
# Get selection
selected_objects = cmds.ls(selection=True)
# Check if there is a selection
if selected_objects:
# Duplicate Special
# 'rr' (return roots) avoid reference duplication, create local object
duplicates = cmds.duplicate(selected_objects, rr=True)
selected_objects = duplicates
# Isolate objects
cmds.isolateSelect(cmds.paneLayout('viewPanes', q=True, pane1=True), state=1)
cmds.isolateSelect(cmds.paneLayout('viewPanes', q=True, pane1=True), addSelected=True)
# Return the duplicated objects
return selected_objects
else:
print("No objects selected.")
return None
# Locator to selection
def locator_to_edit_selection(selected_objects):
if not selected_objects:
cmds.warning("No selection.")
return
# Bounding box boundaries
bounding_box = cmds.exactWorldBoundingBox(selected_objects)
# Calculate x,z and Y to ground
center_x = (bounding_box[0] + bounding_box[3]) / 2.0 # X center
center_z = (bounding_box[2] + bounding_box[5]) / 2.0 # Z center
min_y = bounding_box[1] # Lower Y
# Create locator in the center
locator = cmds.spaceLocator(name="handle")[0]
cmds.xform(locator, worldSpace=True, translation=(center_x, min_y, center_z))
# Parent objects to locator
cmds.parent(selected_objects, locator)
# Geometry not selectable
for obj in selected_objects:
cmds.setAttr(f"{obj}.overrideEnabled", 1) # Activate override
cmds.setAttr(f"{obj}.overrideDisplayType", 2) # Not selectable
# Move locator to origin and freeze transforms
cmds.xform(locator, worldSpace=True, translation=(0, 0, 0), rotation=(0, 0, 0), scale=(1, 1, 1))
cmds.select('handle')
cmds.setToolTo('Rotate')
# Focus to object
cmds.viewFit()
# Return the updated selection
return selected_objects
# Create reference
def create_reference(selected_objects):
if not selected_objects:
cmds.warning("No selected objects")
return
# Parent the objects to the world and freeze transformations
cmds.parent(selected_objects, world=True)
cmds.makeIdentity(selected_objects, apply=True, translate=True, rotate=True, scale=True, normal=False)
cmds.delete('handle')
create_circular_shapes_and_groups()
#
#
#
# Ask for path and file name
export_path = cmds.fileDialog2(fileFilter="Maya ASCII (*.ma)", dialogStyle=2, fileMode=0)
# No file, no job
if not export_path:
cmds.warning("No se seleccionó ninguna ruta de exportación.")
return
# fileDialog2 returns a list, so we need to extract the first item (the path)
export_path = export_path[0]
# Name as namespace
file_name = os.path.splitext(os.path.basename(export_path))[0]
# Export objects to the selected path
cmds.file(export_path, exportSelected=True, type="mayaAscii")
# Delete the selected objects from the scene
cmds.delete(selected_objects)
# Reference the exported file back into the scene with the file name as the namespace
cmds.file(export_path, reference=True, namespace=file_name)
current_panel = cmds.getPanel(withFocus=True)
# Deactivate isolate
if cmds.getPanel(typeOf=current_panel) == "modelPanel":
cmds.isolateSelect(current_panel, state=False)
# Close the window
cmds.deleteUI('customWindow', window=True)
cmds.delete('group_world')
# Export reference
def export_reference(selected_objects):
# Check if the window already exists and delete it if necessary
if cmds.window('customWindow', exists=True):
cmds.deleteUI('customWindow', window=True)
# Create pop-up window
window = cmds.window('customWindow', title="Orient the prop", widthHeight=(400, 400))
cmds.columnLayout(adjustableColumn=True)
# Text instruction
cmds.text(label="Please, set up the prop correctly and press Finish.")
# Button to execute the reference creation
cmds.button(label="Finish", command=lambda *args: create_reference(selected_objects))
# Show the window
cmds.showWindow(window)
# Main workflow
# Step 1: Duplicate special selection
selected_objects = duplicate_special()
# Step 2: Create locator and parent the duplicates to it
selected_objects = locator_to_edit_selection(selected_objects)
# Step 3: Export reference using the selected objects
export_reference(selected_objects)
This code should create a copy of selected objects from a scene to adjust their rotation in the origin and create curves as controllers. But the world_shape is a group not a curve... how could I fix it? I know it's something about transforms and shapes but I'm lost right now. Any help?