Skip to content

Instantly share code, notes, and snippets.

@csprance
Created August 6, 2018 16:29
Show Gist options
  • Save csprance/6414d5ae16227b95ba6519b3e3f86a4c to your computer and use it in GitHub Desktop.
Save csprance/6414d5ae16227b95ba6519b3e3f86a4c to your computer and use it in GitHub Desktop.
"""
@name: cryengine_setup.py
@author: Chris Sprance
@project: Miscreated
@description: Sets up an fbx from modo to be ready to export in maya
@instructions: Select top level group(s) hit button. fist bump
"""
import re
import maya.mel as mm
import pymel.core as pm
def strip_garbage_fbx_characters(node):
"""
strips any bogus character created by fbx import
:return Node - renamed node or original node
"""
regex = r'(FBX.*)'
matches = re.findall(regex, str(node))
if matches:
new_name = re.sub(regex, "", str(node))
return pm.rename(node, new_name)
return node
def create_cryexport_node(node, groups):
""" creates and parents a cryexport node to a selection of groups Example: cryExportNode_meshname """
regex = "cryexportnode_"
re.sub(regex, "", str(node))
node_name = 'cryExportNode_%s' % re.sub(regex, "", str(node))
group = pm.group(groups, n=node_name, w=True, r=True)
pm.xform(piv=(0, 0, 0)) # set the pivot to 0,0,0
return group
def create_cryexport_group_node(node, meshes):
"""
creates and parents a group node to a selection of meshes
Example:
meshname_group
"""
regex = "cryexportnode_"
re.sub(regex, "", str(node))
group_name = '%s_group' % re.sub(regex, "", str(node))
return pm.group(meshes, n=group_name, w=True)
def harden_uv_borders(mesh):
"""
Runs the mel scripts harden_uv_borders
"""
pm.select(cl=True)
pm.select(mesh)
# mm.eval('source harden_uv_borders.mel')
return mesh
def get_meshes(group):
"""
Gets meshes in a group
:param group: a meshname_group node
:return: a list of meshes in the group
"""
meshes = list()
group_meshes = group.listRelatives(type='transform')
for mesh in group_meshes:
meshes.append(harden_uv_borders(strip_garbage_fbx_characters(mesh)))
return meshes
def get_groups(node):
"""
gets all the groups transform nodes
:param node:
:return: list of nodes
"""
return node.listRelatives(type='transform')
def set_translate_to_zero(node):
"""
if an objects Translation is not zeroed out. Do that
:param node: a pymel node
"""
x, y, z = node.getTranslation()
if x != 0.0 or y != 0.0 or z != 0.0:
node.setTranslation([0.0, 0.0, 0.0])
def start():
# set the framerate to 30
pm.currentUnit(time='ntsc')
# get selected FBX locators
for node in pm.selected():
set_translate_to_zero(node)
groups = get_groups(node)
cryexport_groups = [
create_cryexport_group_node(node, get_meshes(group))
for group in groups
]
create_cryexport_node(node, cryexport_groups)
# delete all the old stuff
pm.delete(groups, node)
# set up the material
mm.eval('source cryMaterial.mel')
mm.eval('cryMaterialWin')
mm.eval('cryMaterialUICreateGroupFromSelection()')
# add the attributes to the export nodes and the scene
mm.eval('source cryExport.mel')
mm.eval('cryExportAddAttributes()')
if __name__ == '__main__':
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment