<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating a new label class in Arc Pro using the arcpy CIM in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1313044#M68277</link>
    <description>&lt;P&gt;My solution is different, and may be of help to you if you are still experiencing this issue.&amp;nbsp; &amp;nbsp;I have figured out a way to bypass the corruption of the CIM definition when attempting to create a new label class with ArcPy CIM.&amp;nbsp; To test this out, you can use any layer those default class is named "Class 1." Instead of using&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="andale mono,times" color="#008000"&gt;arcpy.cim.CreateCIMObjectFromClassName('CIMLabelClass','V2')&amp;nbsp;&lt;/FONT&gt;or&amp;nbsp;&lt;FONT face="andale mono,times" color="#008000"&gt;arcpy.cim.CIMLabelClass()&lt;/FONT&gt;&amp;nbsp;as suggested by other users, I used&amp;nbsp;&lt;FONT face="andale mono,times" color="#008000"&gt;cimObject.labelClasses.copy()&lt;/FONT&gt;.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;It's a little awkward, but I hope it helps!&lt;/P&gt;&lt;P&gt;The code to try out is below:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

p = arcpy.mp.ArcGISProject('CURRENT')
m = p.listMaps('Map')[0]
layer = m.listLayers("Your Layer Name Here")[0]
cimObject = layer.getDefinition('V2')
lblClasses = cimObject.labelClasses
    
for lblClass in lblClasses:
    if lblClass.name == "Class 1":
        nlc_index = cimObject.labelClasses.index(lblClass)
        lblClass_copy = cimObject.labelClasses.copy()[nlc_index]
        cimObject.labelClasses.append(lblClass_copy)
        layer.setDefinition(cimObject)
        break

cimObject = layer.getDefinition('V2')
lblClasses = cimObject.labelClasses        
        
for lblClass in lblClasses:
    if lblClass.name == "Class 1":
        nlc_index = cimObject.labelClasses.index(lblClass)
        if nlc_index != 0:
            lblClass.name = "Dummy"
            layer.setDefinition(cimObject)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jul 2023 13:08:44 GMT</pubDate>
    <dc:creator>DestinyKelley1</dc:creator>
    <dc:date>2023-07-28T13:08:44Z</dc:date>
    <item>
      <title>Creating a new label class in Arc Pro using the arcpy CIM</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/511771#M40234</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am attempting to create a new label class for a layer using the arcpy CIM, code below. The new label class gets created, but ArcGIS Pro immediately crashes when I attempt to interact with it in the GUI. I have tried in the python window and in a script.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;aprx = arcpy.mp.ArcGISProject(&lt;SPAN style="color: #6a8759;"&gt;'CURRENT'&lt;/SPAN&gt;)
m = aprx.listMaps(&lt;SPAN style="color: #6a8759;"&gt;"Map"&lt;/SPAN&gt;)[&lt;SPAN style="color: #6897bb;"&gt;0&lt;/SPAN&gt;]
lyr = m.listLayers(&lt;SPAN style="color: #6a8759;"&gt;"SOME LAYER"&lt;/SPAN&gt;)[&lt;SPAN style="color: #6897bb;"&gt;0&lt;/SPAN&gt;]
ldef = lyr.getDefinition(&lt;SPAN style="color: #6a8759;"&gt;'V2'&lt;/SPAN&gt;)

newlblclass = arcpy.cim.CreateCIMObjectFromClassName(&lt;SPAN style="color: #6a8759;"&gt;'CIMLabelClass'&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #6a8759;"&gt;'V2'&lt;/SPAN&gt;)
newlblclass.name = &lt;SPAN style="color: #6a8759;"&gt;"NEW CLASS"
&lt;/SPAN&gt;newlblclass.visibility = &lt;SPAN style="color: #cc7832;"&gt;True
&lt;/SPAN&gt;ldef.labelClasses.append(newlblclass)
lyr.setDefinition(ldef)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:24:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/511771#M40234</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T22:24:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new label class in Arc Pro using the arcpy CIM</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1000623#M59021</link>
      <description>&lt;P&gt;May be there are required properties that need setting? All you set is the name and visibility, but what about the field that the label is derived from?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2020 12:06:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1000623#M59021</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2020-11-12T12:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new label class in Arc Pro using the arcpy CIM</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1001847#M59062</link>
      <description>&lt;P&gt;Correct. After researching there are many "nested" CIMobjects that make up a label class cim object. All of these objects need to be created and assigned in order for the label class object to function properly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Reference:&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/arcpy/mapping/python-cim-access.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/arcpy/mapping/python-cim-access.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 18:49:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1001847#M59062</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2020-11-16T18:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new label class in Arc Pro using the arcpy CIM</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1132448#M63469</link>
      <description>&lt;P&gt;Can you please share your label class creation solution? I have the same problem and I can't solve it. Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jan 2022 15:02:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1132448#M63469</guid>
      <dc:creator>anyry</dc:creator>
      <dc:date>2022-01-11T15:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new label class in Arc Pro using the arcpy CIM</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1313044#M68277</link>
      <description>&lt;P&gt;My solution is different, and may be of help to you if you are still experiencing this issue.&amp;nbsp; &amp;nbsp;I have figured out a way to bypass the corruption of the CIM definition when attempting to create a new label class with ArcPy CIM.&amp;nbsp; To test this out, you can use any layer those default class is named "Class 1." Instead of using&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;FONT face="andale mono,times" color="#008000"&gt;arcpy.cim.CreateCIMObjectFromClassName('CIMLabelClass','V2')&amp;nbsp;&lt;/FONT&gt;or&amp;nbsp;&lt;FONT face="andale mono,times" color="#008000"&gt;arcpy.cim.CIMLabelClass()&lt;/FONT&gt;&amp;nbsp;as suggested by other users, I used&amp;nbsp;&lt;FONT face="andale mono,times" color="#008000"&gt;cimObject.labelClasses.copy()&lt;/FONT&gt;.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;It's a little awkward, but I hope it helps!&lt;/P&gt;&lt;P&gt;The code to try out is below:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

p = arcpy.mp.ArcGISProject('CURRENT')
m = p.listMaps('Map')[0]
layer = m.listLayers("Your Layer Name Here")[0]
cimObject = layer.getDefinition('V2')
lblClasses = cimObject.labelClasses
    
for lblClass in lblClasses:
    if lblClass.name == "Class 1":
        nlc_index = cimObject.labelClasses.index(lblClass)
        lblClass_copy = cimObject.labelClasses.copy()[nlc_index]
        cimObject.labelClasses.append(lblClass_copy)
        layer.setDefinition(cimObject)
        break

cimObject = layer.getDefinition('V2')
lblClasses = cimObject.labelClasses        
        
for lblClass in lblClasses:
    if lblClass.name == "Class 1":
        nlc_index = cimObject.labelClasses.index(lblClass)
        if nlc_index != 0:
            lblClass.name = "Dummy"
            layer.setDefinition(cimObject)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 13:08:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1313044#M68277</guid>
      <dc:creator>DestinyKelley1</dc:creator>
      <dc:date>2023-07-28T13:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new label class in Arc Pro using the arcpy CIM</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1354222#M69301</link>
      <description>&lt;P&gt;Just wondering if you can change the label class name before you append it in the first for loop&lt;/P&gt;&lt;P&gt;something like this&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for lblClass in lblClasses:
    if lblClass.name == "Class 1":
        nlc_index = cimObject.labelClasses.index(lblClass)
        lblClass_copy = cimObject.labelClasses.copy()[nlc_index]
        
        lblClass_copy.name = "new_class_name"
        cimObject.labelClasses.append(lblClass_copy)
        layer.setDefinition(cimObject)
        break&lt;/LI-CODE&gt;&lt;P&gt;I have not tested this, but just wondering if this will optimize the code and avoid the second for loop....&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 18:13:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-new-label-class-in-arc-pro-using-the/m-p/1354222#M69301</guid>
      <dc:creator>JingchaoZhou</dc:creator>
      <dc:date>2023-11-28T18:13:09Z</dc:date>
    </item>
  </channel>
</rss>

